![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2006
Posts: 8
Rep Power: 0
![]() |
Simple java problem
Here is a simple program I made in Java that accepts an input for student type then calculates and outputs the student's grades. In my eyes the code should be working, though I'm a beginner in Java the thing looks to me to make perfect logical sense. Being such, it's kind of seriously pissing me off that it wont work. This code is amazingly ugly to me, but I'm still learning java from a book so I'm not sure how to compact this beast. Any feedback on this code will be helpful.
class Grades2 {
public static void main (String args[]) {
final double MIDTERM_PERCENTAGE = .25;
final double FINALEXAM_PERCENTAGE = .25 ;
final double RESEARCH_PERCENTAGE = .30 ;
final double PRESENTATION_PERCENTAGE = .20 ;
final double MATH_MIDTERM_PERCENTAGE= .50;
final double MATH_FINALEXAM_PERCENTAGE =.50;
final double SCIENCE_MIDTERM_PERCENTAGE = .40 ;
final double SCIENCE_FINALEXAM_PERCENTAGE = .40;
final double SCIENCE_RESEARCH_PERCENTAGE = .20;
int midterm = 0;
int finaExamGrade= 0;
int research = 0;
int presentation = 0;
double finalNumericGrade = 0;
String finalLetterGrade= "";
String response ;
// What type of student are we calculating?
response = JOptionPane.showInputDialog ("Enter student type
1= English, 2= Math, 3 = Science") ;
if (response = null) {
System.exit (0);
}
while (response.equals("")) {
JOptionPane.showMessageDialog( null, "You must enter a
number");
response = JOptionPane.showInputDialog("Enter student type
1= English, 2= Math, 3= Science");
}
while (Integer.parseInt(response) < 1 | Integer.parseInt(
response) > 3)
{
JOptionPane.showMessageDialog(null, response + " - is not a
valid student type") ;
response = JOptionPane.showInputDialog("Enter student type
1= English, 2= Math, 3= Science") ;
}
//Student type is valid, now let's calculate the grade
switch(Integer.parseInt(response)) {
// Case 1 is an English student
case 1 :
midterm = Integer.parseInt(JOptionPane.showInputDialog ("
Enter the Midterm Grade"));
finalExamGrade = Integer.parseInt(JOptionPane.
showInputDialog ("Enter the Final Exam Grade"));
research = Integer.parseInt(JOptionPane.showInputDialog("
Enter the research grade"));
presentation= Integer.parseInt(JOptionPane.showInputDialog
("Enter the presentation grade"));
finalNumericGrade=
(midterm * ENGLISH_MIDTERM_PERCENTAGE) +
(finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE)+
(research * ENGLISH_RESEARCH_PERCENTAGE) +
(presentation * ENGLISH_PRESENTATION_PERCENTAGE) ;
if (finalNumericGrade >= 93)
finalLetterGrade= "A" ;
else
if((finalNumericGrade >= 85) & (finalNumericGrade <
93))
finalLetterGrade = "B" ;
else
if ((finalNumericGrade >= 78) & (finalNumericGrade <
85))
finalLetterGrade = "C" ;
else
if ((finalNumericGrade >= 70) & (finalNumericGrade <
78))
finalLetterGrade = "D" ;
else
finalLetterGrade= "F" ;
JOptionPane.showMessageDialog(null, "***ENGLUSH STUD3NT***
\n\n" +
"Midterm grade is: " + midterm + "\n" +
"Final exam grade is: " + finalExamGrade + "\n" +
"Research grade is: " + research + "\n" +
"Presentation grade is: " + presentation + "\n\n" +
"Final Numeric Grade is: " + finalNumericGrade + "
\n" +
"Final Letter Grade is: " + finalLetterGrade) ;
break;
//Case 2 is a math student
case 2:
midterm = Integer.parseInt(JOptionPane.showInputDialog("
Enter teh midterm grade" ));
finalExamGrade= Integer.parseInt(JOptionPane.
showInputDialog("Enteer teh final xam grade"));
finalNumericGrade=
(midterm * MATH_MIDTERM_PERCENTAGE) +
(finalExamGrade * MATH_FINALEXAM_PERCENTAGE) ;
if (finalNumericGrade >= 90)
finalLetterGrade= "A" ;
else
if ((finalNumericGrade >= 83) & (finalNumericGrade > 90))
finalLetterGrade = "B" ;
else
if (( finalNumericGrade >= 76) & (finalNumericGrade <
83))
finalLetterGrade= "C" ;
else
if (( finalNumericGrade >= 65) & (finalNumericGrade < 76))
finalLetterGrade = "D" ;
else
if (finalNumericGrade < 65)
finalLetterGrade = "F" ;
JOptionPane.showMessageDialog (null, "*** MATH STUDENT ***
\n\n" +
"midterm grade is : " + midterm + "\n" +
"Final exam grade is : " + finalExamGrade + "\n" +
"Final numeric grade is : " + finalNumericGrade + "
\n" +
"Final letter grade is : " + finalLetterGrade) ;
break;
case 3:
midterm = Integer.parseInt(JOptionPane.
showInputDialog
("Enter the midterm grade"));
finalExamGrade = Integer.parseInt(JOptionPane.
showInputDialog
("Enter the final exam grade")) ;
research = Integer.parseInt(JOptionPane.
showInputDialog("Enter the research grade")) ;
finalNumericGrade = JOptionsPane.showMessageDialog
(null, "*** SCIENCE STUDENT *** \n\n " +
"midterm grade is : " + midterm + "\n" +
"final exam grade is :" + finalExamGrade + "\n" +
"research grade is :" + research + "\n\n" +
"final numeric grade is : " + finalNumericGrade +
"\n" +
"final letter grade is : " + finalLetterGrade );
break;
default :
JOptionPane.showMessageDialog(null, response + " -
is not a valid response");
System.exit (0);
}
System.exit (0);
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
A few things I noticed:
1) You have no import statements 2) You are using a bitwise 'and' in your selection statements. Change '&' to '&&'. 3) Is your code actually spaced as shown above? If so, there will be several syntax errors. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I'm impressed that you were able to code for so long without testing your program. I only had to make a handful of minor changes to get it working.
As jaeusm mentioned, you need an import statement. import javax.swing.JOptionPane; if (response = null) {Double-quoted Strings cannot span multiple lines. So "Enter student type 1= English, 2= Math, 3 = Science" Due to a typo, you never declare the finalExamGrade variable before you attempt to set it. Correct as follows: int finalExamGrade = 0; finalNumericGrade = midterm * ENGLISH_MIDTERM_PERCENTAGE +
finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE+
research * ENGLISH_RESEARCH_PERCENTAGE +
presentation * ENGLISH_PRESENTATION_PERCENTAGE ;finalNumericGrade = JOptionsPane.showMessageDialog(null, "*** SCIENCE STUDENT *** \n\n " + "midterm grade is : " + midterm + "\n" + "final exam grade is :" + finalExamGrade + "\n" + "research grade is :" + research + "\n\n" + "final numeric grade is : " + finalNumericGrade + "\n" + "final letter grade is : " + finalLetterGrade ); First off, the class you are attempting to use is JOptionPane, not JOptionsPane. Second, since you are trying to receive input, you should be calling JOptionPane.showInputPane; you no longer need to pass in the first parameter (null). In addition, since this method returns a String, you need to use the Integer.parseInt method to convert the input to the proper data type. finalNumericGrade = Integer.parseInt(JOptionPane.showInputDialog(
"*** SCIENCE STUDENT ***" +
"\n\nmidterm grade is : " + midterm +
"\nfinal exam grade is :" + finalExamGrade +
"\nresearch grade is :" + research +
"\n\nfinal numeric grade is : " + finalNumericGrade +
"\n\nfinal letter grade is : " + finalLetterGrade ));As jaesum also mentioned, & does not do what you think it does. Use &&. One last thing... you seem confused about which type of student you are dealing with (English? Science?). You could use objects to improve the clarity of your code; for example, one approach would be to have EnglishStudent and ScienceStudent extend Student and have each calculate their own grades. That should sove most of your problems. Some style tips: You use braces and curly braces much too liberally. You can get away with using much fewer. Also, factor your code. If you have to type the same few lines more than once, you should be doing something differently. Hint: Methods are your friends. Other than that, you should be OK. Last edited by titaniumdecoy; Oct 3rd, 2006 at 2:43 PM. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2006
Posts: 8
Rep Power: 0
![]() |
Thank's guys this helped a lot.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Programming with Java: Tutorial | ReggaetonKing | Java | 7 | May 20th, 2008 10:58 AM |
| simple java applet problem | anant_tickoo | Java | 2 | Aug 9th, 2006 4:13 AM |
| Java for Phones - simple enough? | Oddball | Java | 10 | Mar 10th, 2006 2:37 PM |
| Simple, yet annoying problem with Java Beans | rross46 | Java | 4 | Nov 7th, 2005 8:16 AM |
| Java script problem | zeotrex | JavaScript and Client-Side Browser Scripting | 5 | Sep 2nd, 2005 5:30 AM |