regent help in logic error (code included)

M

me

I am getting the vlaue '0.0' for Percent of Grade A, Garde B, etc.
Everythign else, include total cound for each Grade, average, lowest
score, higest score are working. Can somebody please look at the code
and help me find the logic error?



import java.lang.String;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;


public class test
{
private int countScore, lowestScore, highestScore, gradeA, gradeB,
gradeC, gradeD, gradeF;
private double percentA, percentB, percentC, percentD, percentF;
private double averageScore;
private final double HUNDRED_PERCENT = 100.00;

public static void main( String[] Args)
{
test MyObject = new test();
MyObject.operation();
System.exit(0);
}


private void operation()
{
int score=0, lowestOldScore=0, highestOldScore =0, oldScore=0,
totalScore=0;

String scoreStr="";

scoreStr = JOptionPane.showInputDialog("Enter a score in the range
of 0-100; enter a negative integer to quit..");
score = Integer.parseInt(scoreStr);
lowestScore = score;
highestScore = score;

while ( score>=0 )
{
if (score <= lowestOldScore)
lowestScore = score;

if (score >= highestOldScore)
highestScore = score;

System.out.print(new Integer(lowestScore).toString());
System.out.print("; ");
System.out.println(new Integer(highestScore).toString());

if ( (score >=90) && (score <=100) )
gradeA++;

else if ( (score >=80) && (score <=89) )
gradeB++;

else if ( (score >=70) && (score <=79) )
gradeC++;

else if ( (score >=60) && (score <=69) )
gradeD++;

// if I just use "else", it doesn't count for input '0' as a score
for grade F
else if ( (score >=0) && (score <=59) )
gradeF++;

countScore++;
lowestOldScore = lowestScore;
highestOldScore = highestScore;

totalScore += score;
averageScore = (double)(totalScore)/countScore;

scoreStr = JOptionPane.showInputDialog("Enter a score in the range
of 0-100; enter a negative integer to quit..");
score = Integer.parseInt(scoreStr);

} // end while loop

System.out.println();
System.out.println(" Total Number of scores: " + new
Integer(countScore).toString());
System.out.println();
percentA = (gradeA/countScore) * HUNDRED_PERCENT;
System.out.println("Percent of Grade A: " + new
Double(percentA).toString());

percentB = (gradeB/countScore) * HUNDRED_PERCENT;
System.out.println("Percent of Grade B: " + new
Double(percentB).toString());

percentC = (gradeC/countScore) * HUNDRED_PERCENT;
System.out.println("Percent of Grade C: " + new
Double(percentC).toString());

percentD = (gradeD/countScore) * HUNDRED_PERCENT;
System.out.println("Percent of Grade D: " + new
Double(percentD).toString());

percentF = (gradeF/countScore) * HUNDRED_PERCENT;
System.out.println("Percent of Grade F: " + new
Double(percentF).toString());



}

}
 
P

Paulus de Boska

You're performing integer divisions :
percentA = (gradeA/countScore) * HUNDRED_PERCENT;
If you change this into :
percentA = (gradeA/(double)countScore) * HUNDRED_PERCENT;
fe, casting countScore to a double, you'll get the desired result.
 
M

Mahesh

Hey,
I think U have to learn JAVA from core!.
U have declared the GradeX as integer! & countScore as integer!
though u had GradeX/countScore * 100 assigned to double
"GradeX/countScore" is still a integer operation. ie 1/2 willget u '0'
alone not .5 as u have braced them. TO get the result u xpect
((double)GradeX)/countScore will get u the x pected result
-Mahesh
 
M

me

Mahesh said:
Hey,
I think U have to learn JAVA from core!.

What do you mean? Are you saying the book name?
U have declared the GradeX as integer! & countScore as integer!
though u had GradeX/countScore * 100 assigned to double
"GradeX/countScore" is still a integer operation.

I see. Thanks.
ie 1/2 willget u '0'
alone not .5 as u have braced them. TO get the result u xpect
((double)GradeX)/countScore will get u the x pected result
-Mahesh

BTW, the program asks to put integer value for scores.
 
M

me

Mahesh said:
Hey,
I think U have to learn JAVA from core!.

What do you mean? Are you saying the book name?
U have declared the GradeX as integer! & countScore as integer!
though u had GradeX/countScore * 100 assigned to double
"GradeX/countScore" is still a integer operation.

I see. Thanks.
ie 1/2 willget u '0'
alone not .5 as u have braced them. TO get the result u xpect
((double)GradeX)/countScore will get u the x pected result
-Mahesh

BTW, the program asks to put integer value for scores.
 
M

me

Paulus said:
You're performing integer divisions :
percentA = (gradeA/countScore) * HUNDRED_PERCENT;
If you change this into :
percentA = (gradeA/(double)countScore) * HUNDRED_PERCENT;
fe, casting countScore to a double, you'll get the desired result.

I had percentA = (double) (gradeA/countScore) * HUNDRED_PERCENT before
I made the HUNDRED_PERCENT a double variable and removed it typcasting.
Now I see what was the problem: not typecasting properly. Thanks.


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,184
Members
46,721
Latest member
DollieSawt

Latest Threads

Top