Write a Java class, called LetterGradeConverter, encapsulating the concept of co
ID: 3912128 • Letter: W
Question
Write a Java class, called LetterGradeConverter, encapsulating the concept of converting integer grades to letter grades (A, B, C, D, or F), assuming grades are composed of a list of integers between 0 and 100. The LetterGradeConverter class has three instance variables: an array of integer grades called intGrades, a character array representing the letter grades called LettergradeList, and an integer variable for the actual size of the arrays called actialLength. The LetterGradeConverter class includes the following methods: ? A constructor having two parameters: the name of an input text file, which contains the integer grades of students, and the maximum size of grades list. The constructor would read the integer grades from the input file to initialize the intGrades array, then calls an internal method to convert the integer grades into letter grades. ? A private method, called LetterGradeConverter, which generates the list of letter grades from the list of integer grades. It determines the letter grade corresponding to each integer grade based on the following table: Letter Grade Integer Grade (M) A M ? 90 B 80 ? M < 90 C 70 ? M < 80 D 60 ? M < 70 F M < 60 ? toString() method that returns a nicely formatted string of the LetterGradeConvertor in the form of two-column table. The first column contains the integer grades and the second column contains the corresponding letter grades. ? equals() method that returns true if this LetterGradeConverter object is equal to the passed object. Two LetterGradeConverter objects are considered equal if their corresponding letter grades lists are the same. ? Accessor (i.e., getter) methods for the instance variables. (b) Write a client class, called LetterGradeDisplayer, to test all methods in your class. The client class should allow the user to enter the names of two input files that contain the integer grades of two classes to be converted to letter grades. The LetterGradeDisplayer compares between the two classes lists in terms of equality and displays their integer grades, and their letter grades.
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class LetterGradeConverter {
private int intGrades[];
private char LettergradeList[];
private int actalLength;
//parameterized constructor
LetterGradeConverter(String fileName,int maxGradeSize){
intGrades = new int[maxGradeSize];
actalLength = maxGradeSize;
LettergradeList = new char[maxGradeSize];
//open file
File file = new File(fileName); //reading data from this file
Scanner reader;
int counter=0;
//read int value
try {
reader = new Scanner(file);
while(reader.hasNextInt()){
intGrades[counter] = reader.nextInt();
counter++;
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//call grade converter
GradeConverter();
}
private void GradeConverter(){
for(int i=0;i<actalLength;i++){
int grade = intGrades[i];
if(grade>=90){
LettergradeList[i] = 'A';
}else if(grade>=80){
LettergradeList[i] = 'B';
}else if(grade>=70){
LettergradeList[i] = 'C';
}else if(grade>=60){
LettergradeList[i] = 'D';
}else if(grade<60){
LettergradeList[i] = 'F';
}
}
}
//override to string
@Override
public String toString() {
String output="";
output+=String.format("%-20s%s ", "Int Grade","Letter Grade");
for(int i=0;i<actalLength;i++){
output+=String.format("%-20d%c ", intGrades[i],LettergradeList[i]);
}
return output;
}
//override equal method
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
LetterGradeConverter other = (LetterGradeConverter) obj;
if (!Arrays.equals(LettergradeList, other.LettergradeList))
return false;
if (actalLength != other.actalLength)
return false;
return true;
}
}
-----------------------------------------------------------------------------------------------------
public class LetterGradeDisplayer {
public static void main(String[] args) {
LetterGradeConverter lgc1 = new LetterGradeConverter("grade1.txt", 6);
System.out.println("First grade object content");
System.out.println(lgc1);
LetterGradeConverter lgc2 = new LetterGradeConverter("grade2.txt", 6);
System.out.println("Second grade object content");
System.out.println(lgc2);
System.out.println("Both grade object are same ? "+lgc1.equals(lgc2));
}
}
---------------------------------------------------------
grade1.txt
100 89 77 55 22 11
grade2.txt
100 82 70 40 50 51
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.