Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Simple java problem (http://www.programmingforums.org/showthread.php?t=11468)

Mixmaster Oct 3rd, 2006 1:04 PM

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

}
}


jaeusm Oct 3rd, 2006 2:58 PM

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.

titaniumdecoy Oct 3rd, 2006 3:22 PM

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;
The code below will not compile.

:

if (response = null) {
To test whether response is null, replace = with ==.

Double-quoted Strings cannot span multiple lines. So

:

"Enter student type

1= English, 2= Math, 3 = Science"

Will not work.

Due to a typo, you never declare the finalExamGrade variable before you attempt to set it. Correct as follows:

:

int finalExamGrade = 0;
Further, in the code snippet below, none of the variables in caps exist. You need to declare a new set of grade variables with the prefix ENGLISH_.

:

finalNumericGrade = midterm * ENGLISH_MIDTERM_PERCENTAGE +
      finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE+
      research * ENGLISH_RESEARCH_PERCENTAGE +
      presentation * ENGLISH_PRESENTATION_PERCENTAGE ;

The last error you need to correct is your last attempt to receive input from the user. There are a number of problems with this code:

:

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

I moved the "\n"s to the beginning of each line; that's just my style.

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.

Mixmaster Oct 3rd, 2006 3:57 PM

Thank's guys this helped a lot.


All times are GMT -5. The time now is 1:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC