frijolie
06-06-2005, 03:40 AM
First off I'm not only a new Java programmer, but I'm new to programming in general. I'm all self-taught and all my knowledge is only from books.
Here's what I'm trying to accomplish:
I'm trying to create a Java program that will display your G.P.A after accepting the necessary input from the user. I want to learn how to do this myself and have found some speed bumps along the way. I don't know if my logic is bad here or if it's just a syntax error. I'm hoping someone can help fix my syntax or logic in this program. Here's the pseudocode for my program:
Start
Initialize Variables
Input creditHours
totalCreditHours = creditHours
Input letterGrade
calculateGradePoints()
totalGradePoints = gradePoints
while (moreClasses == "yes")
Input creditHours
totalCreditHours+=creditHours
Input letterGrade
calculateGradePoints()
totalGradePoints+=gradePoints
gradePointAverage=totalGradePoints/totalCreditHours
displayResults()
Exit
I know this program is very "procedural" in nature and I'm not familiar with OOP yet. Although, I am very interested in learing more about OOP, anyone know of a good book on OOP? Since I'm dealin with Java here I'm sure it will be a necessity to learn. Anyways, here's the code that I've got so far and the error I'm receiving in the compiler:
Error:
" calculateGradePoints(java.lang.String,double) in GPACalc cannot
be applied to (java.lang.String)
calculateGradePoints(letterGrade);
^
1 error "
My Code (so far):
-----------------------------------------------------------------------
import javax.swing.JOptionPane;
class GPACalc {
public static void main(String args[]) {
double creditHours = 0.0;
double totalCreditHours = 0.0;
double gradePoints = 0.0;
double totalGradePoints = 0.0;
double gradePointAverage = 0.0;
String moreClasses = "";
String letterGrade = "";
String response = "";
response = JOptionPane.showInputDialog
("Please enter your CREDIT HOURS");
validateResponse(response);
creditHours = Integer.parseInt(response);
totalCreditHours = creditHours;
letterGrade = JOptionPane.showInputDialog
("Please enter your LETTER grade");
letterGrade = letterGrade.toUpperCase();
calculateGradePoints(letterGrade);
JOptionPane.showMessageDialog
(null, "creditHours = "+creditHours+"\n" +
"totalCreditHours = "+totalCreditHours+"\n"+
"letterGrade = "+letterGrade+"\n" +
"gradePoints = "+gradePoints);
System.exit(0);
} // end of main() method
public static void validateResponse(String response) {
if (response == null) {
JOptionPane.showMessageDialog
(null, "D\'oh! You clicked on the Cancel button");
System.exit(0);
}
else
if (response.equals("")) {
JOptionPane.showMessageDialog
(null, "D\'oh! You forgot to input your credit hours");
System.exit(0);
}
} // end of validateResponse() method
public static double calculateGradePoints
(String letterGrade, double gradePoints) {
if (letterGrade.equals("A"))
gradePoints+= 4.0;
else
if (letterGrade.equals("A-"))
gradePoints+= 3.7;
else
if (letterGrade.equals("B+"))
gradePoints+= 3.3;
else
if (letterGrade.equals("B"))
gradePoints+= 3.0;
else
if (letterGrade.equals("B-"))
gradePoints+= 2.7;
else
if (letterGrade.equals("C+"))
gradePoints+= 2.3;
else
if (letterGrade.equals("C"))
gradePoints+= 2.0;
else
if (letterGrade.equals("C-"))
gradePoints+= 1.7;
else
if (letterGrade.equals("D+"))
gradePoints+= 1.3;
else
if (letterGrade.equals("D"))
gradePoints+= 1.0;
else
if (letterGrade.equals("D-"))
gradePoints+= 0.7;
else
if (letterGrade.equals("E"))
gradePoints+= 0.0;
else
if (letterGrade.equals("F"))
gradePoints+= 0.0;
return gradePoints;
} // end of calculateGradePoints() method
} // end of GPACalc class
-------------------------------------------------------------------------
What I've got is extremely simple and is not complete by far.
The current final output is there only to test the values of the variables. Later on I will change this to display the totalCreditHours, totalGradePoints, and the gradePointAverage.
Isn't there an easier way to assign the variable "gradePoints" a value based on "letterGrade's" input instead of multiple if/else statements? Like say, switch statements? Isn't a switch structure only for primitive data types?
Where do I go from here?
Any help/advice you can offer is and will be greatly appreciated!
Thanks in advance,
frijolie
Here's what I'm trying to accomplish:
I'm trying to create a Java program that will display your G.P.A after accepting the necessary input from the user. I want to learn how to do this myself and have found some speed bumps along the way. I don't know if my logic is bad here or if it's just a syntax error. I'm hoping someone can help fix my syntax or logic in this program. Here's the pseudocode for my program:
Start
Initialize Variables
Input creditHours
totalCreditHours = creditHours
Input letterGrade
calculateGradePoints()
totalGradePoints = gradePoints
while (moreClasses == "yes")
Input creditHours
totalCreditHours+=creditHours
Input letterGrade
calculateGradePoints()
totalGradePoints+=gradePoints
gradePointAverage=totalGradePoints/totalCreditHours
displayResults()
Exit
I know this program is very "procedural" in nature and I'm not familiar with OOP yet. Although, I am very interested in learing more about OOP, anyone know of a good book on OOP? Since I'm dealin with Java here I'm sure it will be a necessity to learn. Anyways, here's the code that I've got so far and the error I'm receiving in the compiler:
Error:
" calculateGradePoints(java.lang.String,double) in GPACalc cannot
be applied to (java.lang.String)
calculateGradePoints(letterGrade);
^
1 error "
My Code (so far):
-----------------------------------------------------------------------
import javax.swing.JOptionPane;
class GPACalc {
public static void main(String args[]) {
double creditHours = 0.0;
double totalCreditHours = 0.0;
double gradePoints = 0.0;
double totalGradePoints = 0.0;
double gradePointAverage = 0.0;
String moreClasses = "";
String letterGrade = "";
String response = "";
response = JOptionPane.showInputDialog
("Please enter your CREDIT HOURS");
validateResponse(response);
creditHours = Integer.parseInt(response);
totalCreditHours = creditHours;
letterGrade = JOptionPane.showInputDialog
("Please enter your LETTER grade");
letterGrade = letterGrade.toUpperCase();
calculateGradePoints(letterGrade);
JOptionPane.showMessageDialog
(null, "creditHours = "+creditHours+"\n" +
"totalCreditHours = "+totalCreditHours+"\n"+
"letterGrade = "+letterGrade+"\n" +
"gradePoints = "+gradePoints);
System.exit(0);
} // end of main() method
public static void validateResponse(String response) {
if (response == null) {
JOptionPane.showMessageDialog
(null, "D\'oh! You clicked on the Cancel button");
System.exit(0);
}
else
if (response.equals("")) {
JOptionPane.showMessageDialog
(null, "D\'oh! You forgot to input your credit hours");
System.exit(0);
}
} // end of validateResponse() method
public static double calculateGradePoints
(String letterGrade, double gradePoints) {
if (letterGrade.equals("A"))
gradePoints+= 4.0;
else
if (letterGrade.equals("A-"))
gradePoints+= 3.7;
else
if (letterGrade.equals("B+"))
gradePoints+= 3.3;
else
if (letterGrade.equals("B"))
gradePoints+= 3.0;
else
if (letterGrade.equals("B-"))
gradePoints+= 2.7;
else
if (letterGrade.equals("C+"))
gradePoints+= 2.3;
else
if (letterGrade.equals("C"))
gradePoints+= 2.0;
else
if (letterGrade.equals("C-"))
gradePoints+= 1.7;
else
if (letterGrade.equals("D+"))
gradePoints+= 1.3;
else
if (letterGrade.equals("D"))
gradePoints+= 1.0;
else
if (letterGrade.equals("D-"))
gradePoints+= 0.7;
else
if (letterGrade.equals("E"))
gradePoints+= 0.0;
else
if (letterGrade.equals("F"))
gradePoints+= 0.0;
return gradePoints;
} // end of calculateGradePoints() method
} // end of GPACalc class
-------------------------------------------------------------------------
What I've got is extremely simple and is not complete by far.
The current final output is there only to test the values of the variables. Later on I will change this to display the totalCreditHours, totalGradePoints, and the gradePointAverage.
Isn't there an easier way to assign the variable "gradePoints" a value based on "letterGrade's" input instead of multiple if/else statements? Like say, switch statements? Isn't a switch structure only for primitive data types?
Where do I go from here?
Any help/advice you can offer is and will be greatly appreciated!
Thanks in advance,
frijolie