I have completed this assignment, but am looking for a way to make the program m
ID: 3624321 • Letter: I
Question
I have completed this assignment, but am looking for a way to make the program more efficient if possible. Mainly by eliminating all the if statements in the code used to determine the letter grade of the student.Use the file grades.txt.
Modify your Grade reader program so that the following values are printed.
* The number of grades in the file.
* The sum of the grades in the file
* The average of the grades in the file
* The highest grade in the file
* The lowest grade in the file
* The number of As, Bs, Cs, Ds, and Fs in the file given the following grade scale:
* A: 90 <= grade <= 100
* B: 80 <= grade < 90
* C: 70 <= grade < 80
* D: 60 <= grade < 70
* F: below 60
Your results should be in complete sentences (or easily understood).
-------------------------------------------------------------------------------------------------------
My Code:
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class InputReader
{
Scanner in;
public InputReader()
{
in = new Scanner(System.in);
}
public void GradeReader() throws FileNotFoundException
{
String fileName = "grades.txt";
FileReader input = new FileReader(fileName);
in = new Scanner(input);
}
public void printGradesFromInput()
{
int sum = 0;
int lowest = 110;
int highest = 0;
int count = 0;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
while(in.hasNextInt())
{
int num = in.nextInt();
if(num > highest)
{
highest = num;
}
if(num < lowest)
{
lowest = num;
}
if(num >= 90)
{
aCount++;
}
if(num >= 80 && num < 90)
{
bCount++;
}
if(num >= 70 && num < 80)
{
cCount++;
}
if(num >= 60 && num < 70)
{
dCount++;
}
if(num < 60)
{
fCount++;
}
sum = sum + num;
count++;
}
double avg = sum / count;
System.out.println();
System.out.println("Number of grades: " + count);
System.out.println("The sum is: " + sum);
System.out.println("The highest grade is: " + highest);
System.out.println("The lowest grade is: " + lowest);
System.out.println("The average grade is: " + avg);
System.out.println();
System.out.println("The number of A's: " + aCount);
System.out.println("The number of B's: " + bCount);
System.out.println("The number of C's: " + cCount);
System.out.println("The number of D's: " + dCount);
System.out.println("The number of F's: " + fCount);
}
}
Explanation / Answer
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.