Student Array Write a Java application that does each of the following: For each
ID: 3808241 • Letter: S
Question
Student Array
Write a Java application that does each of the following:
For each student
Reads from a file the name and a series of labs for each person (students do not all have the same number of labs)
Calculates the average of the labs
Stores the name and average in an array of class Grades.
Prints the information from the array using a “for loop”
Prints the following information with appropriate documentation:
Class lab average
Name and average of student with highest average
Name and average of student with lowest average
Searches for particular students
Asks the user for a name
If the name is in the array, prints the name and average for that person.
If the name is not in the array, prints a message to the user indicating the name does not exist.
Continues to ask the user for names until the user wants to stop
You must use one, and only one, array for this application. do NOT use multiple arrays.
Explanation / Answer
//Code as per requirements
import java.util.*;
import java.io.*;
class Grade
{
private String name;
private int avg;
public void set(String name,int a)
{
this.name=name;
avg=a;
}
public String getname()
{
return name;
}
public int getavg()
{
return avg;
}
}
public class Lab
{
public static void main(String args[])// throws FileNotFoundException
{
Grade high=new Grade();
Grade low=new Grade();
int l=10000;//MAximum value
int h=0;//lowest value
Grade arr[]=new Grade[100];
int count=0;
try
{
Scanner s=new Scanner(new File("in.txt"));
//System.out.println("Enter no of students")
while(s.hasNextLine())
{
String data[]=s.nextLine().split(" ");
int sum=0;
for(int i=1;i<data.length;i++)
sum+=(Integer.parseInt(data[i]));
//System.out.println(sum);
int avg=sum/(data.length-1);
Grade hh=new Grade();
hh.set(data[0],avg);
arr[count]=hh; count++;
if(l>avg)
{l=avg;
low.set(data[0],avg);
}
if(h<avg)
{
h=avg;
high.set(data[0],avg);
}
}
Scanner nn=new Scanner(System.in);
System.out.println("Enter the name to search: write quit for stop");
while(true)
{
String input=nn.nextLine();
if(input.equals("quit"))
break;
else
{
int flag=0;
for(int i=0;i<count;i++)
{
if(arr[i].getname().equals(input))
{
flag=0;
System.out.println("Name found");
System.out.println("Name :"+arr[i].getname()+" Average :"+arr[i].getavg());
break;
}
else
flag=1;
}
if(flag==1)
System.out.println("Name not found");
}
}
System.out.println("Highest average : Name -"+high.getname()+" Average-"+high.getavg());
System.out.println("Lowest average : Name -"+low.getname()+" Average-"+low.getavg());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//Input file - in.txt
fdsaf 12 4 34 32 532 21
aa 321 3243 2432 423 452 12
rr 4234 24234 21 23 43 1
//Output :
Enter the name to search: write quit for stop
aa
Name found
Name :aa Average :1147
rr
Name found
Name :rr Average :4759
quit
Highest average : Name -rr Average-4759
Lowest average : Name -fdsaf Average-105