Q:

Write a Java program that performs the following: • Represent Pass/Fail distribution for a course grades as a bar graph. The program should prompt the user to enter either P for pass or F for fail, to stop the user enters S

0

Write a Java program that performs the following:

  • Represent Pass/Fail distribution for a course grades as a bar graph. The program should prompt the user to enter either P for pass or F for fail, to stop the user enters S.
  • After the user enters S, the program calculates and displays the grade distribution as shown in the example below.
  • In the example the user entered 6 grades : 4 Ps and 2 Fs. The P and F bars represent these percentages. The number of asterisks * printed are ratio x 50. For passing grades it is calculated as (4/6) x 50 = 33.33, which means 33 asterisks are printed, and for failing grades it is calculated as (2/6) x 50 = 16.7, which means 16 asterisks will printed.
  • To print the numbers of the scale (0,10,20, … 100), you can use printf() with the format specifier %5s or %5d for each number. The same applies to the bars “|”. 

 

Running Example:

Please enter P for Pass, F for Fail or S to stop P

Please enter P for Pass, F for Fail or S to stop F

Please enter P for Pass, F for Fail or S to stop P

Please enter P for Pass, F for Fail or S to stop F

Please enter P for Pass, F for Fail or S to stop P

Please enter P for Pass, F for Fail or S to stop P

Please enter P for Pass, F for Fail or S to stop S

0    10    20    30    40    50    60    70    80    90    100%

|       |       |       |       |       |       |       |       |       |          |

********************************* P

**************** F  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
//0.5 for the class and main structure
//0.5 for the variable declarations
class Main{
public static void main(String[] args){
 Scanner input=new Scanner(System.in);
 char grade = ' ';
 System.out.print("Please enter P for Pass, F for Fail or S to stop ");
 grade = input.next().charAt(0);
 int countP = 0, countF = 0, countAll = 0;
//2.0 for the while and switch or if statement and logic
 while (grade !='S'){
switch (grade) {
case 'P': countP++;
 countAll++;
 break;
case 'F': countF++;
 countAll++;
break;
default :
System.out.println("Wrong input");}

//using if statement:
/*if(grade == 'P')
{
    countP++;
    countAll++;
}
else if(grade == 'F')
{
    countF++;
    countAll++;
}
else System.out.println("Wrong input");
*/
 System.out.print("Please enter P for Pass, F for Fail or S to stop ");
 grade = input.next().charAt(0);
}
System.out.printf("%s%5s%5s%5s%5s%5s%5s%5s%5s%5s%5s%%%n",
"0","10","20","30","40","50","60","70","80","90","100");
System.out.println("0     10     20 30 40 50 60 70 80 90 100");
System.out.printf("%s%5s%5s%5s%5s%5s%5s%5s%5s%5s%5s%n",
"|","|","|","|","|","|","|","|","|","|","|");
//1.0 for calculating the percentages 
double percentP = (double) countP/countAll * 50;
double percentF = (double) countF/countAll * 50;
int i = 1;
//1.0 for printing the P bar
while (i < percentP){
i++;
System.out.print("*");}
System.out.printf(" P %.2f %n", percentP);
//1.0 for printing the F bar
i = 1;
while (i < percentF){
System.out.print("*");
 i++;}
System.out.printf(" F %.2f %n", percentF); 
}}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now