Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

regent help in logic error (code included)

Thread view: 
me - 07 Feb 2006 10:03 GMT
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());
        

}

}
Paulus de Boska - 07 Feb 2006 12:03 GMT
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.

---
Paul Hamaker, SEMM
http://javalessons.com
me - 07 Feb 2006 18:27 GMT
> 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.

> ---
> Paul Hamaker, SEMM
> http://javalessons.com
Mahesh - 07 Feb 2006 12:39 GMT
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
me - 07 Feb 2006 18:25 GMT
> 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.
me - 07 Feb 2006 18:25 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.