Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-24-2006, 03:42 PM   #1 (permalink)
mziel
Registered User
 
Join Date: Sep 2006
Posts: 2
mziel is on a distinguished road
Exclamation new to java.. code question

hey guys.. i'm new to java (college course).. and i'm currently writing a grading program for a class.
basically all you do is input 3 quiz grades, two midterm grades, and one final grade. It's weighted.. 25% for quizzes, 35% for midterms, and 40% for the final.
i have the code written out completely but it's coming up with some errors. if i input all perfect scores (three 10's, two 100's, and one 100), the result is "1.0, A". Fair enough.. but if I put in anything else, it comes up with something like ".54, Fail", or another number (depending on what i put in). two questions... how might I convert the decimals (1.0 for example) into the actually percentage? (i.e. 1.0 would be 100)? Also, how might I actually get it to work correctly??
any tips?? i'm not looking for someone to write it for me correctly... just some guidance..
Here's my code:
Code:
import java.util.Scanner;

public class Proj1 
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
	
	System.out.println("Enter three quiz grades: ");
	int quizGrade1 = keyboard.nextInt();
	int quizGrade2 = keyboard.nextInt();
	int quizGrade3 = keyboard.nextInt();
	
	double totalQuizGrade = (quizGrade1 + quizGrade2 + quizGrade3);
	totalQuizGrade = totalQuizGrade / 30;
	totalQuizGrade = totalQuizGrade * .25;
	
	System.out.println("Enter two midterm grades: ");
	int midtermGrade1 = keyboard.nextInt();
	int midtermGrade2 = keyboard.nextInt();
	
	double totalMidtermGrade = (midtermGrade1 + midtermGrade2);
	totalMidtermGrade = totalMidtermGrade / 200;
	totalMidtermGrade = totalMidtermGrade * .35;
	
	System.out.println("Enter final exam grade: ");
	int finalExam = keyboard.nextInt();
	double totalFinalCalc = finalExam / 100;
	totalFinalCalc = totalFinalCalc * .40;
	
	double totalGrade = (totalQuizGrade + totalMidtermGrade + totalFinalCalc);
	int totalGrade1 = (int)totalGrade / 10;
	switch(totalGrade1)
	{
	case 10: case 9:
		System.out.println(totalGrade + ", A");
		break;
	case 8:
		System.out.println(totalGrade + ", B");
		break;
	case 7:
		System.out.println(totalGrade + ", C");
		break;
	case 6:
		System.out.println(totalGrade + ", C");
		break;
		default:
			System.out.println(totalGrade + ", Fail");
		break;
		
	}
	}
}



_____________thanks for any help!!
mziel

Last edited by Belisarius; 09-24-2006 at 04:29 PM. Reason: Inserted code tags
mziel is offline   Reply With Quote
Old 09-24-2006, 04:39 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Well, as far as converting decimal to a percentage, simply multiply by 100. After all, .54 is 54 percent of 1, whereas 1 would be 100 percent of 1. It would still be a "decimal", in that you would end up with a number like "100.0", but I assume that's acceptable.

Also, part of your problem very well might be that you're 'casting' between doubles and integers. Being that you're new to Java, you might not know what a 'cast' is. Here is an example:
Code:
double foo = 5.75;
int bar = (int)foo;
Now, the problem with this is that I'm attempting to 'cast', or change the 'type' from a 'double' (or as you would understand it, decimal) to an 'int' (which is short for 'integer'). An integer is a whole number, that is 1, 2, 3, 4, 5, etc but *not* 1.1, 2.4, 5,1. By 'casting' from a 'double' to an 'int', I loose the decimal portion. No rounding takes place, the end is just dropped. So 5.75 becomes simply 5.

I think you can see how that could screw up your math. Be sure to keep everything the same. I'd suggest that you use a System.out.println() to see if 'totalGrade1' is what you expect it to be.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 09-24-2006, 05:19 PM   #3 (permalink)
mziel
Registered User
 
Join Date: Sep 2006
Posts: 2
mziel is on a distinguished road
Exclamation

Hey Belisarius, thanks so much for your reply! I do have some background in programming (C++ class last semester).. so I got what you were saying haha. Although.. I did get things to work.. somewhat. Here is the input/output(I'm using eclipse):

Enter three quiz grades:
10 9 8
Enter two midterm grades:
100 70
Enter final exam grade:
100

0.9225, Fail

How would I get the output to be simply 92? and how would i be able to get it to be read into the switch statement as 10(for 100 percent) or 9, 8, etc.? I'm using switch instead of an if elseif because my prof. said the switch is better practice.. but I can't figure out how to compare the numbers to get the grades without being redundant, etc. Anything?
mziel is offline   Reply With Quote
Old 09-24-2006, 05:30 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
In this case, your professor is right about best practice and switches.

The simplest solution, since the way you store the grade is between 0 and 1, would be to simply multiply it by 10 before it goes into the switch statement. Then convert it to an 'int', which will eliminate any decimals.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 09-25-2006, 09:43 AM   #5 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Quote:
Originally Posted by Belisarius
In this case, your professor is right about best practice and switches.
Is it best practice because it looks nicer?
__________________
toe_cutter is offline   Reply With Quote
Old 09-25-2006, 11:12 AM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Well, presentation is certainly a factor - you want the code to flow naturally and when looking at a series of conditions, a switch statement is easier to understand. But it also is a performance gain, as the compiler can optimize a switch in a different manner than an "if" statement. When one has a number of integers for which a different action must be taken, you will generally see a performance gain by using a switch as opposed to an "if" statement. Generally the performance gain is minimal, as the majority of the time you'd use it the scope versus processor power makes the computation time almost irrelevant. But, if you have to do it an obscene number of times, it's probably better.

So in summary, yes there is a technical reason to do it, but the layout/clear flow of logic is the main reason. In more complex designs, frequently it's advantagous to replace a switch with a polymorphic design, but when simply comparing a set list a switch is the best way to go.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 09-26-2006, 12:29 PM   #7 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Quote:
Originally Posted by Belisarius
Well, presentation is certainly a factor - you want the code to flow naturally and when looking at a series of conditions, a switch statement is easier to understand. But it also is a performance gain, as the compiler can optimize a switch in a different manner than an "if" statement. When one has a number of integers for which a different action must be taken, you will generally see a performance gain by using a switch as opposed to an "if" statement. Generally the performance gain is minimal, as the majority of the time you'd use it the scope versus processor power makes the computation time almost irrelevant. But, if you have to do it an obscene number of times, it's probably better.

So in summary, yes there is a technical reason to do it, but the layout/clear flow of logic is the main reason. In more complex designs, frequently it's advantagous to replace a switch with a polymorphic design, but when simply comparing a set list a switch is the best way to go.
Ya, I asked because I am a C guy and I believe (not possitive) in C when you compile a switch statement and an if statement that do the same thing they will look identical in assembly. So the only benefit in C is that I think switch statements look nicer.

Thanks for the info.
__________________
toe_cutter is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question - icon code Blucutie All Other Coding Languages 2 05-31-2006 01:32 PM
need c++ code for streamtokenizer function of java muralikrishna Standard C, C++ 2 11-06-2005 06:00 PM
Java Resources Belisarius Java 0 03-28-2005 02:03 PM
n00b question re: java sde Java 5 08-04-2003 03:26 AM


All times are GMT -8. The time now is 07:04 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting