Question

In: Computer Science

Hi there, I am having a bit of an issue that I just can't seem to...

Hi there,

I am having a bit of an issue that I just can't seem to figure it out. My code is supposed to read from another file and then have an output of a number for each categorie

////////Required Output///////

Movies in Silent Era: 0\n
Movies in Pre-Golden Era: 7\n
Movies in Golden Era: 581\n
Movies in Change Era: 3445\n
Movies in Modern Era: 10165\n
Movies in New Millennium Era: 15457\n

My program is:

import java.util.ArrayList;

public class MovieReducerEraCount implements MediaReducer
{
    private String result;

    public String reduce(ArrayList<Media> list, String era)
    {
        this.result = result;

        int counter = 0;

        for (int year = 0; year < list.size(); year++)
        {
            
            if (list.get(year).getName().equals(era))

            {
                counter++;
            }
        }

        result = "Movies in " + era + ": " + counter;

        return result;

    }

}

/////////Movie.java//////////

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

    @Override

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

    @Override

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

    @Override

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

///////move_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

I've shorten the file.

Solutions

Expert Solution

/***************************MediaReducer.java*****************************/

import java.util.ArrayList;

public interface MediaReducer {

   public String reduce(ArrayList<Media> list, String era);
}

/***********************************MediaReducerEraCount.java*************************************/

import java.util.ArrayList;

public class MovieReducerEraCount implements MediaReducer {

   public String reduce(ArrayList<Media> list, String era) {

       String result = "";

       int counter = 0;

       for (Media media : list) {

           if (media.getEra().equalsIgnoreCase(era)) {

               counter++;
           }
       }
       result = "Movies in " + era + ": " + counter;

       return result;

   }

}

/*******************************************Media.java**********************************/


public abstract class Media {

   private String name;
   private int year;
   private String genre;

   public Media(String name, int year, String genre) {

       this.name = name;
       this.year = year;
       this.genre = genre;
   }

   public final String getName() {
       return name;
   }

   public final void setName(String name) {
       this.name = name;
   }

   public final int getYear() {
       return year;
   }

   public final void setYear(int year) {
       this.year = year;
   }

   public final String getGenre() {
       return genre;
   }

   public final void setGenre(String genre) {
       this.genre = genre;
   }

   public abstract String getEra();

   public abstract boolean wasReleasedAfter(Media other);

   public abstract boolean wasReleasedBeforeOrInSameYear(Media other);

}
/********************************************Movie.java***********************************/

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

   @Override

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

   @Override

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

   @Override

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

/*******************************************TestMovie.java**********************************/

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

public class TestMovie {

   public static void main(String[] args) {

       ArrayList<Media> list = new ArrayList<>();
       MediaReducer mediaReducer = new MovieReducerEraCount();
       File file = new File("move_list.txt");

       try {

           Scanner sc = new Scanner(file);
           // while loop till file has line
           while (sc.hasNextLine()) {
               // read the line
               String name = sc.nextLine();
               int year = Integer.parseInt(sc.nextLine());
               String genre = sc.nextLine();

               list.add(new Movie(name, year, genre));

           }
           sc.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }

       System.out.println(mediaReducer.reduce(list, "Silent Era"));
       System.out.println(mediaReducer.reduce(list, "Pre-Golden Era"));
       System.out.println(mediaReducer.reduce(list, "Golden Era"));
       System.out.println(mediaReducer.reduce(list, "Change Era"));
       System.out.println(mediaReducer.reduce(list, "Modern Era"));
       System.out.println(mediaReducer.reduce(list, "New Millennium Era"));

   }

}

/*********************************************output***************************/

Movies in Silent Era: 0
Movies in Pre-Golden Era: 0
Movies in Golden Era: 0
Movies in Change Era: 0
Movies in Modern Era: 8
Movies in New Millennium Era: 21

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
Hi, I am doing an experiment that is called NaBH4 reduction of acetophenone. I am having...
Hi, I am doing an experiment that is called NaBH4 reduction of acetophenone. I am having trouble understand the techinque/wording when using a sep funnel. These are some of the procedures below: 1. Pour the mixture into a separatory funnel and add 20 mL of ice water. Rinse the beaker with additional ice water (5 mL) and add the rinsing to the separatory funnel. Rinise the beaker with ether (2 X 15 mL), adding the ether to the separatory funnel....
Hi there I need to write an ethic issue in technology but I am struggling to...
Hi there I need to write an ethic issue in technology but I am struggling to choose a topic. I am asking about the possibility of "Ethical issues" topics that is tread common in technology. Would anyone can give me some options to choose from. Thank you.
Hi! I am having a difficult time thinking of a topic for my research paper if...
Hi! I am having a difficult time thinking of a topic for my research paper if anyone is able to help out that would be appreciated! One challenge is defining a specific topic. For example, the topic "Federal Reserve" is far too broad. Here are considerations and suggestions for you in defining your research topic: 1. Start by review the major topics of the course. See how the textbook is organized, and flip through the various chapters to identify potential...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well. Here is the code #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)...
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
This isn't a homework but since I can't seem to find any answers for it, I...
This isn't a homework but since I can't seem to find any answers for it, I would like to receive some insights from you experts! So, I heard that UK has official left the European Union on Jan 31st (aka Brexit). Are there any trade deals that have been signed? or any negotiations that has been reached? please provide me with some updates I can't seem to find any info on which trade deals were signed and etc. thanks! p.s...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT