Requirement
From now on, all requests will involve defining classes with instance variables and methods.
Instance variables are used to describe an object of the class. For example, to describe a rectangle, we need to have height and width, so should be instance variables of the Rectangle class.
Methods are used to manipulate the instance variables, for example to retrieve, or modify, or print out the instance variables etc.
When designing a class, you should answer the following question: What information is needed in order to describe an object?
This information should be defined as instance variables. If some information is not related with object, they SHOULD NOT be defined as instance variables. For example, Scanner object is needed for input, but it is not necessary to describe a rectangle, so it should not be an instance variable.
When some information is related with the object, but can be obtained from some other instance variables, you have to decide if you want to define them as instance variables. If you do define them as instance variables, you have to make sure the consistency of these variables when you modify some instance variables. For example, the area of a rectangle is a function of height and width, if you define the area as instance variable; you have to make sure the area is updated whenever you update height and width.
This information should be defined as instance variables. If some information is not related with object, they SHOULD NOT be defined as instance variables. For example, Scanner object is needed for input, but it is not necessary to describe a rectangle, so it should not be an instance variable.
When some information is related with the object, but can be obtained from some other instance variables, you have to decide if you want to define them as instance variables. If you do define them as instance variables, you have to make sure the consistency of these variables when you modify some instance variables. For example, the area of a rectangle is a function of height and width, if you define the area as instance variable; you have to make sure the area is updated whenever you update height and width.
This request is based on Project 3 on page 372. Create a class that represents a grade distribution for a given course.
- Download the attached GradeDistribution.txt, rename it to GradeDistribution.java
- Complete the class definition by
- Determine the needed instance variables
- Define the set methods for each of the letter grades A, B, C, D, and F.
Note: a set method should (1) have return type void
(2) take a parameter for the new value of the instance variable.
(3) assign the value of the instance variable to be the given parameter. - Define get method for each of the letter grades A, B, C, D, and F.
Note: a get method should (1) takes no parameter (2) returns the value of an instance variable (3) the return type should be the type of the instance variable.
- Define the following method which returns the total number of grades.
- Method that returns the percentage of each letter grade as a whole number between 0 and 100, inclusive.
- Draw a bar graph of the grade distribution.
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 the class
Download the GradesGraphTest.java, compile and run compare the output with the attached sample output, debug your code if it doesn’t look right.