Finding percent :\

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 9
Reputation: AmyxD is an unknown quantity at this point 
Solved Threads: 0
AmyxD AmyxD is offline Offline
Newbie Poster

Finding percent :\

 
0
  #1
Nov 6th, 2008
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


  1. import java.util.*;
  2.  
  3. public class Grades {
  4. public static void main(String[] args) {
  5.  
  6. Scanner input = new Scanner(System.in);
  7.  
  8. //Variables
  9. double grade = 0;
  10. double sum = 0;
  11. double highest = 0;
  12. double lowest = 100;
  13. int counter = 0;
  14. int Acounter = 0;
  15. int Bcounter = 0;
  16. int Ccounter = 0;
  17. int Dcounter = 0;
  18. int Fcounter = 0;
  19. double Apercent = 0;
  20. double Bpercent = 0;
  21. double Cpercent = 0;
  22. double Dpercent = 0;
  23. double Fpercent = 0;
  24.  
  25. //Header
  26. System.out.println("\t\t\t\tGrades Program");
  27. System.out.println("\nEnter valid student grades (in decimal values ranging from 0-100)." +
  28. "\nInvalid grades will not be considered. Enter -1 to view the results.\n\n");
  29.  
  30. //Scope of valid input numbers
  31. if (grade>=0 && grade<=100){
  32.  
  33. while ( grade != -1 ){ //sentinel value
  34. System.out.print("\tInput a grade: ");
  35. grade = input.nextDouble();
  36.  
  37. //Determines # of valid grades and # of each letter grade
  38. if (grade<=100 && grade>=93){
  39. Acounter++;
  40. counter++;
  41. sum = sum+grade;
  42. }
  43. else if (grade<=92 && grade>=85){
  44. Bcounter++;
  45. counter++;
  46. sum = sum+grade;
  47. }
  48. else if (grade<=84 && grade>=75){
  49. Ccounter++;
  50. counter++;
  51. sum = sum+grade;
  52. }
  53. else if (grade<=75 && grade>=70){
  54. Dcounter++;
  55. counter++;
  56. sum = sum+grade;
  57. }
  58. else if (grade<=74 && grade>=0){
  59. Fcounter++;
  60. counter++;
  61. sum = sum+grade;
  62. }
  63. //Determines highest and lowest values
  64. if (highest<grade){
  65. highest=grade;
  66. }
  67. if (lowest>grade){
  68. lowest=grade;
  69. }
  70.  
  71. }
  72. }
  73. //Calculations
  74. Apercent = ((Acounter/counter)*100);
  75. Bpercent = ((Bcounter/counter)*100);
  76. Cpercent = ((Ccounter/counter)*100);
  77. Dpercent = ((Dcounter/counter)*100);
  78. Fpercent = ((Fcounter/counter)*100);
  79.  
  80. int pass = (Acounter+Bcounter+Ccounter+Dcounter);
  81. double passPercent = ((pass/counter)*100);
  82.  
  83. double average = (sum/counter);
  84.  
  85.  
  86. //Output
  87. System.out.println("\n\nYour Results:");
  88. System.out.println("\n\nThe total amount of valid grades input were: " + counter);
  89.  
  90. System.out.println("\nLetter Equivalent\tInstances\t% of total\t");
  91. System.out.println("\tAs:\t\t" + Acounter + "\t\t" + Apercent + "%");
  92. System.out.println("\tBs:\t\t" + Bcounter + "\t\t" + Bpercent + "%");
  93. System.out.println("\tCs:\t\t" + Ccounter + "\t\t" + Cpercent + "%");
  94. System.out.println("\tDs:\t\t" + Dcounter + "\t\t" + Dpercent + "%");
  95. System.out.println("\tFs:\t\t" + Fcounter + "\t\t" + Fpercent + "%");
  96. System.out.println("\n\tpass:\t\t" + pass + "\t\t" + passPercent + "%");
  97. System.out.println("\tfail:\t\t" + Fcounter + "\t\t" + Fpercent + "%");
  98. System.out.println("\nThe highest valid grade you entered was: " + highest);
  99. System.out.println("The lowest valid grade you entered was: " + lowest);
  100. System.out.println("\nThe average of the valid grades you entered is: " + average + "\n\n");
  101. }
  102. }
Last edited by AmyxD; Nov 6th, 2008 at 9:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Finding percent :\

 
0
  #2
Nov 6th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: AmyxD is an unknown quantity at this point 
Solved Threads: 0
AmyxD AmyxD is offline Offline
Newbie Poster

Re: Finding percent :\

 
0
  #3
Nov 6th, 2008
thank you sillyboy I appreciate you taking the time to help

yes I get 0.0% for everything >.< I'll think about what you said and play around with the code a bit more
Last edited by AmyxD; Nov 6th, 2008 at 10:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Finding percent :\

 
0
  #4
Nov 6th, 2008
Give a heads up if you get too stuck.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: AmyxD is an unknown quantity at this point 
Solved Threads: 0
AmyxD AmyxD is offline Offline
Newbie Poster

Re: Finding percent :\

 
0
  #5
Nov 6th, 2008
double Apercent = (((double)Acounter)/counter)*100);
I tried changing my percentage equations to this... I read that the Acounter will be considered a double now as well as the counter if used like that but no luck
Last edited by AmyxD; Nov 6th, 2008 at 11:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Finding percent :\

 
0
  #6
Nov 6th, 2008
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:

  1. public class Main {
  2.  
  3. public static void main(String [] args) {
  4.  
  5. double result;
  6.  
  7. int value = 50;
  8. int count = 100;
  9.  
  10. result = ((double)value / count) * 100;
  11.  
  12. System.out.println(result);
  13. }
  14.  
  15. }

Console output:

  1. 50.0
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: AmyxD is an unknown quantity at this point 
Solved Threads: 0
AmyxD AmyxD is offline Offline
Newbie Poster

Re: Finding percent :\

 
0
  #7
Nov 7th, 2008
hmmmm... I'll keep searching
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Finding percent :\

 
0
  #8
Nov 7th, 2008
what about this

  1. Apercent = (((double)Acounter/counter)*100.0);
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Finding percent :\

 
0
  #9
Nov 7th, 2008
I copy / pasted your original code and added (double) in the divisions, this is the output I got (looks like it works to me):

  1. Grades Program
  2.  
  3. Enter valid student grades (in decimal values ranging from 0-100).
  4. Invalid grades will not be considered. Enter -1 to view the results.
  5.  
  6.  
  7. Input a grade: 99
  8. Input a grade: 85
  9. Input a grade: 70
  10. Input a grade: 60
  11. Input a grade: 50
  12. Input a grade: 40
  13. Input a grade: 30
  14. Input a grade: -1
  15.  
  16.  
  17. Your Results:
  18.  
  19.  
  20. The total amount of valid grades input were: 7
  21.  
  22. Letter Equivalent Instances % of total
  23. As: 1 14.285714285714285%
  24. Bs: 1 14.285714285714285%
  25. Cs: 0 0.0%
  26. Ds: 1 14.285714285714285%
  27. Fs: 4 57.14285714285714%
  28.  
  29. pass: 3 42.857142857142854%
  30. fail: 4 57.14285714285714%
  31.  
  32. The highest valid grade you entered was: 99.0
  33. The lowest valid grade you entered was: -1.0
  34.  
  35. The average of the valid grades you entered is: 62.0
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Finding percent :\

 
0
  #10
Nov 7th, 2008
looks like lowest grade needs to be changed a little bit

  1. if ((lowest>grade) && (grade > -1)){
  2. lowest=grade;
  3. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 3955 | Replies: 18
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC