Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Java Code) . The program works well, but it makes some assumptions about the wa

ID: 3899346 • Letter: #

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 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:

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) + ")";
}
}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. The required changes are done in Activity2D.java file. Thanks.

// Activity2D.java

import java.io.*;

public class Activity2D {

                public static void main(String[] args) {

                                BufferedReader input;

                                String title, ratingText;

                                double rating = 0.0,outOf=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);

                                                                               

                                                                                /**

                                                                                * parsing the total rating (outOf)

                                                                                */

                                                                                outOf= Double.parseDouble(input.readLine());

                                                                                /**

                                                                                * scaling the rating out of 10

                                                                                */

                                                                                rating=(rating/outOf)*10;

                                                                                // 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) + ")";

                }

}

/*OUTPUT*/

Casablanca (rating: 8.7)

Toy Story (rating: 8.05)

Star Wars Episode IV (rating: 8.8)

Finished processing.