Final answer:
The updateAverage method updates the running average by incorporating a new rating, whereas the processNewRatings method processes multiple new ratings and updates the running average accordingly, counting the number of invalid ratings (those less than 0).
Step-by-step explanation:
To solve part (a), the updateAverage method should update the running average of the ratings by considering the new value (newVal) received. The updated average can be calculated by multiplying the current count (number of ratings) by the current average to get the total sum of all the ratings so far, adding the newVal to this total, incrementing the count by 1, then dividing the new total by the new count.
The processNewRatings method is designed to process a given number (num) of new ratings by updating the running average while ignoring negative ratings, which are considered invalid. This method returns the count of these invalid ratings.
Update Average Method
public void updateAverage(double newVal) {
double total = this.average * this.count;
total += newVal;
this.count++;
this.average = total / this.count;
}
Process New Ratings Method
public int processNewRatings(int num) {
int invalidCount = 0;
for (int i = 0; i < num; i++) {
double rating = getNewRating();
if (rating >= 0) {
updateAverage(rating);
} else {
invalidCount++;
}
}
return invalidCount;
}