Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to solve these questions in java. I have attached a code that I want to solve by modifying this code import javax.swing.JOptionPane; //Main

I want to solve these questions in java. I have attached a code that I want to solve by modifying this code

image text in transcribedimage text in transcribed

import javax.swing.JOptionPane; //Main Class public class MainClass { public static void main(String[] args) { // Creating object myPlatform of Platform Platform myPlatform = new Platform(); String input = ""; // variable to store input from user while (!input.equals("5")) { // check if input is not 5 (Exit) // Showing prompt to enter input input = JOptionPane.showInputDialog( "What do you want to do? 1.Add show 2.Search shows 3.Print movies 4.Print Series 5.Exit"); // switch case to perform operation based on input switch (input) { case "1": myPlatform.addShow(); // calling method to add show break; case "2": myPlatform.searchShows(); // calling method to search shows break; case "3": myPlatform.printMovies(); // calling method to print movies break; case "4": myPlatform.printSeries(); // calling method to print series break; case "5": // Program will exit break; default: // For all other options wrong input message is showed JOptionPane.showMessageDialog(null, "Wrong input"); } } } } 

Movie.java

import javax.swing.JOptionPane; //Class Movie inheriting Show class public class Movie extends Show { // Attributes of class private double duration; private int productionYear; // no-args constructor of class public Movie() { // Getting input from user using JOptionPane // Getting duration as input and parsing it to double duration = Double.parseDouble(JOptionPane.showInputDialog("Enter movie duration: ")); // Getting productionYear as input and parsing it to integer productionYear = Integer.parseInt(JOptionPane.showInputDialog("Enter production year: ")); } // parameterized constructor public Movie(String name, double duration, int productionYear) { super(name); this.duration = duration; this.productionYear = productionYear; } // Getter method of Duration public double getDuration() { return duration; } // Getter method of production year public int getProductionYear() { return productionYear; } // toString method of the class public String toString() { return super.toString() + " " + duration + " - " + productionYear; } // Overriding equals method @Override public boolean equals(Object obj) { // calling parent equals method, checking production year are eqaul return super.equals(obj) && (productionYear == ((Movie) obj).getProductionYear()); } } 

Platform.java

import java.util.ArrayList; import javax.swing.JOptionPane; //Class Platform public class Platform { // ArrayList to store shows private ArrayList shows; // no-args constructor public Platform() { // initializing shows to empty arraylist shows = new ArrayList(); } // Method to add shows public void addShow() { // Getting input from user String option = JOptionPane.showInputDialog("What type of show do you want to add? 1.Movie 2.Series"); switch (option) { case "1": shows.add(new Movie()); // adding new object of movie to arraylist break; case "2": shows.add(new Series()); // adding new object of series to arraylist break; default: JOptionPane.showMessageDialog(null, "Wrong Input"); // wrong input message for invalid input } } // Method to search shows public void searchShows() { // Getting showname from user String showName = JOptionPane.showInputDialog("Enter name of show you want to find: "); String output = "Could not find show with title " + showName; // initializing output message // Iteratiing over arraylist to find show with matching input for (Show show : shows) { if (show.name.equalsIgnoreCase(showName)) { // checking if name are equal output = show.toString(); // show is converted to string break; } } JOptionPane.showMessageDialog(null, output); // output is showed using message dialog } // Method to print movies public void printMovies() { String output = ""; // variable to keep output message for (Show show : shows) { // iterating over shows if (show instanceof Movie) { // checking if show is an instance of Movie output += show.toString() + " "; // storing string value of show to output variable } } JOptionPane.showMessageDialog(null, output);// output is showed using message dialog } public void printSeries() { String output = ""; // variable to keep output message for (Show show : shows) {// iterating over shows if (show instanceof Series) { // checking if show is an instance of Series output += show.toString() + " ";// storing string value of show to output variable } } JOptionPane.showMessageDialog(null, output); // output is showed using message dialog } } 

Series.java

