Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(A) Write a Java class, called LetterGradeConverter, encapsulating the concept o

ID: 3911392 • Letter: #

Question

(A) 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


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input files in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.


input file:grades1.txt
-----------
80 90 70 78 60 50 95

input file:grades2.txt
-----------
96 87 60 89 68 70 83

LetterGradeConverter.java
-------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LetterGradeConverter {
private int[] intGrades;
private char[] LettergradeList;
private int actualLength;

public LetterGradeConverter(String filename, int maxSize) throws FileNotFoundException
{
Scanner infile = new Scanner(new File(filename));
intGrades = new int[maxSize];
LettergradeList = new char[maxSize];
actualLength = 0;
while(infile.hasNextInt())
{
intGrades[actualLength] = infile.nextInt();
actualLength++;
}
infile.close();
LetterGradeConverter();
}

private void LetterGradeConverter()
{
for(int i = 0; i < actualLength; i++)
{
int M = intGrades[i];
if(M >= 90)
LettergradeList[i] = 'A';
else if(M >= 80)
LettergradeList[i] = 'B';
else if(M >= 70)
LettergradeList[i] = 'C';
else if(M >= 60)
LettergradeList[i] = 'D';
else
LettergradeList[i] = 'F';
}
}

public String toString()
{
String s = String.format("%10s %10s ", "Marks", "Grade");
for(int i = 0; i < actualLength; i++)
s += String.format("%10s %10s ", intGrades[i], LettergradeList[i]);

return s;

}

@Override
public boolean equals(Object obj) {
if(obj instanceof LetterGradeConverter)
{
LetterGradeConverter c2 = (LetterGradeConverter)obj;
if(actualLength == c2.actualLength)
{
for(int i = 0; i < actualLength; i++)
{
if(LettergradeList[i] != c2.LettergradeList[i]) //letter grades not matching
return false;
}
return true; //all grades matched
}
}
return false;
}
}


LetterGradeDisplayer.java
------------------------
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LetterGradeDisplayer {
public static void main(String[] args) {
String filename1, filename2;
int maxGrades = 50;
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter input filename 1 containing grades: ");
filename1 = keyboard.next();

System.out.print("Enter input filename 2 containing grades: ");
filename2 = keyboard.next();


try {
LetterGradeConverter c1 = new LetterGradeConverter(filename1, maxGrades);
LetterGradeConverter c2 = new LetterGradeConverter(filename2, maxGrades);

System.out.println("Grades for " + filename1);
System.out.println(c1.toString());
System.out.println("----------------------- ");

System.out.println("Grades for " + filename2);
System.out.println(c2.toString());
System.out.println("----------------------- ");

if(c1.equals(c2))
System.out.println("The 2 grade lists are equal");
else
System.out.println("The 2 grade lists are not equal");

} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}


}
}


output

----
Enter input filename 1 containing grades: grades1.txt
Enter input filename 2 containing grades: grades2.txt
Grades for grades1.txt
Marks Grade
80 B
90 A
70 C
78 C
60 D
50 F
95 A

-----------------------


Grades for grades2.txt
Marks Grade
96 A
87 B
60 D
89 B
68 D
70 C
83 B

-----------------------


The 2 grade lists are not equal