| | |
Finding percent :\
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 9
Reputation:
Solved Threads: 0
Hey guys =] First I just wanted to say I am excited to have found this forum, it looks like the members are very helpful and friendly! Hopefully I'll be able to contribute what I can but I'm not very knowledgeable 
Anyway, I am to create a grading program(utilizing only loops, arrays aren't allowed) that accepts grades input by the user, quits when they enter the sentinel value, and display the highest and lowest grades. Also the teacher would like us to display the mean, median and mode if possible (I assume that the median and mode aren't possible without an array). All this was easy and I got it done without cheating from the book like the rest of my classmates
.
I decided to add some of my own touches to it, like displaying the number of each equivalent letter grade, the number of passing and failing grades, and the percentage of each. This is where my problem comes in! I can't figure out how to create percentages!!
For example:
There is a counter for the total valid grades entered, as well as seperate counters for 'As', 'Bs', 'Cs', etc. If I try to find the percent of A grades out of the total I just can't figure it out. I messed around trying different things for 2 hours or so and tried googling for a class I could import or an equation that could help me. I know what I have now is incorrect, I just can't figure out why. This logic seems right to me... percent = ((part/total)*100). It works like that in a calculator. Does anyone have any advice to help me? Is there something I'm just looking over?
I'm not here to get answers without trying, I just really need a push in the right direction from somebody who knows what they're talking about(because I sure don't). I've tried really hard to solve the problem myself and this is a last resort! Also, if you can't help with the percents problem, could you at least critique me on the style of my coding (neatness/efficiency), that would be very very helpful as well
I'm trying to improve myself! Thanks guys!!!!!!!!!! 
Oh! and out of curiousity... is it possible to make a String my sentinel value even though the input is a double type? (User types 'quit' instead of -1) Hmm we did discuss data type conversions a bit I'll look into it if its possible

Anyway, I am to create a grading program(utilizing only loops, arrays aren't allowed) that accepts grades input by the user, quits when they enter the sentinel value, and display the highest and lowest grades. Also the teacher would like us to display the mean, median and mode if possible (I assume that the median and mode aren't possible without an array). All this was easy and I got it done without cheating from the book like the rest of my classmates
.I decided to add some of my own touches to it, like displaying the number of each equivalent letter grade, the number of passing and failing grades, and the percentage of each. This is where my problem comes in! I can't figure out how to create percentages!!
For example:
There is a counter for the total valid grades entered, as well as seperate counters for 'As', 'Bs', 'Cs', etc. If I try to find the percent of A grades out of the total I just can't figure it out. I messed around trying different things for 2 hours or so and tried googling for a class I could import or an equation that could help me. I know what I have now is incorrect, I just can't figure out why. This logic seems right to me... percent = ((part/total)*100). It works like that in a calculator. Does anyone have any advice to help me? Is there something I'm just looking over?
I'm not here to get answers without trying, I just really need a push in the right direction from somebody who knows what they're talking about(because I sure don't). I've tried really hard to solve the problem myself and this is a last resort! Also, if you can't help with the percents problem, could you at least critique me on the style of my coding (neatness/efficiency), that would be very very helpful as well
I'm trying to improve myself! Thanks guys!!!!!!!!!! Oh! and out of curiousity... is it possible to make a String my sentinel value even though the input is a double type? (User types 'quit' instead of -1) Hmm we did discuss data type conversions a bit I'll look into it if its possible

Java Syntax (Toggle Plain Text)
import java.util.*; public class Grades { public static void main(String[] args) { Scanner input = new Scanner(System.in); //Variables double grade = 0; double sum = 0; double highest = 0; double lowest = 100; int counter = 0; int Acounter = 0; int Bcounter = 0; int Ccounter = 0; int Dcounter = 0; int Fcounter = 0; double Apercent = 0; double Bpercent = 0; double Cpercent = 0; double Dpercent = 0; double Fpercent = 0; //Header System.out.println("\t\t\t\tGrades Program"); System.out.println("\nEnter valid student grades (in decimal values ranging from 0-100)." + "\nInvalid grades will not be considered. Enter -1 to view the results.\n\n"); //Scope of valid input numbers if (grade>=0 && grade<=100){ while ( grade != -1 ){ //sentinel value System.out.print("\tInput a grade: "); grade = input.nextDouble(); //Determines # of valid grades and # of each letter grade if (grade<=100 && grade>=93){ Acounter++; counter++; sum = sum+grade; } else if (grade<=92 && grade>=85){ Bcounter++; counter++; sum = sum+grade; } else if (grade<=84 && grade>=75){ Ccounter++; counter++; sum = sum+grade; } else if (grade<=75 && grade>=70){ Dcounter++; counter++; sum = sum+grade; } else if (grade<=74 && grade>=0){ Fcounter++; counter++; sum = sum+grade; } //Determines highest and lowest values if (highest<grade){ highest=grade; } if (lowest>grade){ lowest=grade; } } } //Calculations Apercent = ((Acounter/counter)*100); Bpercent = ((Bcounter/counter)*100); Cpercent = ((Ccounter/counter)*100); Dpercent = ((Dcounter/counter)*100); Fpercent = ((Fcounter/counter)*100); int pass = (Acounter+Bcounter+Ccounter+Dcounter); double passPercent = ((pass/counter)*100); double average = (sum/counter); //Output System.out.println("\n\nYour Results:"); System.out.println("\n\nThe total amount of valid grades input were: " + counter); System.out.println("\nLetter Equivalent\tInstances\t% of total\t"); System.out.println("\tAs:\t\t" + Acounter + "\t\t" + Apercent + "%"); System.out.println("\tBs:\t\t" + Bcounter + "\t\t" + Bpercent + "%"); System.out.println("\tCs:\t\t" + Ccounter + "\t\t" + Cpercent + "%"); System.out.println("\tDs:\t\t" + Dcounter + "\t\t" + Dpercent + "%"); System.out.println("\tFs:\t\t" + Fcounter + "\t\t" + Fpercent + "%"); System.out.println("\n\tpass:\t\t" + pass + "\t\t" + passPercent + "%"); System.out.println("\tfail:\t\t" + Fcounter + "\t\t" + Fpercent + "%"); System.out.println("\nThe highest valid grade you entered was: " + highest); System.out.println("The lowest valid grade you entered was: " + lowest); System.out.println("\nThe average of the valid grades you entered is: " + average + "\n\n"); } }
Last edited by AmyxD; Nov 6th, 2008 at 9:52 pm.
Hey, I think you are on the right track with your percentages. So by it not working, I assume you get getting results of 0% for everything?
If this is indeed the case, the problem is int / int is integer division, and will return a int. So... since counter is always greater than your percentage, it is always 0.XYZ which will get trimmed to be 0. Since you are keen to learn and put in the effort, you should be able to find a solution to this
.
In regard to having string inputs, sure you will be able to do this. But if you decide to take this route, you may need to learn some exception handling... I am not too sure how strict your requirements for the assignment are and it seems you are already going beyond the scope, so perhaps you won't need exception handling.
hope I have helped...
If this is indeed the case, the problem is int / int is integer division, and will return a int. So... since counter is always greater than your percentage, it is always 0.XYZ which will get trimmed to be 0. Since you are keen to learn and put in the effort, you should be able to find a solution to this
.In regard to having string inputs, sure you will be able to do this. But if you decide to take this route, you may need to learn some exception handling... I am not too sure how strict your requirements for the assignment are and it seems you are already going beyond the scope, so perhaps you won't need exception handling.
hope I have helped...
That looks correct, so if it isn't working I fear you probably have some silly mistake somewhere else :p
This is a simple test program I wrote:
Console output:
This is a simple test program I wrote:
Java Syntax (Toggle Plain Text)
public class Main { public static void main(String [] args) { double result; int value = 50; int count = 100; result = ((double)value / count) * 100; System.out.println(result); } }
Console output:
Java Syntax (Toggle Plain Text)
50.0
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
I copy / pasted your original code and added (double) in the divisions, this is the output I got (looks like it works to me):
Java Syntax (Toggle Plain Text)
Grades Program Enter valid student grades (in decimal values ranging from 0-100). Invalid grades will not be considered. Enter -1 to view the results. Input a grade: 99 Input a grade: 85 Input a grade: 70 Input a grade: 60 Input a grade: 50 Input a grade: 40 Input a grade: 30 Input a grade: -1 Your Results: The total amount of valid grades input were: 7 Letter Equivalent Instances % of total As: 1 14.285714285714285% Bs: 1 14.285714285714285% Cs: 0 0.0% Ds: 1 14.285714285714285% Fs: 4 57.14285714285714% pass: 3 42.857142857142854% fail: 4 57.14285714285714% The highest valid grade you entered was: 99.0 The lowest valid grade you entered was: -1.0 The average of the valid grades you entered is: 62.0
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
looks like lowest grade needs to be changed a little bit
Java Syntax (Toggle Plain Text)
if ((lowest>grade) && (grade > -1)){ lowest=grade; }
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- Piracy (Geeks' Lounge)
- how can i... (VB.NET)
- Marketing a Poker Site (Promotion and Marketing Plans)
- Students saying no to computer science (IT Professionals' Lounge)
- Loop Issues (C)
Other Threads in the Java Forum
- Previous Thread: Help required in running simple spring web application...
- Next Thread: Inheritance
Views: 3955 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






