JAVA Programming Assignment
Problem 1: Toy and ToyInventory
Robert wants to get organized with a system to keep track of his toys. Implement two classes to help him: Toy and ToyInventory.
Toy must include the following public methods:
● Toy(String name, String description, boolean destroyed) // Constructor that constructs the Toy object
○ name: name of the toy ○ description: description of the toy ○ destroyed: whether or not this toy is destroyed
● String getName() // returns name ● void setName(String name) // sets the name to the given name ● String getDescription() // returns description ● void setDescription(String description) // sets the description to the given
description ● boolean isDestroyed() // Returns true if the toy is destroyed, false otherwise ● void setDestroyed(boolean destroyed) // sets the destroyed property to the
given value
ToyInventory must include the following public methods:
● ToyInventory() // Constructor that constructs a new ToyInventory object ● void addToy(Toy toy) // adds the given toy if a toy with that name doesn't
already exist in the inventory ● void removeToy(String name) // removes the toy with the given name if it
exists in the inventory ● String getInventoryReport() // returns a String that lists the name, description,
and DESTROYED or NOT DESTROYED for each toy (see example for formatting)
Example:
Toy lamby = new Toy("Lamby", "white fluffy stuffed lamb that I got for Christmas", false); Toy sharky = new Toy("Sharky", "blue shark with its mouth chewed off", true); Toy socky = new Toy("Socky", "my brother's old sock that I like to carry around in my mouth like a treasure", true); Toy tennisBall = new Toy("Tennis Ball", "standard green tennis ball that I play with outside", false); ToyInventory inventory = new ToyInventory();
inventory.addToy(lamby); inventory.addToy(sharky); inventory.addToy(socky); inventory.addToy(tennisBall); inventory.removeToy("Sharky"); inventory.getInventoryReport(); // returns: Lamby white fluffy stuffed lamb that I got for Christmas NOT DESTROYED Socky my brother's old sock that I like to carry around in my mouth like a treasure DESTROYED Tennis Ball standard green tennis ball that I play with outside NOT DESTROYED
Problem 2: BasicAccount
Given the class BankAccount (BankAccount.java ), implement a subclass of BankAccount called BasicAccount whose withdraw method charges a penalty of $30 for each withdrawal that results in an overdraft (a negative balance).
Example:
BankAccount account = new BasicAccount(100.00) // creates a BasicAccount with an initial balance of 100.00
account.withdraw(80.00); // no overdraft, so no penalty
account.getBalance(); // returns 20.0
account.withdraw(50.00); // charge $30 penalty for overdraft
account.getBalance(); // returns -60.0
account.withdraw(50.00); // charge $30 penalty for overdraft
account.getBalance(); // returns -140.0
Problems 3: Library
For this assignment, you are given an abstract class LibraryItem (LibraryItem.java ). You must complete the following:
1. Add an equals method to LibraryItem 2. Implement three direct subclasses of LibraryItem: DVD (a concrete class),
Magazine (a concrete class), and Book (an abstract class). 3. Implement three concrete subclasses of Book: DigitalBook, PrintBook, and
Audiobook.
Each item type has a loan period and a maximum number of checkouts per item (i.e., a physical item like a print book can only be checked out by one person at a time, whereas a digital item like an audio book can be checked out by multiple people at a time, depending on the number of licenses the library has for that item). This information is given in the below table:
● LibraryItem public method to add: ○ boolean equals(Object otherObject) // returns true if otherObject
has the same instance variable(s) value(s) as this LibraryItem ● DVD public methods:
○ DVD(String title, ArrayList<String> actors) // Constructs a DVD object with the given title and actors
○ ArrayList<String> getActors() ○ void setActors(ArrayList<String> actors) ○ String checkOut() // if this item is not already checked out, this
method checks this item out and returns the loan period; if it is already checked out, returns the String "NOT ALLOWED"; overrides LibraryItem.checkOut()
○ boolean equals(Object otherObject) // returns true if otherObject has the same instance variable(s) value(s) as this DVD
● Magazine public methods:
Item Type Loan Period Max # checkouts per item
DVD 7 days 1
Magazine 7 days 1
Digital Book 14 days 3
Print Book 21 days 1
Audio Book 28 days 2
○ Magazine(String title, int issueNumber, String publicationDate) // Constructs a Magazine object with the given title, issue number, and publication date
○ int getIssueNumber() ○ void setIssueNumber(int issueNumber) ○ String getPublicationDate() ○ void setPublicationDate(String publicationDate) ○ String checkOut() // if this item is not already checked out, this
method checks this item out and returns the loan period; if it is already checked out, returns the String "NOT ALLOWED"; overrides LibraryItem.checkOut()
○ boolean equals(Object otherObject) // returns true if otherObject has the same instance variable(s) value(s) as this Magazine
● Book (abstract class) public methods: ○ Book(String title, String author) // Constructs a Book object with the
given title and author ○ String getAuthor() ○ void setAuthor(String author) ○ boolean equals(Object otherObject) // returns true if otherObject
has the same instance variable(s) value(s) as this Book ● DigitalBook public methods:
○ DigitalBook(String title, String author, int numPages) // Constructs a DigitalBook object with the given title, author, and number of pages
○ int getNumPages() ○ void setNumPages(int numPages) ○ String checkOut() // if the max number of checkouts for this item
has not already been reached, this method checks this item out and returns the loan period; if no more check outs are available for this item, returns the String "NOT ALLOWED"; overrides LibraryItem.checkOut()
○ void checkIn() // checks in this item (frees up one checkout for this item); overrides LibraryItem.checkIn()
○ boolean equals(Object otherObject) // returns true if otherObject has the same instance variable(s) value(s) as this DigitalBook
● PrintBook public methods: ○ PrintBook(String title, String author, int numPages) // Constructs a
PrintBook object with the given title, author, and number of pages ○ int getNumPages() ○ void setNumPages(int numPages)
○ String checkOut() // if this item is not already checked out, this method checks this item out and returns the loan period; if it is already checked out, returns the String "NOT ALLOWED"; overrides LibraryItem.checkOut()
○ boolean equals(Object otherObject) // returns true if otherObject has the same instance variable(s) value(s) as this PrintBook
● AudioBook public methods: ○ AudioBook(String title, String author, double playingTime) //
Constructs an AudioBook object with the given title, author, and playing time (in hours)
○ double getPlayingTime() ○ void setPlayingTime(double playingTime) ○ String checkOut() // if the max number of checkouts for this item
has not already been reached, this method checks this item out and returns the loan period; if no more check outs are available for this item, returns the String "NOT ALLOWED"; overrides LibraryItem.checkOut()
○ void checkIn() // checks in this item (frees up one checkout for this item); overrides LibraryItem.checkIn()
○ boolean equals(Object otherObject) // returns true if otherObject has the same instance variable(s) value(s) as this AudioBook
Example: ArrayList<String> actors = new ArrayList<String>(); actors.add("Amy Adams"); actors.add("Glenn Close"); actors.add("Haley Bennett"); actors.add("Gabriel Basso"); DVD dvd1 = new DVD("Hillbilly Elegy", actors); Magazine mag1 = new Magazine("Time", 435, "February 2021"); DigitalBook db1 = new DigitalBook("Hillbilly Elegy", "J.D. Vance", 264); PrintBook pb1 = new PrintBook("Hillbilly Elegy", "J.D. Vance", 264); PrintBook pb2 = new PrintBook("The Warmth of Other Suns", "Isabel Wilkerson", 622); PrintBook pb3 = new PrintBook("Caste", "Isabel Wilkerson", 496); AudioBook ab1 = new AudioBook("Hillbilly Elegy", "J.D. Vance", 6.8); AudioBook ab2 = new AudioBook("Hillbilly Elegy", "J.D. Vance", 6.8); ab1.equals(ab2); // returns true dvd1.equals(db1); // returns false pb2.equals(pb3); // returns false
LibraryItem[] items = new LibraryItem[8]; items[0] = dvd1; items[1] = mag1; items[2] = db1; items[3] = pb1; items[4] = pb2; items[5] = pb3; items[6] = ab1; items[7] = ab2; for (LibraryItem item : items) { System.out.println(item.checkOut()); } // =========================== // above should print: // 7 days // 7 days // 14 days // 21 days // 21 days // 21 days // 28 days // 28 days System.out.println(items[6].checkOut()); // 28 days System.out.println(items[6].checkOut()); // NOT ALLOWED items[6].checkIn(); System.out.println(items[6].checkOut()); // 28 days