Question

In: Computer Science

Create a class called MovieReducerExtremes that implements MediaReducer. Implement a reducer that takes a movie list...

Create a class called MovieReducerExtremes that implements MediaReducer. Implement a reducer that takes a movie list and an option ("newest" or "oldest"), then return the newest or oldest movie as appropriate.Submit both the MovieReducerExtremes and the Movie class from the first question.

/////Required Output:///////

Newest\n
 2014 AKA Jessica Jones                                       Action         \n
Oldest\n
 1936 Cabaret                                                 Music          \n

Given Files:

Movie.java

public class Movie extends Media {
    public Movie(String name, int year, String genre) {
        super(name, year, genre);
    }

    public String getEra() {
        if (getYear() >= 2000) {
            return "New Millennium Era";
        } else if (getYear() >= 1977) {
            return "Modern Era";
        } else if (getYear() >= 1955) {
            return "Change Era";
        } else if (getYear() >= 1941) {
            return "Golden Era";
        }

        return "Pre-Golden Era";
    }

    public boolean wasReleasedAfter(Media other) {
        return getYear() > other.getYear();
    }

    public boolean wasReleasedBeforeOrInSameYear(Media other) {
        return getYear() <= other.getYear();
    }
}

Demo3.java

import java.io.FileNotFoundException;
import java.util.ArrayList;

public class Demo3 
{

    public static void main(String[] args) throws FileNotFoundException {

        ArrayList movies = MovieLoader.loadAllMovies();

        MediaReducer op = new MovieReducerExtremes();

        System.out.println("Newest");
        System.out.println(op.reduce(movies, "Newest"));
        System.out.println("Oldest");
        System.out.println(op.reduce(movies, "Oldest"));
    }
}
Media.java
public abstract class Media {
    private String name;
    private int year;
    private String genre;

    public Media(String n, int y, String g) {
        name = n;
        year = y;
        genre = g;
    }

    public String getName() {
        return name;
    }

    public int getYear() {
        return year;
    }

    public String getGenre() {
        return genre;
    }

    public String toString() {
        return String.format("%5d %-55s %-15s", year, name, genre);
    }

    //if the media was released on or after the year 2000, return New Millennium Era
    //if the media was released on or after the year 1977, return Modern Era
    //if the media was released on or after the year 1955, return Change Era
    //if the media was released on or after the year 1941, return Golden Era
    //in any other situation, return Pre-Golden Era
    public abstract String getEra();

    //return true if this media has a greater release year than the other's
    public abstract boolean wasReleasedAfter(Media other);

    //return true if this media was a lesser or equal release yearn than the other's
    public abstract boolean wasReleasedBeforeOrInSameYear(Media other);
}

MovieLoader.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class MovieLoader {
    public static ArrayList loadAllMovies() throws FileNotFoundException {
        File f = new File("movie_list.txt");
        Scanner inputFile = new Scanner(f);
        ArrayList result = new ArrayList<>();
        while (inputFile.hasNextLine()) {
            String name = inputFile.nextLine();
            int year = inputFile.nextInt();
            //skip new line
            inputFile.nextLine();
            String genre = inputFile.nextLine();
            Media m = new Movie(name, year, genre);
            result.add(m);
        }
        return result;
    }
}

MediaReducer

import java.util.ArrayList;

public interface MediaReducer {
    public String reduce(ArrayList list, String key);
}

A couple from the movie_list.txt

