Question
Create a class called GradeHistogram Copy the code for GradeHistogram given below into the class. Take a few minutes to understand what the class does.
- Create a class called GradeHistogram
- Copy the code for GradeHistogram given below into the class.
- Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called printChart. printChart calls the method printDataRow that prints the asterisks for how many As, Bs, Cs, etc look at output to see what the histogram looks like.
- You will have to write one method in the GradeHistogram class
- The method should be called printDataRow
- It has two formal parameters A string for the row label An int for how many asterisks are in the row
- The method will print The string that was passed in followed by a colon and a space The asterisks (*) After the last asterisk it will print a newline You may want to test the method multiple times in BlueJ before going on. Example: the statement: printDataRow("Number of As", 7); would output the following:
Number of As: *******
Code for class GradeHistogram
public class GradeHistogram { private String className; private int numAs; private int numBs; private int numCs; private int numDs; private int numFs; public GradeHistogram(String className, int numAs, int numBs, int numCs, int numDs, int numFs) { this.className = className; this.numAs = numAs; this.numBs = numBs; this.numCs = numCs; this.numDs = numDs; this.numFs = numFs; } public void setClassName(String name) { className = name; } public void setNumAs(int num) { numAs = num; } public void setNumBs(int num) { numBs = num; } public void setNumCs(int num) { numCs = num; } public void setNumDs(int num) { numDs = num; } public void setNumFs(int num) { numFs = num; } public String getClassName() { return className; } public int getNumAs() { return numAs; } public int getNumBs() { return numBs; }public int getNumCs() { return numCs; }public int getNumDs() { return numDs; }public int getNumFs() { return numFs; } // Write the method printDataRow public void printChart() { System.out.println("Class Name: " + className); printDataRow("Number of As", numAs); printDataRow("Number of Bs", numBs); printDataRow("Number of Cs", numCs); printDataRow("Number of Ds", numDs); printDataRow("Number of Fs", numFs); } }
Once you have the class working
Read the entire section before coding - you may want to jot some code on paper to think it through before you type it in
-
Create a class called Main and write the main method in the class to produce the output given
- Declare a variable to hold the grade as it is read in
- Declare 5 variables to hold counts to count the number of As, Bs, Cs, etc
- declare a variable to hold the class name and ask the user to enter the name of the class whose grades are going to be entered.
- Write a loop that reads in an integer (the grade) a negative value will terminate the loop
- The loop should check the grade entered and then add to the appropriate count depending on if the grade is an A, B, C, etc (90 100 is an A, 80 89 is a B, etc)
- After the loop has ended and all the counts have the values, create an object of the GradeHistogram class using the constructor in the class.
- Use the printChart method to print the histogram for the object you created.
-
The program, when finished, should create the output given
-
When you have it running correctly, upload the classes to zybook and submit it for grading.
It would be wise to check things are working correctly as you go. You might want to first see if you can get the loop to read in the values and stop with the sentinel value. Then you might want to just count the As (check your answer by printing it out or using the debugger), Continue adding slowly to main and make sure the counts are correct. Then create the object and print the histogram.
It might be easier to test it using your own data at first for example try it with just one A (a 90), then add another grade. Make sure it works as expected. Then you can try it with the examples I have below. You can just copy the following lines and paste them in when the computer asks to enter the grades
68 75 91 -12 75 95 85 72 68 59 92 95 97 64 0 71 58 74 93 88 87 88 77 75 65 83 89 71 75 78 86 61 84 -5 95 72 93 91 78 94 80 -1
Sample Output 1
Enter the name of the class: CS140-001 Enter a negative number to indicate the last grade Enter the class grades: 68 75 91 -12 Class Name: CS140-001 Number of As: * Number of Bs: Number of Cs: * Number of Ds: * Number of Fs:
Sample Output 2
Enter the name of the class: CS145-001 Enter a negative number to indicate the last grade Enter the class grades: 75 95 85 72 68 59 92 95 97 64 0 71 58 74 93 88 87 88 77 75 65 83 89 71 75 78 86 61 84 -5 Class Name: CS145-001 Number of As: ***** Number of Bs: ******** Number of Cs: ********* Number of Ds: **** Number of Fs: ***
Sample Output 3
Enter the name of the class: CS111-001 Enter a negative number to indicate the last grade Enter the class grades: 95 72 93 91 78 94 80 -1 Class Name: CS111-001 Number of As: **** Number of Bs: * Number of Cs: ** Number of Ds: Number of Fs:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started