Question

In: Computer Science

JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...

JAVA programming - please answer all prompts as apart of 1 java assignment.

Part A

Create a java class InventoryItem which has

  • a String description
  • a double price
  • an int howMany

Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one.

Write a clone method that uses the copy constructor to create a copy. Create similar clone methods in all classes in this assignment.

Write a toString for this class that returns something like "Footo the Wonder Boot Exploder ($22.99)" (leave out howMany)

Also write an equals method for this class. InventoryItems can only be equal to other InventoryItems, and only if they have the same price and description (even if howMany is different). Note how the equals method agrees with the copy constructor about what it means for two InventoryItems to be the same.

Add a method view(), that prints something like "Viewing: Footo the Wonder Boot Exploder"

In a harness class with a main, create several InventoryItems, clone them, and check that equals works properly.

Part B

Create a class Book which inherits from InventoryItem and also has a String author (Book will use description to hold the book's title). toString for this class will return something like "Book: The Curse of the Flying Wombat by Constance deCoverlet ($12.95)".

For Book, override view() to print something like "Opening Book Exerpt: The Curse of the Flying Wombat"

Also override equals to require author is the same, in addition to the requirements in the superclass (chain the equals methods together).

Create a class MusicCD which inherits from InventoryItem and also has a String performer (it will use description to hold the CD's title). toString for this class will return something like "CD: Tommy Gnosis: Greatest Hits ($18.65)"

For MusicCD override view() to print something like "Now Playing Sample: Greatest Hits".

Also override equals to require performer is the same, in addition to the requirements in the superclass.

In your main, create more InventoryItem variables, but point them at a Book and a MusicCD. Use clone to make copies of each type and make sure this works. Check that equals works properly.

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

InventroryItems

class InventoryItem implements Cloneable{
        // instance variables
        protected String description;
        protected double price;
        protected int howMany;

        // constructor
        public InventoryItem(String description, double price, int howMany) {
                this.description = description;
                this.price = price;
                this.howMany = howMany;
        }

        // copy constructor
        public InventoryItem(InventoryItem obj) {
                this.description = obj.description;
                this.price = obj.price;
                howMany = 1;
        }

        // clone method
        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }

        // toString method
        @Override
        public String toString() {
                return description + " ($" + price + ")";
        }

        // equals method
        @Override
        public boolean equals(Object obj) {
                InventoryItem other = (InventoryItem) obj;
                if (description.equals(other.description) && price == other.price)
                        return true;
                return false;
        }

        // view method
        public void view() {
                System.out.println("Viewing: " + description);
        }
}

Book

class Book extends InventoryItem {
        private String author;

        public Book(String description, double price, int howMany, String author) {
                super(description, price, howMany);
                this.author = author;
        }

        public Book(Book obj) {
//              super(new InventoryItem(obj.description, obj.price, obj.howMany));
                super(obj);
                this.author = obj.author;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }

        @Override
        public String toString() {
                return "Book: " + description + " by " + author + " ($" + price + ")";
        }

        @Override
        public void view() {
                System.out.println("Openeing Book: " + description);
        }

        @Override
        public boolean equals(Object obj) {
                Book other = (Book) obj;
                if (super.equals(obj) && author.equals(other.author))
                        return true;
                return false;
        }
}

MusicCD

class MusicCD extends InventoryItem{
        private String performer;

        public MusicCD(String description, double price, int howMany, String performer) {
                super(description, price, howMany);
                this.performer = performer;
        }
        
        public MusicCD(MusicCD obj) {
                super(obj);
                this.performer = obj.performer;
        }
        
        @Override
        public String toString() {
                return "CD: "+performer+": "+super.toString();
        }
        
        @Override
        public void view() {
                System.out.println("Now Playing Sample: "+description);
        }
        
        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }
        
        @Override
        public boolean equals(Object obj) {
                MusicCD other = (MusicCD) obj;
                if(super.equals(obj) && performer.equals(other.performer))
                        return true;
                return false;
        }
}

Test

public class Test{
        public static void main(String[] args) throws CloneNotSupportedException {
                //testing the classes
                InventoryItem book1 = new Book("Somthing Nice", 2.56, 5, "John Wick");
                InventoryItem book2 = (InventoryItem) book1.clone();
                InventoryItem book3 = new Book((Book)book2);
                
                InventoryItem cd1 = new MusicCD("Classic Hits", 3.26, 6, "Elvis Presley");
                InventoryItem cd2 = (InventoryItem) cd1.clone();
                InventoryItem cd3 = new MusicCD((MusicCD)cd2);
                
                System.out.println("book2.equals(book3)? "+book2.equals(book3));
                System.out.println("cd1.equals(cd3)? "+cd1.equals(cd3));
        }
}

Output


Related Solutions

JAVA programming- please answer all prompts as apart of the same java project Enums enumeration values()...
JAVA programming- please answer all prompts as apart of the same java project Enums enumeration values() and other utilities Part A Create an enumeration AptType representing several different types of apartment. Each type of apartment has a description, a size (square footage), and a rent amount. In the enumeration, provide public final instance variables for these. Also write a parameterized constructor that sets the values of these variables (doing this is just part of seeing up the enum) The apartment...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT