Write a program to declare & instantiate an 2D-array to hold marks obtained by students in different subjects in a class. Assume that there are up to 10 students in a class & there are 5 subjects.Find out the best student according to average marks of all subjects and display all the marks of him/her.
package assignment;
import java.io.*;
class student{
double s1,s2,s3,s4,s5,avg;
String name,s;
void add()
{
try{
System.out.print("Give the name :");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=br.readLine();
System.out.print("Give the Marks :");
s=br.readLine();
s1=Double.parseDouble(s);
s=br.readLine();
s2=Double.parseDouble(s);
s=br.readLine();
s3=Double.parseDouble(s);
s=br.readLine();
s4=Double.parseDouble(s);
s=br.readLine();
s5=Double.parseDouble(s);
avg=(s1+s2+s3+s4+s5)/5;
}
catch(Exception e)
{
System.out.println(e);
}
}
void show()
{
System.out.println(name);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
}
}
public class as16
{
public static void main(String[] args) {
try{
student[] ob=new student[10];
int i,j=0;
for(i=0;i<10;i++)
{
ob[i]=new student();
ob[i].add();
}
for(i=0;i<9;i++)
{
if(ob[i].avg>ob[i+1].avg){
j=i;
}
else
j=i+1;
}
System.out.println("The best student is\n");
ob[j].show();
}
catch(Exception e)
{
System.out.println(e);
}
}
}