Intro to java programming: Using an array that holds a minimum of 10 grades, wri
ID: 3861571 • Letter: I
Question
Intro to java programming:
Using an array that holds a minimum of 10 grades, write a program that accepts letter grades (A-thru-F) until the array is full or the user enters an “escape value” (e.g., Q, X, etc.). The program will then calculate an overall GPA. The program will need to convert letter grades into “grade scores”.
‘A’ 4
‘B’ 3
‘C’ 2
‘D’ 1
‘F’ 0
If the entry is not a valid grade, the program should print an error message and the grade should not be included in the GPA (i.e., only use valid letter grades). Print out the letter grades, their associated “grade score”, and the GPA (to two decimal places).
Example:
A – 4
A – 4
B – 3
A – 4
C – 2
Your GPA, based on 5 letter grades, is 3.40
Implementation Constraints
1. You need to use a loop to accept the input.
2. You need to use a loop to calculate the GPA.
Extra Credit (5 pts) Write the program with a “driver” class, and a second class called “Grades” that implements the grade array, converts letter grades to “grade scores”, and generates the GPA.
Explanation / Answer
Grade.java
public class Grade {
char arr[];
Grade(int n)
{
arr=new char[n];
}
boolean isValidGrade(char c)
{
if(c=='A'||c=='B'||c=='C'||c=='D'||c=='F')
return true;
return false;
}
int getGradeScore(char c)
{
if(c=='A')
return 4;
else if(c=='B')
return 3;
else if(c=='C')
return 2;
else if(c=='D')
return 1;
else if(c=='F')
return 0;
else
return -1;
}
double GenGPA(int n)
{
double gpa=0;
for (int i = 0; i < n; i++)
{
gpa+=getGradeScore(arr[i]);
}
return gpa/n;
}
}
====================================================================
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag=true;
Grade gr=new Grade(10);
int i=0;
while(flag)
{
System.out.println("Enter Grades (A,B,C,D,F) and Q to Quit");
Scanner sc=new Scanner(System.in);
String ch=sc.next();
char c=ch.charAt(0);
if(c=='Q')
{
flag=false;
}
else
{
if(gr.isValidGrade(c))
{
gr.arr[i++]=c;
}
else
{
System.out.println("Invalid grade");
}
}
}
for (int j = 0; j < i; j++)
{
System.out.println(gr.arr[j]+"-"+gr.getGradeScore(gr.arr[j ]));
}
System.out.println("Your GPA based on 5 lettter grade is "+gr.GenGPA(i));
}
}
======================================================================
Output:
Enter Grades (A,B,C,D,F) and Q to Quit
A
Enter Grades (A,B,C,D,F) and Q to Quit
A
Enter Grades (A,B,C,D,F) and Q to Quit
B
Enter Grades (A,B,C,D,F) and Q to Quit
A
Enter Grades (A,B,C,D,F) and Q to Quit
C
Enter Grades (A,B,C,D,F) and Q to Quit
Q
A-4
A-4
B-3
A-4
C-2
Your GPA based on 5 lettter grade is 3.4
======================================================================
Let me knoe if you have any problem
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.