SHOW ALL YOUR WORK. PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. You shouldn't write significant amounts of code that can be replaced by a call to one of these methods .   A manufacturer wants to keep track of the average of the ratings that have been submitted for an item using a running average. The algorithm for calculating a running average differs from the standard algorithm for calculating an average, as described in part (a). A partial declaration of the RunningAverage class is shown below. You will write two methods of the RunningAverage class. public class RunningAverage { /** The number of ratings included in the running average. */ private int count;   /** The average of the ratings that have been entered. */ private double average;   // There are no other instance variables.   /** Creates a RunningAverage object. * Postcondition: count is initialized to 0 and average is * initialized to 0.0. */ public RunningAverage() { /* implementation not shown */ }   /** Updates the running average to reflect the entry of a new * rating, as described in part (a). */ public void updateAverage(double newVal) { /* to be implemented in part (a) */ }   /** Processes num new ratings by considering them for inclusion * in the running average and updating the running average as * necessary. Returns an integer that represents the number of * invalid ratings, as described in part (b). * Precondition: num > 0 */ public int processNewRatings(int num) { /* to be implemented in part (b) */ }   /** Returns a single numeric rating. */ public double getNewRating() { /* implementation not shown */ } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Pls answer part a and b accordingly. Read this first:

SHOW ALL YOUR WORK. PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

  • Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
  • Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
  • In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. You shouldn't write significant amounts of code that can be replaced by a call to one of these methods .

 

A manufacturer wants to keep track of the average of the ratings that have been submitted for an item using a running average. The algorithm for calculating a running average differs from the standard algorithm for calculating an average, as described in part (a).

A partial declaration of the RunningAverage class is shown below. You will write two methods of the RunningAverage class.

public class RunningAverage

{

/** The number of ratings included in the running average. */

private int count;

 

/** The average of the ratings that have been entered. */

private double average;

 

// There are no other instance variables.

 

/** Creates a RunningAverage object.

* Postcondition: count is initialized to 0 and average is

* initialized to 0.0.

*/

public RunningAverage()

{ /* implementation not shown */ }

 

/** Updates the running average to reflect the entry of a new

* rating, as described in part (a).

*/

public void updateAverage(double newVal)

{ /* to be implemented in part (a) */ }

 

/** Processes num new ratings by considering them for inclusion

* in the running average and updating the running average as

* necessary. Returns an integer that represents the number of

* invalid ratings, as described in part (b).

* Precondition: num > 0

*/

public int processNewRatings(int num)

{ /* to be implemented in part (b) */ }

 

/** Returns a single numeric rating. */

public double getNewRating()

{ /* implementation not shown */ }

}

(b) Write the processNewRatings method, which considers num new ratings for inclusion in the running average. A helper method, getNewRating, which returns a single rating, has been provided for you.
The running average must only be updated with ratings that are greater than or equal to zero. Ratings that are less than 0 are considered invalid and are not included in the running average.
The processNewRatings method returns the number of invalid ratings. See the table below for three examples of how calls to processNewRatings should work.
* invalid ratings, as described in part (b).
* Precondition: num > 0
Statement
public int process NewRatings (int num)
processNewRatings (2)
processNewRatings (1)
processNewRatings (4)
Ratings
Generated
2.5, 4.5
-2.0
0.0, -2.2,
3.5, -1.5
Complete method process NewRatings. Assume that updateAverage works as specified, regardless of what you wrote in part (a). You must use getNewRating and updateAverage appropriately,
/** Processes num new ratings by considering them for inclusion
* in the running average and updating the running average as
* necessary. Returns an integer that represents the number of
processNewRatings
Return Value
0
1
2
Comments
Both new ratings are included in the running average.
No new ratings are included in the running average.
Two new ratings (0.0 and 3.5) are included in the running average.
Transcribed Image Text:(b) Write the processNewRatings method, which considers num new ratings for inclusion in the running average. A helper method, getNewRating, which returns a single rating, has been provided for you. The running average must only be updated with ratings that are greater than or equal to zero. Ratings that are less than 0 are considered invalid and are not included in the running average. The processNewRatings method returns the number of invalid ratings. See the table below for three examples of how calls to processNewRatings should work. * invalid ratings, as described in part (b). * Precondition: num > 0 Statement public int process NewRatings (int num) processNewRatings (2) processNewRatings (1) processNewRatings (4) Ratings Generated 2.5, 4.5 -2.0 0.0, -2.2, 3.5, -1.5 Complete method process NewRatings. Assume that updateAverage works as specified, regardless of what you wrote in part (a). You must use getNewRating and updateAverage appropriately, /** Processes num new ratings by considering them for inclusion * in the running average and updating the running average as * necessary. Returns an integer that represents the number of processNewRatings Return Value 0 1 2 Comments Both new ratings are included in the running average. No new ratings are included in the running average. Two new ratings (0.0 and 3.5) are included in the running average.
(a) Write the method updateAverage, which updates the Running Average object to include a new rating. To update a running average, add the new rating to a calculated total, which is the number of ratings times the current running average. Divide the new total by the incremented count to obtain
the new running average.
For example, if there are 4 ratings with a current running average of 3.5, the calculated total is 4 times 3.5, or 14.0. When a fifth rating with a value of 6.0 is included, the new total becomes 20.0. The new running average is 20.0 divided by 5, or 4.0.
Complete method updateAverage.
/** Updates the running average to reflect the entry of a new
* rating, as described in part (a).
public void updateAverage (double newVal)
Bookmark
Transcribed Image Text:(a) Write the method updateAverage, which updates the Running Average object to include a new rating. To update a running average, add the new rating to a calculated total, which is the number of ratings times the current running average. Divide the new total by the incremented count to obtain the new running average. For example, if there are 4 ratings with a current running average of 3.5, the calculated total is 4 times 3.5, or 14.0. When a fifth rating with a value of 6.0 is included, the new total becomes 20.0. The new running average is 20.0 divided by 5, or 4.0. Complete method updateAverage. /** Updates the running average to reflect the entry of a new * rating, as described in part (a). public void updateAverage (double newVal) Bookmark
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education