import javax.swing.JOptionPane; public class Series extends Show { private int numOfEpisodes; private double episodeDuration; // no-arg constructor public Series() { // Getting input from user using JOptionPane numOfEpisodes = Integer.parseInt(JOptionPane.showInputDialog("Enter number of episodes: ")); episodeDuration = Double.parseDouble(JOptionPane.showInputDialog("Enter average episode duration: ")); } // parameterized constructor public Series(String name, int numOfEpisodes, double episodeDuration) { super(name); this.numOfEpisodes = numOfEpisodes; this.episodeDuration = episodeDuration; } // Getter of numOfEpisodes public int getNumOfEpisodes() { return numOfEpisodes; } // Getter of episodeDuration public double getEpisodeDuration() { return episodeDuration; } // toString method to print series as string @Override public String toString() { return super.toString() + " " + numOfEpisodes + " - " + episodeDuration; } } 

Show.java

import java.util.Date; import javax.swing.JOptionPane; //Class Show public class Show { // Attributes of class public String name; private int timesWatched; private double rate; private static int maxRate = 5; private Date dateAdded; // Date variable // no-args constructor public Show() { // Getting input from user using JOptionPane name = JOptionPane.showInputDialog("Enter show name: "); timesWatched = Integer.parseInt(JOptionPane.showInputDialog("Enter number of times watched: ")); rate = Double.parseDouble(JOptionPane.showInputDialog("Enter rate: ")); dateAdded = new Date(); // dateAdded is initialized using default Date constructor } // parameterized constructor public Show(String name) { this.name = name; } // Getter of rate public double getRate() { return rate; } // Getter of timesWatched public double getTimesWatched() { return timesWatched; } // Getter of maxRate public static int getMaxRate() { return maxRate; } // Getter of dateAdded public Date getDateAdded() { return dateAdded; } // Method to updateRate public void updateRate() { // Getting input from user using JOptionPane double userRate = Double .parseDouble(JOptionPane.showInputDialog("Enter you rate from 0 to " + maxRate + " for " + name)); double sum = rate * timesWatched + userRate; timesWatched++; rate = sum / timesWatched; } // Overriding toString method @Override public String toString() { // dateAdded is also printed using toString method return name + " - " + rate + " - " + timesWatched + " - " + dateAdded.toString(); } // Overriding equals method @Override public boolean equals(Object obj) { return rate == ((Show) obj).getRate(); // checking if rate is equal } }
Lab Objectives: Apply the concept of abstract classes and methods. Define and implement interfaces. Use methods of the String class to perform operations on Strings. * Use the classes Show, Movie, Series, and Platform that you implemented in Labs, and perform the following modifications which are shown in this UML diagram: > Ratable + updateRate():double > java.lang. Comparable +compareTo(o:Show):int Platform - shows: ArrayList Show + name: String - times Watched: int - rate: double -max Rate: int = 5 - date Added: java.util.Date + Platform + addShow(): void + searchShows(): void + printMovies(): void + printSeries(): void + updateRate():double + print Top 100): void + watchAndUpdate(): void + Show + Show(name:String) + getRate(): double + getTimes Watched():int + getMax Rate(:int + getDate Added(): java.util.Date + updateRate():double + toString(): String + equals(o: Object): boolean + compare To(o: Show):int + getDuration(): double 4 Movie Series - duration: double - production Year: int - numOfEpisodes: int - episodeDuration: double + Movie + Movie(name:String, duration:double, production Year: int) + getDuration(): double + getProduction Year(): int + toString(: String + equals(o: Object): boolean + Series + Series(name:String, numOfEpisodes:int, episode Duration: int) + getNumOfEpisodes(): int +getEpisodeDuration): double + toString():String + get Duration(): double 1. Modify the Show class such that it implements the Comparable interface of java.lang. Remember to add the concrete type Show between when you implement Comparable. Note the syntax error that occurs and understand why it occurs. (0.5 point) 2. Implement (override) the compare To method in the Show class such that it compares the shows according to their rates: returns 1 if the first show's rate is greater than the second, -lif the first show's rate is less than the second, and 0 if they are equal. (First show is this", second show is o) (1 point) 3. Define an interface named Ratable, that has the method updateRate as shown in the UML diagram, in a file named Ratable.java. (1point) 4. Modify the Show class such that it implements the Ratable interface. Note the syntax error that occurs in the updateRate method and understand why it occurs. (0.5 point) 5. Modify the update Rate method in the Show class such that it returns a double value that represents the updated rate. (0.5 point) 6. Modify the Platform class such that it implements the Ratable interface. Note the syntax error that occurs and understand why it occurs. (0.5 point) 7. Implement (override) the updateRate method in the Platform class such that it returns the average of the rates of all shows in the platform. (1 point) 8. Define the abstract method getDuration() in the Show class as shown in the UML diagram. Note the syntax error that occurs and understand why it occurs. (0.5 point) 9. Re-define the Show class to be abstract. Does this solve the syntax error that occurred in the previous step? Note the syntax error that occurs now in the Series class and understand why it occurs. Why doesn't the same error occur in the Movie class. (0.5 point) 10. Implement (override) the getDuration method in the Series class such that it returns the multiplication of the numOfEpisodes by the episodeDuration. (0.5 point) 11. In the Platform class, implement the method print Top10 which prints the top 10 shows in the platform according to their rates in a message dialog box. If the number of shows in the platform is less than or equals 10, it must print names and rates of the available shows ordered according to their rates from the highest rated to the least rated each on a line. If the number of shows in the platform is greater than 10, names and rates of the ten shows with the highest rates must be printed from the highest rated to the least rated each on a line. This method must use the sort method of the Arrays class. (1 point) 12. Modify the search Shows method such that it asks the user to enter the name of the show he wants to search for or part of it. The search must be performed to find all the shows whose names contain the entered String. Names (only) of these shows must be displayed in a message dialog box each on a new line. (1 point) 13. Add the method watch And Rate to the Platform class as shown in the UML diagram. The method must ask the user to enter the name of the show he wants to watch, search for the show (assume that the user may not enter the name with case sensitivity), and invoke the update Rate method for that show. If no show with the entered name is found, you must print the following message in a message dialog box "Sorry! Could not find show with title: ....". (1 point) 14. Add the two options: Watch and Rate, Show Top 10 to the options provided by the menu in the main method. For each option invoke the appropriate method. Your menu must look like the one below: (0.5 point) Input ? What do you want to do? 1. Add show 2. Search shows 3.Print movies 4.Print Series 5.Watch and rate 6.Show Top 10 7.Exit OK Cancel

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

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions