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);
}
}