Arrays Write a Java application that does each of the following: For each studen
ID: 3686538 • Letter: A
Question
Arrays 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 an arrayList and do NOT use multiple arrays.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
// Gradee Class
public class Grade {
private String name;
private double grade;
public Grade(String name, double grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
@Override
public String toString() {
return "Name: " + name + ", Grade: " + String.format("%.2f", grade);
}
}
class App {
public static void search(Grade[] grades, int n, String name){
for(int i=0; i<n; i++){
if(grades[i].getName().equalsIgnoreCase(name)){
System.out.println(grades[i].toString());
return;
}
}
System.out.println(name+ " does not exist.");
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = sc.next();
// opening file
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line;
// reading file
// creating Grade array of size 50;
Grade[] grades = new Grade[50];
int count = 0;
while ((line = br.readLine()) != null) {
// splitting line by space
String temp[] = line.trim().split("\s+");
// calculating average grade
double total = 0;
for (int i = 1; i < temp.length; i++) { // iterate through temp from
// 1 index, 0th index - name
// is store
total = total + Double.parseDouble(temp[i].trim());
}
double avg = total / (temp.length - 1); // one entry is for name
// Creating grade object
Grade grade = new Grade(temp[0], avg);
// storing into array
grades[count++] = grade;
}
br.close();
fr.close();
// printing all grade - Name and AverageGrade
for (int i = 0; i < count; i++)
System.out.println(grades[i].toString());
// printing max grade and name
int max_index = 0;
double maxGrade = grades[0].getGrade();
for (int i = 1; i < count; i++) {
if (grades[i].getGrade() > maxGrade) {
maxGrade = grades[i].getGrade();
max_index = i;
}
}
System.out.println("Maximum Average Grade: ");
System.out.println(grades[max_index].toString());
// printing min grade and name
int min_index = 0;
double minGrade = grades[0].getGrade();
for (int i = 1; i < count; i++) {
if (grades[i].getGrade() < minGrade) {
minGrade = grades[i].getGrade();
min_index = i;
}
}
System.out.println("Minimum Average Grade: ");
System.out.println(grades[min_index].toString());
//asking for name and searching
while(true){
System.out.print("Enter name: (E to exit)");
String name = sc.next();
if("E".equalsIgnoreCase(name))
break;
search(grades, count, name);
}
}
}
/*
Input file example:
grades.txt:
pravesh 65 76 45 87 67 90
mukesh 76 87 56 65 90 45
vikash 56 45 34 87 78
Alex 76 45
Bob 34 89 78 76 65 67
Output:
Enter file name:
grades.txt
Name: pravesh, Grade: 71.67
Name: mukesh, Grade: 69.83
Name: vikash, Grade: 60.00
Name: Alex, Grade: 60.50
Name: Bob, Grade: 68.17
Maximum Average Grade:
Name: pravesh, Grade: 71.67
Minimum Average Grade:
Name: vikash, Grade: 60.00
Enter name: (E to exit)pravesh
Name: pravesh, Grade: 71.67
Enter name: (E to exit)Bob
Name: Bob, Grade: 68.17
Enter name: (E to exit)E
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.