Create a Java class named GradeBook which prompts the user to enter the total number of exams, quizzes and lab assignments. Next, prompt the user to enter each grade and save the values in arrays, then calculate the exam, quiz, and lab averages using the values in the three arrays.
> java GradeBook
Enter exam grade #1: 60
Welcome to the CS Gradebook
How many exams? 3
How many quizzes? 3
How many lab assignments? 4
Enter exam grade #1: 80
Enter exam grade #2: 75
Enter exam grade #3: 96
Enter quiz grade #1: 88
Enter quiz grade #2: 100
Enter quiz grade #3: 70
Enter lab grade #1: 90
Enter lab grade #2: 80
Enter lab grade #3: 77
Enter lab grade #4: 89
Exam average: 83.67
Quiz average: 86.00
Lab average: 84.00
GradeBook class
Declare an initialize a static Scanner variable that can be used in all three methods (remember to include an import statement for the Scanner class)
main method
Prompt for total number of exams then declare
Create int array to contain exam grades using the size entered by the user
Prompt for total number of quizzes then declare
Create int array to contain quiz grades using the size entered by the user
Prompt for total number of labs then declare create int array to contain lab grades
Create an int array to contain lab grades using the size entered by the user
Call captureGrades method passing the String "exam" and the exam grades array
Call captureGrades method passing the String "quiz" and the quiz grades array
Call captureGrades method passing the String "lab" and the lab grades array
Call calculateAverage method passing the exam grades array. Print the exam average.
Call calculateAverage method passing the quiz grades array. Print the quiz average.
Call calculateAverage method passing the lab grades array. Print the lab average.
captureGrades method
Two parameters:
- String (containing grade type: "exam", "quiz", "lab")
- int array that will contain values for this grade type
Loop through the array that was passed in as a parameter
Print a prompt with the grade type and sequence number
Use Scanner object to capture the grade as an int and save it in the array
This method doesn't return anything
calculateAverage method
One parameter: int array containing grades for one of the grade types
Declare an int to contain the sum and initialize to zero
Loop through the array that was passed in as a parameter
Retrieve each value from the array and add it to the sum
Declare double variable and calculate the average as sum divided by quantity of grades
Return the double containing the average
1) Roll three blue dice and calculate the sum.
2) Continue to roll three red dice until the red dice sum matches the sum of the blue dice.
3) Print a messaging stating how many attempts it took to match the sum of the red dice with the blue dice.
RedBlueDice class
static variables
int array containing three blue die values
int array containing three red die values
int containing the sum of the blue dice (initialize to 0)
int containing the sum of the red dice (initialize to 0)
Variable containing instance of java.util.Random (this requires an import statement)
main method
Call rollBlue method
Declare and initialize roll counter variable to track the number of rolls required to match
loop
Call rollRed method
Call showDice method
Call compareDice method
if it returns true, break out of the loop
if it returns false, increment the counter
Print a message showing the total number of rolls it took to get matching sums
rollBlue method
Generate three random roll values (range 1-6) and store each in the blue die values array
Sum the three values and save in the variable containing the sum of the blue dice
rollRed method
Generate three random roll values (range 1-6) and store each in the red die values array
Sum the three values and save in the variable containing the sum of the red dice
showDice method
Print the three blue die values and the blue total and the red die values and the red total
(Match the format shown in the sample output)
Must use loops to retrieve and print array values for full credit
compareDice method
Returns true if the blue sum is equal to the red sum, otherwise returns false
Red Blue Dice Sample Output
> java RedBlueDice
Blue dice: 1 4 5 (sum: 10) Red dice: 1 4 4 (sum: 9)
Blue dice: 1 4 5 (sum: 10) Red dice: 6 1 6 (sum: 13)
Blue dice: 1 4 5 (sum: 10) Red dice: 2 5 2 (sum: 9)
Blue dice: 1 4 5 (sum: 10) Red dice: 1 3 3 (sum: 7)
Blue dice: 1 4 5 (sum: 10) Red dice: 3 2 6 (sum: 11)
Blue dice: 1 4 5 (sum: 10) Red dice: 2 3 1 (sum: 6)
Blue dice: 1 4 5 (sum: 10) Red dice: 4 5 3 (sum: 12)
Blue dice: 1 4 5 (sum: 10) Red dice: 3 1 3 (sum: 7)
Blue dice: 1 4 5 (sum: 10) Red dice: 5 5 6 (sum: 16)
Blue dice: 1 4 5 (sum: 10) Red dice: 6 3 3 (sum: 12)
Blue dice: 1 4 5 (sum: 10) Red dice: 3 2 5 (sum: 10)
It took 11 rolls to match the sums of the red and blue dice