write an application that creates a simple Grade class and then writes Grade Obj
ID: 3604226 • Letter: W
Question
write an application that creates a simple Grade class and then writes Grade Object data to a RandomAccessFile object.
Grade Class
Instance variables: studentID:byte, grade:double
Constructors - two constructor: one default and one constructor with new student ID and new grade as parameters.
Accessor and mutator methods Use Eclipse's functionality to create these method quickly.
toString() - "Student 4 grade is 88.0"
RandomAccessDemo Class
Creates at least 8 Grade objects and write them to a RandomAccessFile, no need to store the objects after they are written to the file. Hint use a loop to create and store the objects. Make sure a record size is determined for each grade.
Develop user interface that:
Locates a grade record based on placement in the file, for example the user might want to see the 5th grade, or the 3rd grade.
Prints a statement identifying the grade and its location such as "The fourth grade is for 85.4 and belongs to student 25".
Ask the user if they wish to look up another grade or quit.
The program must catch and successfully handle (continue with out exiting)
NumberFormatException or InputMismatchException depending how you handle collection of the user input.
Any required checked exception.
Explanation / Answer
import java.util.*;
public class GradeAverage {
public static Scanner kbd = new Scanner(System.in);
//Method that will calculate the sum
public static int sum( int[] gradeArray, int size){
int sum = 0;
int [] temp = gradeArray;//tem array is set equal to original array
for (int i = 0; i<gradeArray.length; i++){
sum +=temp[i];//array numbers are added to calculate sum
}
return sum;//calculated sum is returned
}
//method that will calculate the average by receiving array and size
//method will return average.
public static double average(int[] gradeArray, int size){
double average;
int[] temp = gradeArray;
int sum = 0;
for (int i = 0; i<gradeArray.length; i++){
sum +=temp[i];
}
//System.out.println("The sum is: " + sum);
average = (double)sum/(double)size;
return average;
}
//letterGrade method will determine what letter grade will be returned.
public static String letterGrade(double gradeAverage){
String lettrGrade = "";
int quotient = (int)gradeAverage/10; //quotient will deterimine LetterGrade
int remainder = (int)gradeAverage%10; // remainder will determine if a "+ or -"
switch(quotient){//switch statement for cases if grade is A, B, C, D, F
case 10://100% so just return A+;
return "A+";
case 9: //90-89 is a A
lettrGrade="A";
break;
case 8: //80-89 is a B
lettrGrade = "C";
break;
case 7: //70-79 is a C
lettrGrade="C";
break;
case 6: //60-69 is a D :
lettrGrade = "D";
break;
default:
return "F";
}
switch (remainder){ //remainder switch statement will be used to
//determine if a + or a - is added to the letter grade.
//these are the cases that will receive "+"
case 0: case 1: case 2: case 3: case 4:
lettrGrade+="-";
break;
case 7: case 8: case 9:
lettrGrade +="+";
}
return lettrGrade;//letter grade with final + or _
}
//DashLine method will print out a dash line for better readability.
public static void DashLine(){
for(int i = 0; i<20; i++){
System.out.print("-");
}
}
public static void main(String[] args) {
int[] grades;//array where grades will be stored
int numGrades;//will determine the size of the array.
double gradeAverage; //will be set equal to the average method.
int total;//will be set equal to the sum method.
String cont = "";//empty string
do{//loop will run as long as 'y' is entered
System.out.print("HOW MANY GRADES DO YOU WISH TO ENTER?: ");
numGrades = kbd.nextInt();
grades = new int[numGrades];
System.out.print(" PLEASE ENTER " + numGrades + " GRADES: ");
for(int i = 0; i<grades.length; i++){
grades[i]=kbd.nextInt();
if(grades[i]>100 || grades[i]<0){
System.out.println("GRADES CAN NOT EXCEED 100 or BE LESS THAN 1");
i--;
}
}
total = sum(grades, numGrades);//calls on sum method
gradeAverage = average(grades,numGrades);//calls on average method
DashLine();//prints dashlines
System.out.print(" The Total Sum is: " + total + " ");
System.out.printf("The Average is: %.2f ", gradeAverage);
String ltrGrade;
ltrGrade= letterGrade(gradeAverage);
if(!(ltrGrade.equalsIgnoreCase("F"))){//test if ltr grade is F,
//if its not F, print the following
System.out.println(" Your Final Letter Grade is a: "+ ltrGrade
+" YOU PASS!!");
}
else{//failing grade prints this.
System.out.println(" Your Final Letter Grade is a: "+ ltrGrade
+" YOUR PRETTY DUMB!!");
}
DashLine();//prints dash lines
System.out.println(" DO YOU WANT TO ENTER MORE GRADES? Y or N");
cont = kbd.next();
for(int j = 0; j< 80; j++){//simulates clear screen.
System.out.println();
}
}while(cont.equalsIgnoreCase("y"));
kbd.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.