I am currently in a beginners Java Programming course and am having trouble with
ID: 3659739 • Letter: I
Question
I am currently in a beginners Java Programming course and am having trouble with this homework problem. If someone has any input on the question please post your thoughts. Thanks. Java Illuminated 3rd edition Chapter 11 #59 Design a class that calculates statistics on data in a file. We expect the file to contain grades represented by integer values, one per line. If you encounter a value that is not an integer, you should throw an exception, print a message to the console, skip that value, and continues processing. Store the grades that you read in an ArrayList so that all the grades are available for retrieval. You should also have, as a minimum, methods that return the grade average, the highest grade, the lowest grade, and ones that return all the grades as an array of letter grades. Test your class with a client class.Explanation / Answer
//ParseGrades.java
package javaapplication2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ParseGrades
{
final String FILE_NAME = "D:\Cramster\grades.txt";
public ArrayList _ListGrades= new ArrayList();
public ArrayList _ListLetterGrades= new ArrayList();
public void ConvertToLetterGrades()
{
for(int i=0;i<_ListGrades.size();i++)
{
switch((int)_ListGrades.get(i))
{
case 1: _ListLetterGrades.add("A"); break;
case 2: _ListLetterGrades.add("B"); break;
case 3: _ListLetterGrades.add("C"); break;
case 4: _ListLetterGrades.add("D"); break;
default:_ListLetterGrades.add("F");break;
}
}
}
public void DisplayGrades()
{
for(int i=0;i<_ListGrades.size();i++)
{
System.out.println(_ListGrades.get(i).toString());
}
}
public void DisplayLetterGrades()
{
for(int i=0;i<_ListLetterGrades.size();i++)
{
System.out.println(_ListLetterGrades.get(i).toString());
}
}
public int FindMin()
{
int min = (int)_ListGrades.get(0);
for(int i=0;i<_ListGrades.size();i++)
{
if(min > (int)_ListGrades.get(i))
min = (int)_ListGrades.get(i);
}
return min;
}
public int FindMax()
{
int max = (int)_ListGrades.get(0);
for(int i=0;i<_ListGrades.size();i++)
{
if(max < (int)_ListGrades.get(i))
max = (int)_ListGrades.get(i);
}
return max;
}
public double FindAvg()
{
int sum = 0;
for(int i=0;i<_ListGrades.size();i++)
{
sum = sum + (int)_ListGrades.get(i);
}
return (double)sum /_ListGrades.size();
}
public void ReadFile() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
try
{
String line = br.readLine();
while (line != null)
{
try
{
_ListGrades.add(Integer.parseInt(line));
}
catch(Exception exp)
{
System.out.println(line + " is not a number.");
}
line = br.readLine();
}
}
finally
{
br.close();
}
}
}
//Client class
package javaapplication2;
public class ClientClass
{
public static void main(String[] args) throws Exception
{
ParseGrades _pg = new ParseGrades();
_pg.ReadFile();
_pg.DisplayGrades();
System.out.println("Min grade : "+_pg.FindMin());
System.out.println("Max grade : "+_pg.FindMax());
System.out.println("Avg grade : "+_pg.FindAvg());
_pg.ConvertToLetterGrades();
_pg.DisplayLetterGrades();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.