Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with this java question Your task for this lab is to implement and test a class to store ratings of items.

Can someone help me with this java question

image text in transcribed

image text in transcribed

image text in transcribed

Your task for this lab is to implement and test a class to store ratings of items. Your Rating class should have the following fields and methods: private double sumofRatings - the sum of all of the ratings private int raters - the number of people who have rated an item public Rating () - initialize both fields to zero public Rating (double sumofRatings, int raters) initialize the fields to the parameter values public void addRating (double newRating) increase the sumofRatings by newRating and increase the number of raters by one public double getAverageRating () return the average rating; if there are no raters so far, return 0.0 public String toString() - display the average rating and how many reviews it is based on; for example, if the sum of the ratings is 32 and that is based on ratings from ten people, the toString method will return "3.2 based on 10 reviews" Your Rating class should also implement the Comparable interface - Ratings should be sorted such that the highest average rating appears first. If two Rating objects have the same average, the one with the higher number of reviews should appear first If your class is used with this driver code: + rl); Rating r1 = new Rating(); System.out.println("r1: rl.addRating (5); System.out.println("r1: rl.addRating (3.5); System.out.println("r1: + rl); + rl); Rating r2 = new Rating (40, 10); System.out.println("r2: " + r2); Rating r3 = new Rating (12.75, 3); System.out.println("r3: " + r3); ArrayList ratings = new ArrayList(); ratings.add(rl); ratings.add (r2); ratings.add (r3); Collections.sort (ratings); for (Rating r: ratings) { System.out.println(r); it should produce this output: rl: 0.0 based on 0 reviews ri: 5.0 based on 1 reviews ri: 4.25 based on 2 reviews r2: 4.0 based on 10 reviews r3: 4.25 based on 3 reviews 4.25 based on 3 reviews 4.25 based on 2 reviews 4.0 based on 10 reviews

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

What is the pattern to the IPv 4 addressing used in this simulation

Answered: 1 week ago

Question

What are oxidation and reduction reactions? Explain with examples

Answered: 1 week ago