Question

In: Computer Science

--- Note: to save time, write the code directly on Moodle and make sure to save...

--- Note: to save time, write the code directly on Moodle and make sure to save your code frequently

Write a complete Java code that contain two classes. A GradeBook and a GradeBookTester.

- The GradeBook class contains the followings:

- An array of grades (integer array) declared as a field.

- A constructor that takes an integer value as parameter (len), and creates the grades array of size equals to len.

- A method called fillArray that fills the grades array with random integers. Acceptable values are between 1 and 100 (inclusive)

- A method called printStatistics that prints 4 values: the average of the grades, the minimum grade, the maximum grade, and the median grade (the middle value of the sorted array).

- The GradeBookTester contains the main and do the followings:

- Uses the input dialog to ask the user to enter an integer value (assume that the user will enter a value that is non-zero and positive / no need to check)

- creates an object of GradeBook class while passing the entered value as parameter.

- calls the functions fillArray and printStatistics for testing.

Solutions

Expert Solution

GradeBook.java

import java.util.Random; // import Random class
public class GradeBook { // create GradeBok class
private int grades[]; // grades array is defined
  
//default constructor with array size parameter has been created
public GradeBook(int size) {   
grades = new int[size];
}

// method to fill the array
public void fillArray() {
Random rn=new Random();
// loop through the array and randomly fill it
for (int i = 0; i < grades.length; i++) {
grades[i] = rn.nextInt(100) + 1;
}
}

// print the statistics of the array values
public void printStatistics() {
int max = grades[0], min = grades[0];
float avg=0, median=0;
// loop through the array
for (int a : grades) {
if (a < min)
min = a;
if (a > max)
max = a;
avg += a;
}
avg = avg / grades.length;
// sort the array
for (int i = 0; i < grades.length - 1; i++) {
for (int j = 0; j < grades.length - i - 1; j++) {
if (grades[j] > grades[j + 1]) {
int t = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = t;
}
}
}
int l = grades.length;
median = l % 2 == 0 ? (grades[l / 2] + grades[l / 2 + 1]) / 2 : grades[l / 2];
System.out.println("Minimum : " + min + "\nMaximum : " + max + "\nAverage : " + avg + "\n Median : " + median);
}
}

GradeBookTester.java

import java.util.Scanner; // import Scanner class

public class GradeBookTester { // For Testing purpose GradeTester class has created

@SuppressWarnings("resource")
   public static void main(String args[]) { // to test GradeBook purpose we are created main method
Scanner x = new Scanner(System.in); // user to give input of array size purpose we are using canner
// get user input
System.out.print("Enter size of grades array : ");
int l = Integer.parseInt(x.nextLine());
// call GradeBook methods
GradeBook grade = new GradeBook(l);
grade.fillArray(); // call the fillArray
grade.printStatistics(); // call printStatistics to print grades
}
}

output:

Enter size of grades array : 10
Minimum : 5
Maximum : 95
Average : 49.2
Median : 55.0

Screenshots for above classes and output:

GradeBook.java

GradeBookTester.java

Output:


Related Solutions

The following flow chart is a bubble sort. Write the code for this sort. Make sure...
The following flow chart is a bubble sort. Write the code for this sort. Make sure you display each pass and comparison. Include comments.
Crafting a vision statement takes time. Why not save time and go directly to selecting strategies?...
Crafting a vision statement takes time. Why not save time and go directly to selecting strategies? Tracking and measuring sustainability can involve tedious reports and statistics. This can detract from the mission of creating a broader base of support for sustainability. Search community and businesses involved in sustainable actions and find examples of an engaging vision. Refer to Question 9 on page 315 of the textbook.
Write code for a game of picking match sticks. You must make sure that the computer...
Write code for a game of picking match sticks. You must make sure that the computer always wins. The game is as follows There are 26 matchsticks initially. The computer will ask the user to pick anywhere between 1 and 4 matchsticks. The player (whether the user or the computer) who has to pick up the last matchstick (or matchsticks) is the loser. You will have to use a combination of branching and looping commands with appropriate prompts and the...
* Make sure you turn in your code (take a screen shot of your code in...
* Make sure you turn in your code (take a screen shot of your code in R)and answers. Conduct the hypothesis and solve questions by using R. 2) A random sample of 12 graduates of a secretarial school averaged 73.2 words per minute with a standard deviation of 7.9 words per minute on a typing test. What can we conclude, at the .05 level, regarding the claim that secretaries at this school average less than 75 words per minute on...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE WESCHEME IDE************* Write a tail-recursive Racket function "kept-short-rec" that takes an integer and a list of strings as parameters and evaluates to an integer. The resulting integer should be the number of strings on the original list whose string length is less than the integer parameter. For example, (kept-short-rec 3 '("abc" "ab" "a")) should evaluate to 2 because there are only 2 strings shorter...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME IDE*************** Write a recursive Racket function "keep-short-rec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all of the strings from the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME IDE*************** Write a recursive Racket function "sum-diff" that takes two lists of integers that are the same length and evaluates to an integer. The resulting integer should be the sum of the absolute value of the differences between each pair of integers with the same index in the two lists. For example (sum-diff '(-1 -2 -3) '(1 2 3)) should evaluate to 12 because...
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Assignment You should already have some experience with jQuery and some simple experience with JSON (from charting). In this assignment we will be creating some JSON data, parsing it, and displaying it. Step 1 – If you are not familiar with JSON you should complete the JSON tutorial at w3schools Step 2- You will now create a JSON file to represent some data of your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT