Question: Part 1: Inheritance, polymorphism and interfaces A new test client is included to help you work through the necessary changes to the Catalog class. File
Part 1: Inheritance, polymorphism and interfaces
A new test client is included to help you work through the necessary changes to the Catalog class. File name is Lab7TestClient.java. You will be creating a new client application once the classes function as desired.
Service classes:
Modify the CatalogItem class such that it is abstract. This will prevent instantiation of objects directly from the class. The class will serve as a super class to three new sub classes: Book, Film, and Music. By extending the CatalogItem class, these three classes will inherit the public fields and methods of the CatalogItem class. The classes should also define the following new fields unique to each class: (the inherited description field will serve as the title for each item.)
Book: publisher, author
Film: studio, actors
Music: producer, musician, genre
For each sub class, define accessor and mutator methods for the fields unique to the class. Each sub class should also define a constructor that accepts all fields (the three inherited fields plus the fields unique to the sub class) and a copy constructor.
The Catalog class must implement the given Lab7Interface. When an interface is implemented, the method signatures noted in the interface must be implemented within the class.
Examine the interface. You will notice that the methods from the previous lab are contained within the interface with the exception of the add methods. The add methods have been replaced and several new methods have been added. In order for the code to compile, ALL methods of the interface must be implemented. Add each new method using the signature given in the interface. Leave the body of each method blank. For example,
public void displayFilms() { // TO DO }
Comment out the code contained in the getCatalogItem method, leaving the 'return null' statement:
public CatalogItem getCatalogItem(int id) { return null; }
The code should compile and test 1 of the test client should function correctly.
Replace the add methods from the previous lab with the following three methods: addBook, addFilm, and addMusic. Each add method will accept all fields for the item to be added. This includes the itemId, description (title), cost, and the distinct fields for each class type. After implementing the methods, try test 2 of the test client.
Add a method to display only Books, a method to display only Films, and a method to display only Music. The instanceof operator can be used to determine if an object instance is of a particular class:
Cat myCat= new Cat("Snow White","Domestic Short Hair","F"); if (myCat instanceof Cat) // will be true if (myCat instanceof Dog) // will be false
Correct code will allow test 3 of the test client to function correctly.
The getCatalogItem method of the Catalog class must create a new instance of a Book, a Film or Music item to be returned to the caller. Modify the method to create the desired sub class type of the new object to be returned. Hint, use type casting.
Client application:
Develop a menu-driven application for working with a Catalog of Books, Films, and Music items. The end-user should be able to add new items, display items and update items. After each operation, re-display the main menu and continue until the end-user requests to exit. Use static methods to organize your code.
For this part of the lab, the end-user will only enter legal data (valid menu options and numbers and strings as needed.)
Example run: Click HERE
Part 2: Exception Handling
Modify the client application to handle InputMismatchExceptions. These exceptions occur when the end-user enters data of the wrong type. For example, an InputMismatchException will occur on a nextInt Scanner method call when the end-user enters character data. The exception should be handled by the program by displaying an error message and allowing the end-user the start over. The program should not abort.
Develop a class named CatalogErrorException to define a custom exception handler. This class should extend the Exception class and implement two constructors: a default (no parameter) constructor and a constructor that accepts a string message.
Modify the update methods of the Catalog class such that they throw an instance of the CatalogErrorException class when the id passed as a parameter is not found in the catalog. Be sure to change the method signature in the interface! Update the application to handle this exception.
Also update the methods of the application to throw a CatalogErrorException when an invalid option is selected from a menu and add the code necessary to handle this exception in the main method.
Example run: Click HERE
Completion:
When you have completed your work, place all files in a compressed folder. Return to this lab and click the Submit Assignment link displayed in the Sidebar on the right hand side of the Canvas page. This will present a File Upload box at the bottom of the page. Click the Chose File button to select and upload the compressed folder containing your work.
The assignment is due no later than midnight (11:59 PM) of the date given.
code already completed...
***********CatalogItem Class:***********
public class CatalogItem { private int itemID; private String description; private double priceValue; public CatalogItem() { this.itemID = 0; this.description = ""; this.priceValue = 0; } public CatalogItem(int itemID, String description, double priceValue) { this.itemID = itemID; this.description = description; this.priceValue = priceValue; } public CatalogItem(CatalogItem catalogItem) { this.itemID = catalogItem.itemID; this.description = catalogItem.description; this.priceValue = catalogItem.priceValue; } public int getItemId() { return itemID; } public String getDescription() { return description; } public double getPriceValue() { return priceValue; } public void setDescription(String description) { this.description = description; } public void setPriceValue(double priceValue) { this.priceValue = priceValue; } @Override public String toString() { return "Item: " + itemID + ", " + description + ". Price: $" + String.format("%.2f", priceValue); } }
***********Catalog Class:***********
import java.util.ArrayList; public class Catalog { private ArrayList ***********Lab7Interface:*********** //The Lab7Interface specifies the method signatures of the Catalog class public interface Lab7Interface { // Methods that are needed from Lab 6 public void display(); public void priceIncrease(double priceInc); public String toString(); public CatalogItem getCatalogItem(int id); // Methods that are needed from Lab 6 and Part 1 of Lab 7 public void update(int id, String newDescription); public void update(int id, double newPrice); // Comment out the above and use the version below for part 2 of Lab 7 // public void update(int id, String newDescription) throws CatalogErrorException; // public void update(int id, double newPrice) throws CatalogErrorException; // New methods added in Lab 7 (Note, the identifiers of the parameters may be different in the implementing class.) // The 'description' and 'title' are the same field public void addFilm(int id, String title, double price, String studio, String actor); public void addBook(int id, String title, double price, String publisher, String author); public void addMusic(int id, String title, double price, String producer, String musician, String genre); public void displayFilms(); public void displayBooks(); public void displayMusic(); } ***********Lab7TestClient:*********** //Test Client for testing the Catalog class public class Lab7TestClient { public static void main(String[] args) { // TEST 1 // Create catalog and test the static getCount method System.out.println("Class has not been created. There are " + Catalog.count + " items."); Catalog myCatalog = new Catalog(); System.out.println(" Catalog class created. "); myCatalog.display(); Book b = new Book(123, "Programmers Rule", 12.25, "Student Press", "Karel"); Film f = new Film(234, "Just a Little Byte", 4.99, "CollegeTown Studios", "Justane"); Music m = new Music(567, "My Printer Broke and Nobody Cares", 1.29, "Songs of the South", "Robins", "Country"); System.out.println(" The following objects were created: " + b.toString() + " " + f.toString() + " " + m.toString()); /****** Move this line down to the next Test as you progress in completing the Lab // TEST 2 // Test the add method (version 1) myCatalog.addBook(123, "Programmers Rule", 12.25, "Student Press", "Karel"); myCatalog.addFilm(234, "Just a Little Byte", 4.99, "CollegeTown Studios", "Justane"); myCatalog.addMusic(567, "My Printer Broke and Nobody Cares", 1.29, "Songs of the South", "Robins", "Country"); myCatalog.display(); // TEST 3 // Test the display methods myCatalog.displayBooks(); myCatalog.displayFilms(); myCatalog.displayMusic(); // TEST 4 // Test the getCatalogItem method CatalogItem item4 = myCatalog.getCatalogItem(123); if (item4 != null) System.out.println(" Found item 123: " + item4.toString()); else System.out.println(" Item 123 not found."); // */ } } Show transcribed image text
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
