Click to See Complete Forum and Search --> : Java logic/syntax error


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

buntine
06-06-2005, 04:19 AM
In referance to the error message, you are receiving because you are not passing the correct parameters to the colculateGradePoints(...) method. It takes two paremeters -- an int and a double, respectively. But you are only passing it an int (without the double).

It looks like a bit of a syntax error in that you have placed the variable declaration in the method definition rather than with in the method itself. It should look for more this:

public static double calculateGradePoints
(String letterGrade) {

double _gradePoints;

if (letterGrade.equals("A"))
_gradePoints+= 4.0;
else
if (letterGrade.equals("A-"))
_gradePoints+= 3.7;

...

return _gradePoints;

} // end of calculateGradePoints() method

When calling the method, you would assign (or add) its return value to the gradePoints variable.

gradePoints += calculateGradePoints(letterGrade);

Regards.

buntine
06-06-2005, 04:25 AM
Regarding your other questions:

1) Any credible Java Beginner's book will teach you the basic concepts of Object Oriented programming. O'Reilly have a good series of Java books.

2) A better way to handle the grading would be via an name/value collection object such as a HashMap or Vector.

Regards.

Khalid Ali
06-06-2005, 08:08 AM
........
Error:

" calculateGradePoints(java.lang.String,double) in GPACalc cannot
be applied to (java.lang.String)
calculateGradePoints(letterGrade);
^
1 error "
...............frijolie

your problem is obvious....the method above is expecting a string where as you have provided it a wrong type of variable.(double to be clear)

frijolie
06-06-2005, 12:11 PM
I think I figured it out, here's what I did to fix it. It's a working program although not entirely complete:


import javax.swing.JOptionPane;

class GPACalc {

static double creditHours = 0.0;
static double totalCreditHours = 0.0;
static double gradeScore = 0.0;
static double gradePoints = 0.0;
static double totalGradePoints = 0.0;
static double gradePointAverage = 0.0;
static String moreClasses = "";
static String letterGrade = "";
static String response = "";

public static void main(String args[]) {

displayWelcomeMessage();

response = JOptionPane.showInputDialog
("How many CREDIT HOURS was the class?");

validateResponse(response);

creditHours = Integer.parseInt(response);

totalCreditHours = creditHours;

letterGrade = JOptionPane.showInputDialog
("What was your LETTER grade for that class?");

letterGrade = letterGrade.toUpperCase();

calculateGradeScore(letterGrade);

gradePoints = creditHours * gradeScore;

totalGradePoints = gradePoints;

moreClasses = JOptionPane.showInputDialog
("Do you have more classes to input?");

moreClasses = moreClasses.toUpperCase();

validateMoreClasses(moreClasses);


while (moreClasses.equals("YES")) {

response = JOptionPane.showInputDialog
("How many CREDIT HOURS was the class");

validateResponse(response);

creditHours = Integer.parseInt(response);

totalCreditHours+=creditHours;

letterGrade = JOptionPane.showInputDialog
("What was your LETTER grade for that class?");

letterGrade = letterGrade.toUpperCase();

calculateGradeScore(letterGrade);

gradePoints = creditHours * gradeScore;

totalGradePoints+=gradePoints;

moreClasses = JOptionPane.showInputDialog
("Do you have more classes to input?");

moreClasses = moreClasses.toUpperCase();

validateMoreClasses(moreClasses);

} // end of while loop

gradePointAverage = totalGradePoints / totalCreditHours;

displayResults();

System.exit(0);

} // end of main() method


public static void displayWelcomeMessage() {
JOptionPane.showMessageDialog
(null, "**** Grade Point Average Calculation Program ****"+"\n\n" +

"Welcome to this program! It will calculate your G.P.A"+"\n" +
"based on the total credit hours and grades you input."+"\n\n" +

"Type in the information for each class individually!"+"\n\n" +

"The program prompts you for your credit hours first," + "\n" +
"and then for your grades to be entered. Once again, please" + "\n" +
"input information seperately--do not total amounts!" + "\n\n" +

"Thank you for using the Grade Point Average Calculator!");

} // end of displayWelcomeMessage() 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 void validateMoreClasses(String moreClasses) {
if (moreClasses == null) {
JOptionPane.showMessageDialog
(null, "D\'oh! You clicked on the Cancel button");
System.exit(0);
}
else
if (moreClasses.equals("")) {
JOptionPane.showMessageDialog
(null, "D\'oh! You didn\'t type in \"Yes\" or \"No\"!");
System.exit(0);
}

} // end of validateMoreClasses() method


public static double calculateGradeScore(String letterGrade) {

if (letterGrade.equals("A"))
gradeScore = 4.0;
else
if (letterGrade.equals("A-"))
gradeScore = 3.7;
else
if (letterGrade.equals("B+"))
gradeScore = 3.3;
else
if (letterGrade.equals("B"))
gradeScore = 3.0;
else
if (letterGrade.equals("B-"))
gradeScore = 2.7;
else
if (letterGrade.equals("C+"))
gradeScore = 2.3;
else
if (letterGrade.equals("C"))
gradeScore = 2.0;
else
if (letterGrade.equals("C-"))
gradeScore = 1.7;
else
if (letterGrade.equals("D+"))
gradeScore = 1.3;
else
if (letterGrade.equals("D"))
gradeScore = 1.0;
else
if (letterGrade.equals("D-"))
gradeScore = 0.7;
else
if (letterGrade.equals("E"))
gradeScore = 0.0;
else
if (letterGrade.equals("F"))
gradeScore = 0.0;

return gradeScore;

} // end of calculateGradeScore() method


public static void displayResults() {
JOptionPane.showMessageDialog
(null, "totalCreditHours = "+totalCreditHours+"\n"+
"totalGradePoints = "+totalGradePoints+"\n\n"+

"Your G.P.A is : "+gradePointAverage);
} // end of displayResults() method

} // end of GPACalc class


Have I taken one step forward but two steps back? Did I just assign all the varaibles as global?

The next thing I'd like to do is to format the G.P.A to round to two decimal points. How do you do that? Don't you have to convert it to string?

Thanks for your help/suggestions!

P.S. How you you get those scrolling textareas around your replies (like the code) in these posts?

Khalid Ali
06-06-2005, 12:18 PM
enclose all of your code between the code tags or php tags...
global variable declaration is not a commendable approach.