Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Item { private String itemName; private double value; } // one argument constructor using value attribute public Item(double value) { this.value =

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

public class Item { private String itemName; private double value; } // one argument constructor using value attribute public Item(double value) { this.value = value; } // two argument constructor public Item(String name, double value) { this.itemName = name; this.value = value; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public double getValue() { return value; } public void setValue(double value) { this.value = value; } this.value; } public String toString() { "I String result = "Item name: + this.itemName + " Item value: + return result; 1 11 O ZOOM + import java.util.ArrayList; // This class CANNOT be instantiated. // (You can not create objects of this class.) public interface ItemCollection { } // Abstract Methods // (Their implementations are not specified.) public double returnCollectionValue(); public void printItemList(); public void add(Item e); public void combineCollections (ArrayList newItems); 3 public class CollectionDriver1 { public static void main(String[] args) { System.out.println("This is a record of all the items of value I own:"); } } Item item1 = new Item("Computer", 500); System.out.println(item1.toString()); Item item2 = new Item("Bike", 300); System.out.println(item2.toString()); Item item3 = new Item("Phone", 200); System.out.println(item3.toString()); Item item4 = new Item("Guitar", 500); System.out.println(item4.toString()); Item item5 = new Item("Book", 10); System.out.println(item5.toString()); // For PART 1: Add some more items here! 2 import java.util.ArrayList; public class MyItemCollection implements ItemCollection{ private ArrayList items; } public MyItemCollection() { } @Override public double returnCollectionValue() { double collectionValue = 0; for (Item i: this.items) { } this.items = new ArrayList (); } @Override // This method should print the names of the items in the collection, // with one item name per line. public void printItemList() { } collectionValue += i.getValue(); } return collectionValue; @Override public void add(Item e) { this.items.add(e); } // PART 2.1: YOUR CODE HERE! @Override public void combineCollections (ArrayList newItems) { for (Item i: newItems) { this.items.add(i); } } public ArrayList getItems() { return items; public void setItems (ArrayList items) { this.items - items; 4 public class CollectionDriver2 { public static void main(String[] args) { MyItemCollection myBooks = new MyItemCollection(); Book book1 = new Book ("The Phantom Tollbooth", "Juster"); Book book2 = new Book ("Where the Wild Things Are", "Maurice", 5 "Sendak"); } } Book book3 = book2; myBooks.add(book1); myBooks.add(book2); myBooks.add(book3); myBooks.printItemList(); System.out.println(book3 == book2); System.out.println(book3.equals(book2)); // This class inherits from the Item superclass. public class Book extends Item{ private String title; private String authorLast; private String authorFirst; private int numPages; public Book (String title, String authorFirst, String authorLast, double value, int numPages) { super (value); // Note the use of super here this.title = title; this.author First = author First; this.author Last = = author Last; this.numPages = numPages; } public value) { } public Book (String title, String authorFirst, String author Last) { super (0); Book (String title, String authorFirst, String authorLast, double super (value); this.title = title; this.author First = author First; this.author Last = author Last; this.setNumPages(-1); } } public Book(String title, String authorLast) { super (0); this.title = title; this.author First = author First; this.author Last = author Last; this.setNumPages (-1); } this.title = title; this.author First = "unknown"; this.author Last = author Last; this.setNumPages(-1); public String getTitle() { return title; 6A } public void setAuthorLast(String authorLast) { this.author Last = author Last; } } public String getAuthorFirst() { return author First; } public void setAuthor First(String author First) { this.author First = author First; } public int getNumPages() { return numPages; } public void setNumPages(int numPages) { this.numPages = numPages; } // HW Problem 2.2 // Override the equals() method below I have the determination based on the following: For 2 book objects to be equal, the following must be true: // 1) The two books must have the same title AND // 2) The two book authors must have the same author LAST NAME. // Write your method so that the titles and author names do not have to match upper/lower case exactly. ---- } 6C ---- //(i.e. Ignore case) public boolean equals(Book b) { // PART 2.2: YOUR CODE HERE! return false; import java.util.ArrayList; public class CollectionDriver3 { public static void main(String[] args) { "Sendak"); } } MyItemCollection myBooks = new MyItemCollection(); Book book1 = new Book ("The Phantom Tollbooth", "Juster"); Book book2 = new Book ("Where the Wild Things Are", "Maurice", Book book3 = book2; Book book4= new Book ("Where the Wild Things Are", "Sendak"); myBooks.add(book1); myBooks.add(book2); myBooks.add(book3); myBooks.add(book4); System.out.println(book3 == book2); System.out.println(book3.equals(book2)); System.out.println(book3.equals(book4)); System.out.println(book3 == book4); 7 import java.util.ArrayList; public class CollectionDriver4 { public static void main(String[] args) { "Sendak"); "Herriot"); } } MyItemCollection myBooks = new MyItemCollection(); Book book1 = new Book ("The Phantom Tollbooth", "Juster"); Book book2 = new Book ("Where the Wild Things Are", "Maurice", Book book3 = new Book ("All Creatures Great and Small", "James", myBooks.add(book1); myBooks.add(book2); myBooks.add(book3), // Add more books! ArrayList currentList = myBooks.getItems(); currentList.sort(null); myBooks.setItems(currentList); myBooks.printItemList(); 8 public Book (String title, String authorLast) { super (0); } public String getTitle() { return title; this.title = title; this.author First = "unknown"; this.author Last = author Last; this.setNumPages (-1); } // Override the Item class method // the book title as the item name. public String getItemName() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthorLast() { return author Last; } } since we want to use public void setAuthor Last(String author Last) { this.author Last = author Last; 6b import java.util.ArrayList; public class CollectionDriver5 { public static void printItems (ArrayList books) { for (Book b: books) { System.out.println(b.getTitle()); } } } public static void main(String[] args) { ArrayList myBooks = new ArrayList (); Book book1 = new Book ("The Phantom Tollbooth", 'Juster"); Book book2 = new Book ("Where the Wild Things Are", "Maurice", Book book3 = new Book ("All Creatures Great and Small", "Sendak"); "Herriot"); } // Add more books! myBooks.add(book1); myBooks.add(book2); myBooks.add(book3); myBooks. sort(null); printItems(myBooks); 'James", 11 9

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

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions

Question

Calculate the missing value.

Answered: 1 week ago