Student ArrayList Write a Java application that does each of the following: For
ID: 3807415 • Letter: S
Question
Student ArrayList
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, arrayList for this application.
Explanation / Answer
code.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class code {
public static void main(String[] args)
{
int[] grades = new int[1000];
String[] names = new String[1000];
int maxx = Integer.MIN_VALUE;
int minn = Integer.MAX_VALUE;
int maxind = 0; int minind = 0;
int records = 0;
try{
File file = new File("C:\Users\Akash\Documents\NetBeansProjects\JavaApplication2\src\javaapplication2\input.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int i =0;
while(true)
{
String line;
line = bufferedReader.readLine();
if(line == null)
{
break;
}
records++;
String[] parts = line.split(" ");
names[i] = parts[0];
int sum = 0;
for(int j = 1; j< parts.length;j++)
{
sum = sum + Integer.parseInt(parts[j]);
}
grades[i] = sum/(parts.length - 1);
if(maxx <= grades[i])
{
maxind = i;
}
if(minn >= grades[i])
{
minind = i;
}
maxx = Math.max(maxx, grades[i]);
minn = Math.max(minn, grades[i]);
i++;
}
System.out.println("Name"+ " " + "Average Grades");
for(int x = 0; x < records; x++)
{
System.out.println(names[x] + " " + grades[x]);
}
System.out.println(names[maxind] + " has Maximum Average marks= " + grades[maxind]);
System.out.println(names[minind] + " has minimum Average marks= " + grades[minind]);
while(true)
{
String n;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter name you want to search");
n = scanner.next();
boolean flag = true;
for(int x = 0; x < records; x++)
{
if(names[x].equals(n))
{
System.out.println(names[x] + " " + grades[x]);
flag = false;
break;
}
}
if(flag == true)
{
System.out.println("No Student with name " + n + " in database!");
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Sample input.txt:
David 73 44 34 23 55 66
Matt 73 44 23 55 66
Smith 73 44 34 55 66
Marsh 73 44 34 23
Sample Output:
Name Average Grades
David 49
Matt 52
Smith 54
Marsh 43
Smith has Maximum Average marks= 54
Marsh has minimum Average marks= 43
Enter name you want to search
Smith
Smith 54
Enter name you want to search
Akash
No Student with name Akash in database!
Enter name you want to search