Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[Java] Please fix this code. I'm in kind of hurry, but it won't pass the grader. Grader's attached under the instruction. For this assignment you

[Java] Please fix this code. I'm in kind of hurry, but it won't pass the grader.

Grader's attached under the instruction.

For this assignment you will create a class called Movie and observe its behavior when it is managed by different kinds of Film Archive collections.

Class movie Create a public class Movie with private instance variables String title and int year. The class should declare that it implements the Comparable interface, and should provide the following: A constructor that takes 2 arguments: a String and an int (in that order) for initializing title and year. A method that satisfies the Comparable interface. Movies should be compared first by title and then by year.

Methods getTitle() and getYear() that do the right thing. An equals() method that is compatible with the method that satisfies the Comparable interface. A toString() method that returns Movie followed by 1 space followed by the title followed by 1 space followed by open-parenthesis followed by the year followed by close-parenthesis. Example: Movie The Maltese Falcon (1941)

(same title and same year). The remaining elements can be any movies you like. A hashCode() method. Use the following: public int hashCode() { return title.hashCode() + year; } Interface FilmArchive Create an interface called FilmArchive that defines 2 methods: getSorted(), which takes no args and returns ArrayList. Implementations should return an array list whose members are unique according to deep equality, and sorted according to the criteria in Movies compareTo() method. add(), which takes one arg of type Movie and returns a boolean. If add() is called where the arg already appears in the film archive, the method should return false and otherwise do nothing; if the arg of add() does not yet appear in the archive, it should be added as described below and the method should return true. 3 implementing classes Create 3 classes, named ListFilmArchive, HashFilmArchive, and TreeFilmArchive, that implement the FilmArchive interface. Class ListFilmArchive This class should extend ArrayList. In your add() method, check every movie in the array list for deep equality to the arg of add(). If you find a movie that equals() the arg, just return false; if you dont find one, add the arg to the array list and return true. Hint: you are overriding the add() method inherited from the ArrayList superclass. When you detect that the arg movie doesnt appear in the array list, you want to call the superclass version of add(). To do that, use something like this line: boolean reallyAdded = super.add(aMovie); super. in this overridden method tells Java please use the version of add in my superclass. For the getSorted() method, use a TreeSet to do the sorting. First construct a TreeSet, passing the ArrayList into the TreeSet constructor; since ListFilmArchive isa ArrayList, this is a reference to the ArrayList that you want to pass. Then construct and return a new ArrayList, passing the TreeSet into the ArrayList constructor; when that ArrayList is constructed, the movies in the TreeSet will be added to it one by one, in the TreeSets natural order, which is the order that you want. In other words, this long paragraph describes 2 simple lines of code. Class HashFilmArchive This class should extend HashSet. Its ok to add movies to a HashSet because Movie has mutually compatible equals(), and hashCode() methods. For the add() method, first read the documentation for add() in the java.util.HashSet API page (https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html ). Convince yourself that this method does exactly what add() in HashFilmArchive should do. Since the inherited method is acceptable, you dont need to create add() in HashFilmArchive. (But you do need to understand why you dont need to). For the getSorted() method, do something similar to what you did in ListFilmArchive. Class TreeFilmArchive This class should extend TreeSet. For the add() method, first read the documentation for add() in the API page for java.util.TreeSet (https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html ). Convince yourself that this method does exactly what add() in TreeFilmArchive should do. Since the inherited method is acceptable, you dont need to create add() in TreeFilmArchive. For the getSorted() method, do something somewhat similar to, but simpler than, what you did in ListFilmArchive and HashFilmArchive. All you need to do is construct an ArrayList, passing this into the ArrayList ctor. Well, thats not all you need to do you also need to understand why it works.

-------------------------------------------------------------------------------------

This assignment should pass the grader which is provided below.

FilmArchiveGrader.java ----------------------------------------------- import java.lang.reflect.*; import java.util.*;

import javax.xml.ws.handler.MessageContext.Scope;

