This program runs completely correct. It\'s a program that uses array lists to g
ID: 3540853 • Letter: T
Question
This program runs completely correct. It's a program that uses array lists to give an output of 5 students average grades. It imports a txt file for the information needed. I need to split this into two separate files: GradeBook.java and GradeBookTest.java. 3,000 pts to someone who can do it.
Java file:
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class GradeBook {
public static void main(String args[])throws Exception{
File file = new File("grades.txt");
if(!file.exists() || file.isDirectory()){
System.out.println("Wrong input file.......");
System.out.println("Exiting.....");
System.exit(0);
}
Scanner scan = new Scanner(new FileInputStream("grades.txt"));
HashMap<String, Double> studentsAverage = new HashMap<String, Double>();
ArrayList<String> students = new ArrayList<String>();
String name = "";
String value = "";
double total = 0;
int count = 0;
while(scan.hasNext()){
value = scan.nextLine();
try{
total +=Integer.parseInt(value.trim());
count++;
}catch(Exception e){
if(value.trim().equals("")){
continue;
}
if(!"".equals(name)){
studentsAverage.put(name, total/count);
students.add(name);
count = 0;
total = 0;
}
name = value;
}
}
System.out.println("Nona Poehler August 22nd, 2013 ");
if(count != 0){
studentsAverage.put(name, total/count);
students.add(name);
}
for(String student : students){
double average = studentsAverage.get(student);
String grade = "";
if(average >= 90){
grade = "A";
}else if(average >= 80){
grade = "B";
}else if(average >= 70){
grade = "C";
}else if(average >= 60){
grade = "D";
}else{
grade = "E";
}
System.out.println("Name: " + student + " Average: " + average + " Grade: "+grade);
}
}
}
Txt file:
Joanne Smith
98
89
100
76
Will Jones
67
89
91
88
Kerry McDonald
78
79
88
91
Sam Young
88
98
76
56
Jill Barnes
94
93
91
98
Explanation / Answer
//SAVE IT AS GradeBook.java
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class GradeBook {
public static void AssignGrades()throws Exception{
File file = new File("grades.txt");
if(!file.exists() || file.isDirectory()){
System.out.println("Wrong input file.......");
System.out.println("Exiting.....");
System.exit(0);
}
Scanner scan = new Scanner(new FileInputStream("grades.txt"));
HashMap<String, Double> studentsAverage = new HashMap<String, Double>();
ArrayList<String> students = new ArrayList<String>();
String name = "";
String value = "";
double total = 0;
int count = 0;
while(scan.hasNext()){
value = scan.nextLine();
try{
total +=Integer.parseInt(value.trim());
count++;
}catch(Exception e){
if(value.trim().equals("")){
continue;
}
if(!"".equals(name)){
studentsAverage.put(name, total/count);
students.add(name);
count = 0;
total = 0;
}
name = value;
}
}
System.out.println("Nona Poehler August 22nd, 2013 ");
if(count != 0){
studentsAverage.put(name, total/count);
students.add(name);
}
for(String student : students){
double average = studentsAverage.get(student);
String grade = "";
if(average >= 90){
grade = "A";
}else if(average >= 80){
grade = "B";
}else if(average >= 70){
grade = "C";
}else if(average >= 60){
grade = "D";
}else{
grade = "E";
}
System.out.println("Name: " + student + " Average: " + average + " Grade: "+grade);
}
}
}
// END OF GradeBook.java
//SAVE IT AS GradeBookTest.java
public class GradeBookTest
{
public static void main(String[] args) throws Exception
{
GradeBook.AssignGrades();
}
}
// END OF GradeBookTest.java
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.