Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 02:55 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Homework - Ranking Numbers from a loop

I have to write a problem where I must take test grade inputs and display the following;

Total number of grades
Number of each grade (A, B and C etc...)
Percentage of each grade
Highest and Lowest Scores
Average test score.

I am able to do everything but display the highest and lowest scores with this code:

Code:
 System.out.println("Enter exam scores from 100 to 0.  Use a negative "
                + "number to end your input.");
        boolean areMore = true;
        double next, sum = 0, max, min;
        Scanner keyboard = new Scanner(System.in);
        double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, number = 0;
        
          
            
              
        
        while (areMore)
        {
            
              next = keyboard.nextDouble();
            
           
            
            
            if ((next >= 90) && (next <= 100))
                A++;
            else if ((next <= 89) && (next >= 80))
                B++;
            else if ((next <= 79) && (next >= 70))
                C++;
            else if ((next <= 69) && (next >=60))
                D++;
            else if ((next <=59) && (next >=0))
                F++;
            if (next > 0)
            number++;
            
             
             
           
            
            if (next < 0)
                areMore = false;
            else
                sum = sum + next;
            
            
           
            
           
            
                
                        
            
        }
        System.out.println("Total number of grades = " + number);
        System.out.println("Number of A's = " + A);
        System.out.println("Number of B's = " + B);
        System.out.println("Number of C's = " + C);
        System.out.println("Number of D's = " + D);
        System.out.println("Number of F's = " + F);
        System.out.println("Percentage of A's is " + A / number * 100 + "%");
        System.out.println("Percentage of B's is " + B / number * 100 + "%");
        System.out.println("Percentage of C's is " + C / number * 100 + "%");
        System.out.println("Percentage of D's is " + D / number * 100 + "%");
        System.out.println("Percentage of F's is " + D / number * 100 + "%");
        // System.out.println("The highest score is: " + max);
        // System.out.println("The lowest score is: " + min);
        System.out.println("The average score is: " + sum / number);


I have tried to get the highest and lowest to display via this code:

Code:
max = keyboard.nextDouble();
            min = max;
            next = keyboard.nextDouble();
while (areMore)
  if (next > max)
                    max = next;
                else if (next < min)
                    min = next;
                next = keyboard.nextDouble();


When I use this in my program the first input is never put into the loop, and consequently everything else gets messed up. Also, the sentinel value I use to end my loop keeps getting recorded as the lowest value. I'm stuck on this one.

Reply With Quote
  #2  
Old February 23rd, 2013, 03:01 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
The last code I posted will work by itself, but I don't know how to implement it in the program I already have.

Reply With Quote
  #3  
Old February 23rd, 2013, 03:58 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
What happens when you put the logic in that code inside the loop so it looks at each number as it is entered?

Reply With Quote
  #4  
Old February 23rd, 2013, 04:06 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
The first input in the loop does not get put into the values...although the max and min will be output correctly.

If I put in:

97
87
77
67
57

The 97 will not show up as an A, I will get 4 results total, and the mean will be wrong. Although, the max will be output as 97.

Reply With Quote
  #5  
Old February 23rd, 2013, 04:49 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
Please post the current version of the code that will compile, execute and show the problem.

Also post the program's output that shows what is wrong and add some comments saying what the output should be.

Reply With Quote
  #6  
Old February 23rd, 2013, 06:18 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
So when I take out the Min & Max parts, this code works perfectly well. Here is the code with the Min and Max code put in:

Code:
System.out.println("Enter exam scores from 100 to 0.  Use a negative "
                + "number to end your input.");
        boolean areMore = true;
        double next, sum = 0, max, min;
        Scanner keyboard = new Scanner(System.in);
        double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, number = 0;
        max = keyboard.nextDouble();
        min = max;
        next = keyboard.nextDouble();
        
        
           
        
        while (areMore)
        {
            
              
        
             next = keyboard.nextDouble(); 
           
            
            
            if ((next >= 90) && (next <= 100))
                A++;
            else if ((next <= 89) && (next >= 80))
                B++;
            else if ((next <= 79) && (next >= 70))
                C++;
            else if ((next <= 69) && (next >=60))
                D++;
            else if ((next <=59) && (next >=0))
                F++;
            if (next > 0)
            number++;
            
             
             if (next > max)
                max = next;
            else if (next < min)
                min = next;
            next = keyboard.nextDouble();  
            
          
            
            if (next < 0)
                areMore = false;
            else
                sum = sum + next;
            
       }
        System.out.println("Total number of grades = " + number);
        System.out.println("Number of A's = " + A);
        System.out.println("Number of B's = " + B);
        System.out.println("Number of C's = " + C);
        System.out.println("Number of D's = " + D);
        System.out.println("Number of F's = " + F);
        System.out.println("Percentage of A's is " + A / number * 100 + "%");
        System.out.println("Percentage of B's is " + B / number * 100 + "%");
        System.out.println("Percentage of C's is " + C / number * 100 + "%");
        System.out.println("Percentage of D's is " + D / number * 100 + "%");
        System.out.println("Percentage of F's is " + D / number * 100 + "%");
        System.out.println("The highest score is: " + max);
        System.out.println("The lowest score is: " + min);
        System.out.println("The average score is: " + sum / number);


Output
Quote:
Enter exam scores from 100 to 0. Use a negative number to end your input.
97
87
77
67
57
-1
Total number of grades = 2.0
Number of A's = 0.0
Number of B's = 0.0
Number of C's = 1.0
Number of D's = 0.0
Number of F's = 1.0
Percentage of A's is 0.0%
Percentage of B's is 0.0%
Percentage of C's is 50.0%
Percentage of D's is 0.0%
Percentage of F's is 0.0%
The highest score is: 97.0
The lowest score is: 57.0
The average score is: 33.5


