In: Computer Science
Java program.
Need to create a class names gradesgraph which will represent the GradesGraphtest program.
Assignment
Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks:
• Set the number of each of the letter grades A, B, C, D, and F.
• Read the number of each of the letter grades A, B, C, D, and F.
• Return the total number of grades.
• Return the percentage of each letter grade as a whole number between 0 and 100, inclusive.
• Draw a bar graph of the grade distribution.
The graph will have five bars, one per grade. Each bar can be a horizontal row of asterisks, such that the number of asterisks in a row is proportionate to the percentage of grades in each category. Let one asterisk represent 2 percent, so 50 asterisks correspond to 100 percent. Mark the horizontal axis at 10 percent increments from 0 to 100 percent, and label each line with its letter grade.
For example, if the grades are 1 A, 4 Bs, 6 Cs, 2 Ds, and 1 F, the total number of grades is 14, the percentage of As is 7, the percentage of Bs is 29, the percentage of Cs is 43, the percentage of Ds is 14, and the percentage of Fs is 7. The A row would contain 4 asterisks (7 percent of 50 rounded to the nearest integer), the B row 14, the C row 21, the D row 7, and the F row 4.
Test Program
public class GradesGraphTest
{
public static void main(String[] args)
{
int numberOfAs;
int numberOfBs;
int numberOfCs;
int numberOfDs;
int numberOfFs;
System.out.println();
System.out.println("Test case 1: readInput() and writeOutput()");
GradesGraph grades = new GradesGraph();
grades.readInput();
System.out.println();
grades.writeOutput();
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 2:");
System.out.println("set counts as a group: A=1, B=2, etc.");
grades.set(1, 2, 3, 4, 5);
System.out.println();
System.out.println("Verify results with writeOutput():");
grades.writeOutput();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 3:");
System.out.println("set counts individually");
System.out.println("A=10, B=9, etc.");
grades.setAcount(10);
grades.setBcount(9);
grades.setCcount(8);
grades.setDcount(7);
grades.setFcount(6);
System.out.println();
System.out.println("Verify results with writeOutput():");
grades.writeOutput();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 4:");
System.out.println("write (display) counts individually:");
System.out.println();
System.out.println("Verify A=10, B=9, etc.");
System.out.println();
grades.writeAcount();
grades.writeBcount();
grades.writeCcount();
grades.writeDcount();
grades.writeFcount();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 5:");
System.out.println("return counts individually:");
System.out.println();
System.out.println("Verify A=10, B=9, etc.");
System.out.println();
System.out.println("A count = " + grades.getAcount());
System.out.println("B count = " + grades.getBcount());
System.out.println("C count = " + grades.getCcount());
System.out.println("D count = " + grades.getDcount());
System.out.println("F count = " + grades.getFcount());
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 6:");
System.out.println("return total number of grades");
System.out.println();
System.out.println("Verify total = 40");
System.out.println();
System.out.println("Total number of grades = "
+ grades.getTotalNumberOfGrades());
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 7:");
System.out.println("return percentages of each grade");
System.out.println();
System.out.println("Verify A=25, B=23, C=20, D=18, & F=15");
System.out.println();
System.out.println("A = " + grades.getPercentA());
System.out.println("B = " + grades.getPercentB());
System.out.println("C = " + grades.getPercentC());
System.out.println("D = " + grades.getPercentD());
System.out.println("F = " + grades.getPercentF());
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 8:");
System.out.println("Draw graph of grade percentages");
System.out.println();
System.out.println("Verify A=25, B=23, C=20, D=18, & F=15");
System.out.println();
grades.draw();
System.out.println();
System.out.println("Test case 9:");
System.out.println("Draw graph of grade percentages");
System.out.println();
System.out.println("Verify A=100%");
grades.set(1, 0, 0, 0, 0);
System.out.println();
grades.draw();
System.out.println();
System.out.println("Test case 10:");
System.out.println("Draw graph of grade percentages");
System.out.println();
System.out.println("Verify all 0%");
grades.set(0, 0, 0, 0, 0);
System.out.println();
grades.draw();
}
}
The output will be:
Test case 1: readInput() and writeOutput()
How many A's?
5
How many B's?
9
How many C's?
5
How many D's?
6
How many F's?
2
Number of A's = 5
Number of B's = 9
Number of C's = 5
Number of D's = 6
Number of F's = 2
===============================
Test case 2:
set counts as a group: A=1, B=2, etc.
Verify results with writeOutput():
Number of A's = 1
Number of B's = 2
Number of C's = 3
Number of D's = 4
Number of F's = 5
===============================
Test case 3:
set counts individually
A=10, B=9, etc.
Verify results with writeOutput():
Number of A's = 10
Number of B's = 9
Number of C's = 8
Number of D's = 7
Number of F's = 6
===============================
Test case 4:
write (display) counts individually:
Verify A=10, B=9, etc.
Number of A's = 10
Number of B's = 9
Number of C's = 8
Number of D's = 7
Number of F's = 6
===============================
Test case 5:
return counts individually:
Verify A=10, B=9, etc.
A count = 10
B count = 9
C count = 8
D count = 7
F count = 6
===============================
Test case 6:
return total number of grades
Verify total = 40
Total number of grades = 40
===============================
Test case 7:
return percentages of each grade
Verify A=25, B=23, C=20, D=18, & F=15
A = 25
B = 23
C = 20
D = 18
F = 15
===============================
Test case 8:
Draw graph of grade percentages
Verify A=25, B=23, C=20, D=18, & F=15
2 10 20 30 40 50 60 70 80 90 100
| | | | | | | | | | |
**************************************************
************ A
*********** B
********** C
********* D
******* F
Test case 9:
Draw graph of grade percentages
Verify A=100%
2 10 20 30 40 50 60 70 80 90 100
| | | | | | | | | | |
**************************************************
************************************************** A
B
C
D
F
Test case 10:
Draw graph of grade percentages
Verify all 0%
2 10 20 30 40 50 60 70 80 90 100
| | | | | | | | | | |
**************************************************
A
B
C
D
F
Code:-
GradesGraph.java
import java.util.Scanner;
public class GradesGraph
{
private int Acount;
private int Bcount;
private int Ccount;
private int Dcount;
private int Fcount;
private int asteriskCount;
/**
Methods to set data members
*/
//Interactively assign values to all data members
public void readInput() {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many A's?");
Acount = keyboard.nextInt();
System.out.println("How many B's?");
Bcount = keyboard.nextInt();
System.out.println("How many C's?");
Ccount = keyboard.nextInt();
System.out.println("How many D's?");
Dcount = keyboard.nextInt();
System.out.println("How many F's?");
Fcount = keyboard.nextInt();
}
//The following method sets all grades to newGradeCount
public void set(int newAcount, int newBcount,
int newCcount, int newDcount,
int newFcount) {
Acount = newAcount;
Bcount = newBcount;
Ccount = newCcount;
Dcount = newDcount;
Fcount = newFcount;
}
//While these methods set individual grades to
newGradeCount
public void setAcount(int newAcount) {
Acount = newAcount;
}
public void setBcount(int newBcount) {
Bcount = newBcount;
}
public void setCcount(int newCcount) {
Ccount = newCcount;
}
public void setDcount(int newDcount) {
Dcount = newDcount;
}
public void setFcount(int newFcount) {
Fcount = newFcount;
}
public void writeOutput() { //Write out all grade counts
System.out.println("Number of A's = " + Acount);
System.out.println("Number of B's = " + Bcount);
System.out.println("Number of C's = " + Ccount);
System.out.println("Number of D's = " + Dcount);
System.out.println("Number of F's = " + Fcount);
}
//Display data to user
public void writeAcount() {
System.out.println("Number of A's = " + Acount);
}
public void writeBcount() {
System.out.println("Number of B's = " + Bcount);
}
public void writeCcount() {
System.out.println("Number of C's = " + Ccount);
}
public void writeDcount() {
System.out.println("Number of D's = " + Dcount);
}
public void writeFcount() {
System.out.println("Number of F's = " + Fcount);
}
//Return data for grades
public int getAcount() {
return Acount;
}
public int getBcount() {
return Bcount;
}
public int getCcount() {
return Ccount;
}
public int getDcount() {
return Dcount;
}
public int getFcount() {
return Fcount;
}
public int getTotalNumberOfGrades() { //Returns all five
gradeCounts
return (Acount + Bcount + Ccount + Dcount + Fcount);
}
//These get the percentages
public int getPercentA() {
return (int)((float)Acount/this.getTotalNumberOfGrades() *
100
+ 0.5);
}
public int getPercentB() {
return (int)((float)Bcount/this.getTotalNumberOfGrades() *
100
+ 0.5);
}
public int getPercentC() {
return (int)((float)Ccount/this.getTotalNumberOfGrades() *
100
+ 0.5);
}
public int getPercentD() {
return (int)((float)Dcount/this.getTotalNumberOfGrades() *
100
+ 0.5);
}
public int getPercentF() {
return (int)((float)Fcount/this.getTotalNumberOfGrades() *
100
+ 0.5);
}
public void draw() { //Draws the graph thing.
//Draw scale
System.out.println("2 10 20 30 40 50 60 70 80 90 100");
System.out.println("| | | | | | | | | | |");
System.out.println("**************************************************");
//For loops count the asterisks down to get gradeCount
for(asteriskCount = 1;
asteriskCount <= getPercentA()/2;
asteriskCount++) {
System.out.print("*");
}
System.out.println(" A");
for(asteriskCount = 1;
asteriskCount <= getPercentB()/2;
asteriskCount++) {
System.out.print("*");
}
System.out.println(" B");
for(asteriskCount = 1;
asteriskCount <= getPercentC()/2;
asteriskCount++) {
System.out.print("*");
}
System.out.println(" C");
for(asteriskCount = 1;
asteriskCount <= getPercentD()/2;
asteriskCount++) {
System.out.print("*");
}
System.out.println(" D");
for(asteriskCount = 1;
asteriskCount <= getPercentF()/2;
asteriskCount++) {
System.out.print("*");
}
System.out.println(" F");
}
}
GradesGraphTest.java
public class GradesGraphTest
{
public static void main(String[] args)
{
int numberOfAs;
int numberOfBs;
int numberOfCs;
int numberOfDs;
int numberOfFs;
System.out.println();
System.out.println("Test case 1: readInput() and
writeOutput()");
GradesGraph grades = new GradesGraph();
grades.readInput();
System.out.println();
grades.writeOutput();
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 2:");
System.out.println("set counts as a group: A=1, B=2, etc.");
grades.set(1, 2, 3, 4, 5);
System.out.println();
System.out.println("Verify results with writeOutput():");
grades.writeOutput();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 3:");
System.out.println("set counts individually");
System.out.println("A=10, B=9, etc.");
grades.setAcount(10);
grades.setBcount(9);
grades.setCcount(8);
grades.setDcount(7);
grades.setFcount(6);
System.out.println();
System.out.println("Verify results with writeOutput():");
grades.writeOutput();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 4:");
System.out.println("write (display) counts individually:");
System.out.println();
System.out.println("Verify A=10, B=9, etc.");
System.out.println();
grades.writeAcount();
grades.writeBcount();
grades.writeCcount();
grades.writeDcount();
grades.writeFcount();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 5:");
System.out.println("return counts individually:");
System.out.println();
System.out.println("Verify A=10, B=9, etc.");
System.out.println();
System.out.println("A count = " + grades.getAcount());
System.out.println("B count = " + grades.getBcount());
System.out.println("C count = " + grades.getCcount());
System.out.println("D count = " + grades.getDcount());
System.out.println("F count = " + grades.getFcount());
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 6:");
System.out.println("return total number of grades");
System.out.println();
System.out.println("Verify total = 40");
System.out.println();
System.out.println("Total number of grades = "
+ grades.getTotalNumberOfGrades());
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 7:");
System.out.println("return percentages of each grade");
System.out.println();
System.out.println("Verify A=25, B=23, C=20, D=18, &
F=15");
System.out.println();
System.out.println("A = " + grades.getPercentA());
System.out.println("B = " + grades.getPercentB());
System.out.println("C = " + grades.getPercentC());
System.out.println("D = " + grades.getPercentD());
System.out.println("F = " + grades.getPercentF());
System.out.println();
System.out.println("===============================");
System.out.println();
System.out.println();
System.out.println("Test case 8:");
System.out.println("Draw graph of grade percentages");
System.out.println();
System.out.println("Verify A=25, B=23, C=20, D=18, &
F=15");
System.out.println();
grades.draw();
System.out.println();
System.out.println("Test case 9:");
System.out.println("Draw graph of grade percentages");
System.out.println();
System.out.println("Verify A=100%");
grades.set(1, 0, 0, 0, 0);
System.out.println();
grades.draw();
System.out.println();
System.out.println("Test case 10:");
System.out.println("Draw graph of grade percentages");
System.out.println();
System.out.println("Verify all 0%");
grades.set(0, 0, 0, 0, 0);
System.out.println();
grades.draw();
}
}
Output:-
if u like my asnwer then please give a thumbs up..!!