Reputation: 135
I cannot figure out the issue with my program, any assistance is greatly appreciated! Unfortunately I'm a beginner programmer... When I run the program it asks for amount of classes, credits, and grades properly but it disregards the credits entered and just gives the letter grade's normal value. Also at the end it says "Your GPA is 0.0" when clearly that is incorrect. Thanks again!
public class QualityPoints
{
public static void main(String[] args)
{
// Needed variables
String grade;
int totalCredits = 0;
int totalCreditsEarned = 0;
int credits;
int classes;
double gpa;
double number=0;
String greeting = "This program will calculate your GPA.";
JOptionPane.showMessageDialog(null, greeting, "GPA Calculator", 1);
classes = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of classes you are taking"));
//Loop that ends once the student has put information in on all his classes
for(int count = 0; count < classes; count++)
{
credits = Integer.parseInt(JOptionPane.showInputDialog(null, "How many credit was this class?:"));
//reads the letter grade using the String Grade prompt
// gathers input from user and assigns a string into JOptionPane
grade = JOptionPane.showInputDialog(null, "Enter letter grade: ",
"Quality Points Converter", JOptionPane.INFORMATION_MESSAGE);
// calls separate method (computeQualityPoints) using parameter grade
if (!grade.equalsIgnoreCase("a") && !grade.equalsIgnoreCase("a-")
&& !grade.equalsIgnoreCase("b+") && !grade.equalsIgnoreCase("b")
&& !grade.equalsIgnoreCase("b-") && !grade.equalsIgnoreCase("c+")
&& !grade.equalsIgnoreCase("c") && !grade.equalsIgnoreCase("c-")
&& !grade.equalsIgnoreCase("d+") && !grade.equalsIgnoreCase("d")
&& !grade.equalsIgnoreCase("d-") && !grade.equalsIgnoreCase("f")) {
JOptionPane.showMessageDialog(null, "Invalid grade entered");
} else {
JOptionPane.showMessageDialog(null, "You received "
+ computeQualityPoints(grade) + " quality points");
computeQualityPoints(grade);
}
//algorithm for finding the GPA
totalCredits += credits;
totalCreditsEarned += (credits * number);
}
//for loop ends
//GPA is calculated for all the students classes
gpa = totalCreditsEarned / totalCredits;
JOptionPane.showMessageDialog(null, "Your GPA is: " + gpa);
}
/**
* Uses the letter grade given as the parameter to compute quality points
* received, thus displaying quality points as the output
*
* @param grade
* @return JOptionPane message box with the number of quality points, given
* a valid letter grade.
*/
public static double computeQualityPoints(String grade) {
/**
* If/else statments providing the message attached to the output
* corresponding to the grade
*/
if (grade.equalsIgnoreCase("a")) {
return 4.0;
}
if (grade.equalsIgnoreCase("a-")) {
return 3.7;
}
if (grade.equalsIgnoreCase("b+")) {
return 3.3;
}
if (grade.equalsIgnoreCase("b")) {
return 3.0;
}
if (grade.equalsIgnoreCase("b-")) {
return 2.7;
}
if (grade.equalsIgnoreCase("c+")) {
return 2.3;
}
if (grade.equalsIgnoreCase("c")) {
return 2.0;
}
if (grade.equalsIgnoreCase("c-")) {
return 1.7;
}
if (grade.equalsIgnoreCase("d+")) {
return 1.3;
}
if (grade.equalsIgnoreCase("d")) {
return 1.0;
}
if (grade.equalsIgnoreCase("d-")) {
return 0.7;
}
if (grade.equalsIgnoreCase("f")) {
return 0.0;
}
return 0.0;
}
}
Upvotes: 0
Views: 803
Reputation: 4377
As it is obvious you forgot to get result of computeQualityPoints
and store it in the number
variable. so after you showed the message to the user, you should change the line from:
JOptionPane.showMessageDialog(null, "You received "
+ computeQualityPoints(grade) + " quality points");
computeQualityPoints(grade);
to
JOptionPane.showMessageDialog(null, "You received "
+ computeQualityPoints(grade) + " quality points");
number = computeQualityPoints(grade);
But some other hints to improve the quality of you code:
You can ask the user to enter the N'
th class credits in the loop:
credits = Integer.parseInt(JOptionPane.showInputDialog(null, "How many credit was "+(count+1)+"'th class?"));
This gives a better experience to you user.
Also you can change the way of checking for the validity of entered grades which are static in you program, by storing them in a map structure such as a HashMap
:
public class Snippet {
static HashMap<String,Float> GRADES = new HashMap<String, Float>(12);
static{
GRADES.put("a", 4f);
GRADES.put("a-", 3.7f);
GRADES.put("b+", 3.3f);
GRADES.put("b", 3f);
GRADES.put("b-", 2.7f);
GRADES.put("c+", 2.3f);
GRADES.put("c", 2f);
GRADES.put("c-", 1.7f);
GRADES.put("d+", 1.3f);
GRADES.put("d", 1f);
GRADES.put("d-", 0.7f);
GRADES.put("f", 0f);
}
public static void main(String[] args) {
// Needed variables
// ... you codes went here
}
and then simply change the way of checking for invalid entered grade by the user to this:
if (!GRADES.containsKey(grade)) {
JOptionPane.showMessageDialog(null, "Invalid grade entered");
} else {
JOptionPane.showMessageDialog(null, "You received "
+ computeQualityPoints(grade) + " quality points");
number = computeQualityPoints(grade);
}
and it will help you with the calculating the number
due to 'grade' like this:
public static float computeQualityPoints(String grade) {
Float temp = GRADES.get(grade);
if(temp == null){
return 0;
}
return temp;
}
As you may noticed I change the type of output of the computeQualityPoints
from double
to float
since you don't need double for such small floating-point values.
If you don't want to use HashMap
change your if-else
control structures to switch-case
, because when you have this kind of checking switch-case
is preferred over if-else
.
Hope this would be helpful.
Upvotes: 0
Reputation: 393936
totalCreditsEarned += (credits * number);
number
remains 0.0
, which means totalCreditsEarned
and gpa
will also remain 0.0
.
I suspect that
computeQualityPoints(grade);
where you are ignoring the returned value, is supposed to be
number = computeQualityPoints(grade);
(at least I'm assuming that's what number
is supposed to contain)
Upvotes: 1