The Min and Max work, but for some reason, everything else is a train wreck. The percents don't even correlate with the number of instances of the grades. If I take out the min and max code I am using, everything works fine.

Reply With Quote
  #7  
Old February 23rd, 2013, 06:25 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
If I had to guess, something is getting messed up with the keyboard.nextDouble() statements. I have no idea why the 67 does not get recorded. The max and min are working as intended, but how to incorporate them into what I already have.

Reply With Quote
  #8  
Old February 23rd, 2013, 06:41 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
Please post a complete program that will compile, execute and show the problem.

The data that is entered and read into the next variable before the while loop is not used.
The data that is read into the max variable is NOT tested for a grade letter.

There should only be one call to the nextDouble() method. The data entered with the other nextDouble() method calls is not used correctly and/or lost/

Reply With Quote
  #9  
Old February 23rd, 2013, 06:45 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by NormR
Please post a complete program that will compile, execute and show the problem.


Thank you very much...I thought I did though...? It is complete, compiles, and shows the problem.

Reply With Quote
  #10  
Old February 23rd, 2013, 06:52 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
Quote:
It is complete

There are no import statements, class definition or method definition.
The posted code in #6 will not compile.

Reply With Quote
  #11  
Old February 23rd, 2013, 06:58 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by NormR
There are no import statements, class definition or method definition.
The posted code in #6 will not compile.


I'm sorry about that.

Code:
package combo;

import java.util.Scanner;

public class Combo 
{

    
    public static void main(String[] args) 
    
    {
        System.out.println("Enter exam scores from 100 to 0.  Use a negative "
                + "number to end your input.");
        boolean areMore = true;
        double next, sum = 0, max, min;
        Scanner keyboard = new Scanner(System.in);
        double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, number = 0;
        
        max = keyboard.nextDouble();
        min = max;
        
        
        
       
        
        
           
        
        while (areMore)
        {
            next = keyboard.nextDouble(); 
            if (next > max)
                max = next;
            else if (next < min)
                min = next;
            
           
            
               
        
            
           
            
            
            if ((next >= 90) && (next <= 100))
                A++;
            else if ((next <= 89) && (next >= 80))
                B++;
            else if ((next <= 79) && (next >= 70))
                C++;
            else if ((next <= 69) && (next >=60))
                D++;
            else if ((next <=59) && (next >=0))
                F++;
            if (next > 0)
            number++;
            
            
            if (next < 0)
                areMore = false;
            else 
            sum = sum + next;
            
             
            
            
          
            
            
                
                
            
                     }
        System.out.println("Total number of grades = " + number);
        System.out.println("Number of A's = " + A);
        System.out.println("Number of B's = " + B);
        System.out.println("Number of C's = " + C);
        System.out.println("Number of D's = " + D);
        System.out.println("Number of F's = " + F);
        System.out.println("Percentage of A's is " + A / number * 100 + "%");
        System.out.println("Percentage of B's is " + B / number * 100 + "%");
        System.out.println("Percentage of C's is " + C / number * 100 + "%");
        System.out.println("Percentage of D's is " + D / number * 100 + "%");
        System.out.println("Percentage of F's is " + D / number * 100 + "%");
        System.out.println("The highest score is: " + max);
        System.out.println("The lowest score is: " + min);
        System.out.println("The average score is: " + sum / number);
                
    }
}


Quote:
run:
Enter exam scores from 100 to 0. Use a negative number to end your input.
97
87
77
67
57
-1
Total number of grades = 4.0
Number of A's = 0.0
Number of B's = 1.0
Number of C's = 1.0
Number of D's = 1.0
Number of F's = 1.0
Percentage of A's is 0.0%
Percentage of B's is 25.0%
Percentage of C's is 25.0%
Percentage of D's is 25.0%
Percentage of F's is 25.0%
The highest score is: 97.0
The lowest score is: -1.0
The average score is: 72.0
BUILD SUCCESSFUL (total time: 4 seconds)


I took out the the next = keyboard.nextDouble() before the loop. That has made things better, but testing the min and max for grades isn't going so well. The sentinel value is appearing in the min which isn't great either.

Reply With Quote
  #12  
Old February 23rd, 2013, 07:02 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
One trick for finding a max value is to initialize max to a very small or negative number.
The first compare will be larger.
For the min value, set min to a very large number. The first compare will be smaller.
Comments on this post
the_passenger agrees: tyvm

Reply With Quote
  #13  
Old February 23rd, 2013, 07:16 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Ahh, thats very helpful. I added this code to get the sentinel out of the Min

Code:
else if ((next < min) && (next > 0))
                min = next;


Quote:
run:
Enter exam scores from 100 to 0. Use a negative number to end your input.
97
87
77
67
57
-1
Total number of grades = 5.0
Number of A's = 7.0
Number of B's = 1.0
Number of C's = 1.0
Number of D's = 1.0
Number of F's = 1.0
Percentage of A's is 140.0%
Percentage of B's is 20.0%
Percentage of C's is 20.0%
Percentage of D's is 20.0%
Percentage of F's is 20.0%
The highest score is: 97.0
The lowest score is: 1.0
The average score is: 77.0
BUILD SUCCESSFUL (total time: 6 seconds)


How exactly is my min being determined to be 1?

edit - The min is my only issue now. Everything else is great.

Reply With Quote
  #14  
Old February 23rd, 2013, 07:22 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
You should use int values for counting vs double.
Can the scores be floating numbers or are they also all int values?

Reply With Quote
  #15  
Old February 23rd, 2013, 07:25 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by NormR

Can the scores be floating numbers or are they also all int values?


The test score inputs have been declared as doubles.

Quote:
You should use int values for counting vs double.


As in for counting the number of A's, B's, etc? I am doing that.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Ranking Numbers from a loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap