Question
(Java Code) . The program works well, but it makes some assumptions about the way movie ratings are stored. All the ratings have to be
(Java Code) .
The program works well, but it makes some assumptions about the way movie ratings are stored. All the ratings have to be in the same scale (e.g. in this example, they are all out of 10), but often reviews use different scales (stars out of 5, percentages out of 100). Change the program to allow the program to use different scales, but keep the final results as numbers out of 10. The range of the scale will be added as an extra input line for each movie. For example:
Casablanca 8.7 10.0 Toy Story 8.1 10.0 Star Wars Episode IV 88.0 100.0 Toy Story 4.0 5.0
The first two ratings are out of 10 as before, but the third is 88 out of 100, and the last rating is 4 out of 5 stars.
Download the complete Movie Reviews program and add the following features to it:
Read three lines for each movie instead of two. Start out by just printing the data from the extra line.
Convert the third line into a double (outOf) and scale the rating by dividing it by outOf and multiplying the result by 10. Use this as the movie's rating out of 10 when creating or modifying a review.
Note that the conversion of this value should also occur inside the nested "try" block. Since the error message in the "catch" may apply to an invalid rating or an invalid outOf value, change it appropriately.
Submit only the complete file input try-catch block (and not the rest of the main program, or any other methods or classes) by the due date specified in the course schedule.
import java.io.*;
public class Activity2A { public static void main(String[] args) { BufferedReader input; String title, ratingText; double rating;
try { input = new BufferedReader(new FileReader("movies.txt"));
title = input.readLine(); while (title != null) { ratingText = input.readLine();
rating = Double.parseDouble(ratingText); System.out.println(title + ": " + rating);
title = input.readLine(); }
input.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); }
System.out.println(" Finished processing."); } }
import java.io.*;
public class Activity2B { public static void main(String[] args) { BufferedReader input; String title, ratingText; double rating;
Review[] movies = new Review[100]; int size = 0;
try { input = new BufferedReader(new FileReader("movies.txt"));
title = input.readLine(); while (title != null) { ratingText = input.readLine();
rating = Double.parseDouble(ratingText); movies[size] = new Review(title, rating); size++;
title = input.readLine(); }
input.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); }
for (int i = 0; i < size; i++) { System.out.println(movies[i]); }
System.out.println(" Finished processing."); } }
class Review { private String title; private double rating;
public Review(String title, double rating) { this.title = title; this.rating = rating; }
public String toString() { return title + " (rating: " + rating + ")"; } }
import java.io.*;
public class Activity2C { public static void main(String[] args) { BufferedReader input; String title, ratingText; double rating;
Review[] movies = new Review[100]; int size = 0; Review match;
try { input = new BufferedReader(new FileReader("movies.txt"));
title = input.readLine(); while (title != null) { ratingText = input.readLine();
rating = Double.parseDouble(ratingText); match = findReview(movies, size, title); if (match == null) { // movie that was not previously listed movies[size] = new Review(title, rating); size++; } else { // this movie was already reviewed at least once match.addRating(rating); }
title = input.readLine(); }
input.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); }
for (int i = 0; i < size; i++) { System.out.println(movies[i]); }
System.out.println(" Finished processing."); }
public static Review findReview(Review[] movies, int size, String title) { Review result = null; int pos;
pos = 0; while (pos < size && result == null) { if (movies[pos].matchTitle(title)) { result = movies[pos]; } else { pos++; } }
return result; } }
class Review { private String title; private double totalRating; private int reviewCount;
public Review(String title, double rating) { this.title = title; totalRating = rating; reviewCount = 1; }
public void addRating(double rating) { totalRating += rating; reviewCount++; }
public boolean matchTitle(String otherTitle) { // returns true if this movie's title matches otherTitle return title.equals(otherTitle); }
public String toString() { return title + " (rating: " + (totalRating / reviewCount) + ")"; } }
import java.io.*;
public class Activity2D { public static void main(String[] args) { BufferedReader input; String title, ratingText; double rating = 0.0;
Review[] movies = new Review[100]; int size = 0; Review match;
try { input = new BufferedReader(new FileReader("movies.txt"));
title = input.readLine(); while (title != null) { ratingText = input.readLine();
try {
rating = Double.parseDouble(ratingText);
// if the conversion failed and the code below was not in the // "try" block, the rating variable would contain the rating of // the previous movie we read in (bad data) match = findReview(movies, size, title); if (match == null) { // movie that was not previously listed movies[size] = new Review(title, rating); size++; } else { // this movie was already reviewed at least once match.addRating(rating); }
} catch (NumberFormatException nfe) { System.out.println("Invalid rating: " + ratingText); }
title = input.readLine(); }
input.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); }
for (int i = 0; i < size; i++) { System.out.println(movies[i]); }
System.out.println(" Finished processing."); }
public static Review findReview(Review[] movies, int size, String title) { Review result = null; int pos;
pos = 0; while (pos < size && result == null) { if (movies[pos].matchTitle(title)) { result = movies[pos]; } else { pos++; } }
return result; } }
class Review { private String title; private double totalRating; private int reviewCount;
public Review(String title, double rating) { this.title = title; totalRating = rating; reviewCount = 1; }
public void addRating(double rating) { totalRating += rating; reviewCount++; }
public boolean matchTitle(String otherTitle) { // returns true if this movie's title matches otherTitle return title.equals(otherTitle); }
public String toString() { return title + " (rating: " + (totalRating / reviewCount) + ")"; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started