In: Computer Science
JAVA programming - please answer all prompts as apart of 1 java assignment.
Part A
Create a java class InventoryItem which has
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.
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