Student Array Write a Java application that does each of the following: For each
ID: 3807414 • 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
package grades;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.util.stream.Stream;
public class StudentAppli {
private Grades[] grades;
BufferedReader br;
public StudentAppli() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public void readFile() {
System.out.println("**********Welcome to Student Application********");
System.out.println("Please enter the file name to be read:");
try {
String fileName = br.readLine();
File file = new File(fileName);
// Creating grades array
try (Stream<String> lines = Files.lines(file.toPath())) {
grades = new Grades[(int) lines.count()];
}
FileReader frd = new FileReader(file);
BufferedReader brd = new BufferedReader(frd);
String line;
int lineNum = 0;
while ((line = brd.readLine()) != null) {
String[] row = line.split(" ");
double sum = 0;
int noLabs = 0;
for (int i = 1; i < row.length; i++) {
sum += Integer.parseInt(row[i]);
noLabs++;
}
//System.out.println("Average: "+(sum / noLabs));
grades[lineNum++] = new Grades(row[0], sum / noLabs);
}
brd.close();
frd.close();
}
catch(NoSuchFileException e){
System.out.println("File not found!");
System.exit(0);
}
catch(FileNotFoundException e){
System.out.println("File not found!");
System.exit(0);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void display() {
System.out.println("1. Class lab average");
System.out.println("2. Name and average of student with highest average");
System.out.println("3. Name and average of student with lowest average");
System.out.println("4. Searche for a student");
System.out.println("5. Exit");
}
public void runQueries() {
boolean check = true;
while (check) {
display();
//System.out.println("Length: "+grades.length);
try {
int option = Integer.parseInt(br.readLine());
switch (option) {
case 1:
labAverage();
break;
case 2:
highestAverage();
break;
case 3:
lowestAverage();
break;
case 4:
searchStudent();
break;
case 5:
check = false;
break;
default:
System.out.println("Invalid input!!");
break;
}
} catch (NumberFormatException | IOException e) {
System.out.println("Invalid input");
}
}
}
private void searchStudent() {
System.out.println("Enter the student name:");
try {
String name = br.readLine();
boolean found = false;
for (int i = 0; i < grades.length; i++) {
if (grades[i].getName().equalsIgnoreCase(name)) {
found = true;
System.out.println("Name: " + name + " Grade Avg: " + grades[i].getAvg());
}
}
if (!found) {
System.out.println("Entered name not in students list");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void lowestAverage() {
System.out.println("Student with lowest avg:");
double min = Double.MAX_VALUE;
int index = -1;
for (int i = 0; i < grades.length; i++) {
if (grades[i].getAvg() < min) {
min = grades[i].getAvg();
index = i;
}
}
System.out.println("Name: " + grades[index].getName() + " Grade Avg: " + grades[index].getAvg());
}
private void highestAverage() {
System.out.println("Student with highest avg:");
double max = Double.MIN_VALUE;
int index = -1;
for (int i = 0; i < grades.length; i++) {
if (grades[i].getAvg() > max) {
max = grades[i].getAvg();
index = i;
}
}
System.out.println("Name: " + grades[index].getName() + " Grade Avg: " + grades[index].getAvg());
}
private void labAverage() {
System.out.println("Class average:");
double sum = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[i].getAvg();
}
System.out.println("Grade Avg: " + sum/grades.length);
}
public static void main(String[] args) {
StudentAppli app = new StudentAppli();
app.readFile();
app.runQueries();
}
}
Output1:
**********Welcome to Student Application********
Please enter the file name to be read:
student.info
File not found!
Output2:
**********Welcome to Student Application********
Please enter the file name to be read:
student.txt
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
1
Class average:
Grade Avg: 74.3625
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
2
Student with highest avg:
Name: Student3
Grade Avg: 79.0
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
3
Student with lowest avg:
Name: Student4
Grade Avg: 67.0
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
4
Enter the student name:
5
Entered name not in students list
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
2
Student with highest avg:
Name: Student3
Grade Avg: 79.0
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
4
Enter the student name:
Student1
Name: Student1
Grade Avg: 73.25
1. Class lab average
2. Name and average of student with highest average
3. Name and average of student with lowest average
4. Searche for a student
5. Exit
5