!Next?
1994
Documentary
#1 Single
2006
Reality-TV
#ByMySide
2012
Drama
#Follow
2011
Mystery
#nitTWITS
2011
Comedy
$#*! My Dad Says
2010
Comedy
$1,000,000 Chance of a Lifetime
1986
Game-Show
$100 Makeover
2010
Reality-TV
$100 Taxi Ride
2001
Documentary
$100,000 Name That Tune
1984
Game-Show
$100,000 Name That Tune
1984
Music
$2 Bill
2002
Documentary
$2 Bill
2002
Music
$2 Bill
2002
Music
$2 Bill
2002
Music
$2 Bill
2002
Music
$25 Million Dollar Hoax
2004
Reality-TV
$40 a Day
2002
Documentary
$5 Cover
2009
Drama
$5 Cover: Seattle
2009
Drama
$50,000 Letterbox
1980
Game-Show
$9.99
2003
Adventure
$weepstake$
1979
Drama
' Horse Trials '
2011
Sport
'80s Videos: A to Z
2009
Music
'Allo 'Allo!
1982
Comedy
'Allo 'Allo!
1982
War
'Conversations with My Wife'
2010
Comedy
'Da Kink in My Hair
2007
Comedy
'Da Kink in My Hair
2007
Drama
'More strasti'
2000
Romance
'Ons Sterrenkookboek'
2007
Documentary
'Orrible
2001
Comedy
'Orrible
2001
Crime
'Orrible
2001
Drama
'S ann an Ile
2009
Documentary
'Sang linggo nAPO sila
1995
Game-Show
'Sang linggo nAPO sila
1995
Musical
'T Wilhelmina
1975
Comedy
'Til Death Do Us Part
2006
Crime
'Til Death Do Us Part
2006
Drama
'Til Death Do Us Part
2006
Fantasy
'Til Death Do Us Part
2006
Romance
'Til Death Do Us Part
2006
Thriller
'Til Death
2006
Comedy
'Untold
2004
Documentary
'Wag kukurap
2004
Horror
'Way Out
1961
Drama
'Way Out
1961
Horror
'Way Out
1961
Sci-Fi
'n Shrink
2009
Comedy
't Is maar TV
1999
Comedy
't Is maar TV
1999
Game-Show
't Is maar een spel
2002
Comedy
't Is maar een spel
2002
Game-Show
't Schaep Met De 5 Pooten
1969
Comedy
't Schaep Met De 5 Pooten
2006
Comedy
't Schaep Met De 5 Pooten
2006
Drama
't Zal je gebeuren...
1998
Drama
't Zonnetje in huis
1993
Comedy
(S)truth
1999
Drama
+ Clair
2001
Documentary
+ Emprendedores mi+d
2010
Documentary
+ Investigadores
2008
Documentary
+ de cin�ma
2001
Documentary
+ de cin�ma
2001
News
... ins Gr�ne! Das Stadt-Land-Lust-Magazin
2010
Documentary
... und basta!
2006
Comedy
... und basta!
2006
Music
... und die Tuba bl�st der Huber
1981
Comedy

Solutions

Expert Solution

Here is the completed code for MovieReducerExtremes.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// MovieReducerExtremes.java

import java.util.ArrayList;

public class MovieReducerExtremes implements MediaReducer {

      @Override

      public String reduce(ArrayList list, String key) {

            // checking if the key is newest or oldest

            if (key.equalsIgnoreCase("newest")) {

                  // a variable to store the newest movie

                  Media newest = null;

                  // looping through each Media in list

                  for (int i = 0; i < list.size(); i++) {

                        Media m = (Media) list.get(i);

                        // if newest is not initialized or m is newer than newest,

                        // updating newest

                        if (newest == null || m.getYear() > newest.getYear()) {

                              newest = m;

                        }

                  }

                  // will invoke and return String returned by toString() in Media

                  // class

                  return "" + newest;

            } else if (key.equalsIgnoreCase("oldest")) {

                  Media oldest = null;

                  // looping through each Media in list

                  for (int i = 0; i < list.size(); i++) {

                        Media m = (Media) list.get(i);

                        // if oldest is not initialized or m is older than oldest,

                        // updating oldest

                        if (oldest == null || m.getYear() < oldest.getYear()) {

                              oldest = m;

                        }

                  }

                  return "" + oldest;

            }

            // invalid key

            return null;

      }

}


Related Solutions

Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements. Add the four fields to the Headphone class. Create a constructor that takes two parameters....
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT