Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Oct 3rd, 2006, 1:04 PM   #1
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
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);

}
}
Mixmaster is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 11:58 AM
simple java applet problem anant_tickoo Java 2 Aug 9th, 2006 5:13 AM
Java for Phones - simple enough? Oddball Java 10 Mar 10th, 2006 3:37 PM
Simple, yet annoying problem with Java Beans rross46 Java 4 Nov 7th, 2005 9:16 AM
Java script problem zeotrex JavaScript and Client-Side Browser Scripting 5 Sep 2nd, 2005 6:30 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:35 PM.

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