public class FilmArchiveGrader { private final static Class[] EMPTY_TYPE_LIST = { }; private final static Class[] STRING_INT_TYPE_LIST = { String.class, int.class };

private static Class[] MOVIE_TYPE_LIST; private static Class filmArchiveClass;

static { try { MOVIE_TYPE_LIST = new Class[] { Class.forName("movies.Movie") }; } catch (ClassNotFoundException x) { sop("No movies.Movie class"); System.exit(1); } }

private static Map reasonToDeductions;

// Aborts on 1st error. private static void checkStructure() { checkFilmArchiveStructure(); checkMovieStructure(); checkImplementorsStructure(); }

private static void abortZeroPoints(String msg) { sop(msg + " Zero points"); System.exit(1); }

private static void checkFilmArchiveStructure() { try { filmArchiveClass = Class.forName("movies.FilmArchive"); // exists? } catch (ClassNotFoundException x) { abortZeroPoints("No movies.FilmArchive interface"); } assert filmArchiveClass != null; if (!filmArchiveClass.isInterface()) // is an interface? { abortZeroPoints("movies.FilmArchive isn't an interface"); } try { filmArchiveClass.getMethod("getSorted", EMPTY_TYPE_LIST); // getSorted() ok? } catch (NoSuchMethodException x) { abortZeroPoints("No getSorted() method in FilmArchive"); } try { Method addMeth = filmArchiveClass.getMethod("add", MOVIE_TYPE_LIST); // add() ok? if (addMeth.getReturnType() != boolean.class) abortZeroPoints("add() in FilmArchive has wrong return type, should be boolean"); } catch (NoSuchMethodException x) { sop("No add(Movie) method in FilmArchive"); System.exit(1); } }

// Static initializer already verified class exists. private static void checkMovieStructure() { Class movieClass = MOVIE_TYPE_LIST[0]; assert movieClass != null;

try { movieClass.getConstructor(STRING_INT_TYPE_LIST); } catch (NoSuchMethodException x) { abortZeroPoints("No Movie(String, int) constructor in Movie"); }

boolean implementsOk = false; for (Class c: movieClass.getInterfaces()) { if (c.equals(Comparable.class)) { implementsOk = true; break; } } if (!implementsOk) // implements Comparable? abortZeroPoints("Movies class doesn't declare that it implements Comparable");

try { Method cmpMeth = movieClass.getMethod("compareTo", MOVIE_TYPE_LIST); // compareTo() if (cmpMeth.getReturnType() != int.class) abortZeroPoints("compareTo() in Movie has wrong return type, should be int"); } catch (NoSuchMethodException x) { sop("No compareTo(Movie) method in Movie"); System.exit(1); }

try // getTestMovies() { Method getTestMoviesMeth = movieClass.getMethod("getTestMovies", EMPTY_TYPE_LIST); int mods = getTestMoviesMeth.getModifiers(); if ((mods & Modifier.STATIC) == 0) abortZeroPoints("getTestMovies() in Movie isn't static"); } catch (NoSuchMethodException x) { sop("No getTestMovies() method in Movie"); System.exit(1); }

try // getTitle() { Method getTitleMeth = movieClass.getMethod("getTitle", EMPTY_TYPE_LIST); if (getTitleMeth.getReturnType() != String.class) abortZeroPoints("In Movie, getTitle() should return a String"); } catch (NoSuchMethodException x) { sop("No getTestMovies() method in Movie"); System.exit(1); }

try // getYear() { Method getYearMeth = movieClass.getMethod("getYear", EMPTY_TYPE_LIST); if (getYearMeth.getReturnType() != int.class) abortZeroPoints("In Movie, getTitle() should return an int"); } catch (NoSuchMethodException x) { sop("No getTestMovies() method in Movie"); System.exit(1); } }

private static void checkImplementorsStructure() { String[] classNames = { "ListFilmArchive", "HashFilmArchive", "TreeFilmArchive" }; for (String className: classNames) { try { Class clazz = Class.forName("movies." + className); // exists? boolean implementsOk = false; for (Class ifclass: clazz.getInterfaces()) { if (ifclass == filmArchiveClass) { implementsOk = true; break; } } if (!implementsOk) // implements Comparable? abortZeroPoints(className + " class doesn't declare that it implements FilmArchive"); int mods = clazz.getModifiers(); if ((mods & Modifier.ABSTRACT) != 0) abortZeroPoints(className + " should not be abstract"); } catch (ClassNotFoundException x) { abortZeroPoints("No class " + className); } } }

private static void checkFunctionality() { checkMovieFunctionality(); }

private static String movieToString(Movie m) { return '"' + m.getTitle() + "\" " + m.getYear(); }

private static void checkMovieFunctionality() { Movie falcon1 = new Movie("The Maltese Falcon", 1941); Movie falcon2 = new Movie("The Maltese Falcon", 1941); Movie tc1 = new Movie("The Thomas Crown Affair", 1968); Movie tc2 = new Movie("The Thomas Crown Affair", 1999); Movie[] movies = { falcon1, falcon2, tc1, tc2 };

// Check hash codes. for (Movie m: movies) { int goldenHashCode = m.getTitle().hashCode() + m.getYear(); if (m.hashCode() != goldenHashCode) { String err = "Bad hashCode for " + movieToString(m) + ": expected " + goldenHashCode + ", saw " + m.hashCode(); reasonToDeductions.put(err, 15); break; } }

// Comparison. Movie fclub = new Movie("Fight Club", 1999); Movie[] these = { falcon1, falcon1, falcon1, tc1, tc2, fclub }; Movie[] those = { falcon1, falcon2, tc1, tc2, tc1, tc2 }; int[] expects = { 0, 0, -1, -1, 1, -1 }; assert these.length == those.length; assert those.length == expects.length; for (int i=0; i

// Test movies. Movie[] testMovies = Movie.getTestMovies(); if (testMovies == null) { reasonToDeductions.put("Movie.getTestMovies() returned null", 25); return; } if (testMovies.length != 10) { String s = "Movie.getTestMovies() returned an array of length " + testMovies.length + ", expected 10"; if (testMovies.length < 6) { reasonToDeductions.put(s, 25); return; } else reasonToDeductions.put(s, 15); } assert testMovies.length == 10; for (int i=0; i<10; i++) { if (testMovies[i] == null) { reasonToDeductions.put("testMovies[" + i + "] is null", 15); break; } } Movie m0 = testMovies[0]; Movie m1 = testMovies[1]; if (!m0.getTitle().equals(m1.getTitle())) reasonToDeductions.put("Items 0 and 1 in test movies array don't have same title", 10); if (m0.getYear() == m1.getYear()) reasonToDeductions.put("Items 0 and 1 in test movies array don't have different years", 10); Movie m2 = testMovies[2]; Movie m3 = testMovies[3]; if (m2.getTitle().equals(m3.getTitle())) reasonToDeductions.put("Items 2 and 3 in test movies array don't have different titles", 10); if (m2.getYear() != m3.getYear()) reasonToDeductions.put("Items 2 and 3 in test movies array don't have same years", 10); }

private static void checkAnalyzers() { String[] names = { "HashAnalyzer", "ListAnalyzer" }; for (String name: names) { try { Class clazz = Class.forName("movies." + name); } catch (ClassNotFoundException x) { reasonToDeductions.put("No " + name + " class", 25); } } }

private static void printScore() { int totalDeductions = 0; for (Integer ded: reasonToDeductions.values()) totalDeductions += ded; totalDeductions = Math.min(totalDeductions, 100); if (totalDeductions == 0) sop("Graderbot deducted zero points"); else { sop("Graderbot deductions: "); for (String reason: reasonToDeductions.keySet()) sop(" -" + reasonToDeductions.get(reason) + ": " + reason); sop("Total graderbot deductions = " + totalDeductions); } }

static void sop(Object x) { System.out.println(x); }

public static void main(String[] args) { sop("START");

// Any structure errors => immediate failure. checkStructure();

// Functional errors cause deductions. reasonToDeductions = new LinkedHashMap<>(); checkFunctionality();

// Check analyzers. Submission can pass if these have wrong structure. checkAnalyzers();

printScore(); } } ------------------------------------------------------------------------------------------ FilmArchive.java --------------------------------------------

import java.util.ArrayList;

public interface FilmArchive { public ArrayList getSorted(); public boolean add(Movie m); }

------------------------------------------------------------------------------------------ HashFilmArchive.java -------------------------------------------- import java.util.ArrayList; import java.util.HashSet; import java.util.TreeSet;

public class HashFilmArchive extends HashSet implements FilmArchive {

public ArrayList getSorted() { HashSet hashy = new HashSet(this); ArrayList someMovies = new ArrayList(hashy); return someMovies; }

public static void main(String[] args) { HashFilmArchive archive = new HashFilmArchive(); for (Movie m: Movie.getTestMovies()) archive.add(m);

for (Movie m: archive) System.out.println(m); }

} ----------------------------------------------------------------------------------------- HashAnalyzer.java ----------------------------------- public class HashAnalyzer { public static void main(String[] args) { HashFilmArchive archive = new HashFilmArchive(); for (int i=0; i<100000; i++) { archive.add(new Movie("Movie" + i, 2017)); if (i%1000 == 0) System.out.println(i); } } } ----------------------------------------------------------------------------------------- ListFilmArchive.java -------------------------------------------- import java.util.ArrayList; import java.util.TreeSet;

public class ListFilmArchive extends ArrayList implements FilmArchive{

public boolean add(Movie that) { for(Movie m: this) { if(m.getYear() == that.getYear() && m.getTitle().equals(that.getTitle())) { return false; } } super.add(that); return true; }

public ArrayList getSorted() { TreeSet tree = new TreeSet(this); ArrayList someMovies = new ArrayList(tree); return someMovies;

}

public static void main(String[] args) { ListFilmArchive archive = new ListFilmArchive(); for (Movie m: Movie.getTestMovies()) archive.add(m);

for (Movie m: archive) System.out.println(m); } } ------------------------------------------------------------------------------------------- ListAnalyzer.java ---------------------------------------------- public class ListAnalyzer { public static void main(String[] args) { ListFilmArchive archive = new ListFilmArchive(); for (int i = 0; i < 100000; i++) { archive.add(new Movie("Movie" + i, 2017)); if (i%1000 == 0) System.out.println(i); } } } ---------------------------------------------------------------------------------------- Movies.java ----------------------------------- public class Movie implements Comparable{

private String title; private int year;

public Movie(String title, int year) { this.title = title; this.year = year; }

public String getTitle() { return title; }

public int getYear() { return year; }

public boolean equals(Object x) { Movie that = (Movie)x; return this.compareTo(that) == 0; }

public int compareTo(Movie that) { int titleCmp = this.title.compareTo(that.title); if (titleCmp != 0) { return titleCmp; } if (this.year < that.year) { return -1; } else if (this.year == that.year) { return 0; } else { return 1; } }

public String toString() { return "Movie " + title + " (" + year + ")"; }

public static Movie[] getTestMovies() { Movie[] allMovies = new Movie[10]; allMovies[0] = new Movie("Aladdin", 1968); allMovies[1] = new Movie("Aladdin", 1956); allMovies[2] = new Movie("Mr.Penguin", 1977); allMovies[3] = new Movie("Fremont", 1977); allMovies[4] = new Movie("The Parent Trap", 1998); allMovies[5] = new Movie("The Parent Trap", 1998); allMovies[6] = new Movie("The Intern", 2015); allMovies[7] = new Movie("Tangled", 2013); allMovies[8] = new Movie("Up", 2008); allMovies[9] = new Movie("Pride & Prejudice", 2003); return allMovies; }

public int hashCode() { return title.hashCode() + year; } } -------------------------------------------------------------------------------- TreeFilmArchive.java ----------------------------------------- import java.util.TreeSet; import java.util.ArrayList;

public class TreeFilmArchive extends TreeSet implements FilmArchive {

public ArrayList getSorted() { ArrayList movies = new ArrayList(this); return movies; }

public static void main(String[] args) { TreeFilmArchive archive = new TreeFilmArchive(); for (Movie m: Movie.getTestMovies()) archive.add(m);

for (Movie m: archive) System.out.println(m); }

}

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

Step: 3

blur-text-image

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

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago