Question: Can someone helps me with this unfinished java code?(The finished part I marked as DONE. see all requirements below )Thank u so much ShoppingList.java: import
Can someone helps me with this unfinished java code?(The finished part I marked as "DONE". see all requirements below )Thank u so much
ShoppingList.java:
import java.util.Arrays;
/** * Implement a simple shopping list that allows for adding * and removing items. The list has a maximum size. * * @author * */
/* * Here is a suggested order for this lab (we will do 1-5 mostly together): * Some javadocs have been done for you. You should fill in the missing ones. * 1. Instance variables (2 of them) Done * 2. int constructor Done * 3. toString (let Eclipse generate it for us) Done * 4. add() (initially, an incomplete version that kind of works) Done * 5. copy constructor * 6. Fix add() so that it handles full lists correctly * 7. getCapacity(), getItemCount(), isEmpty() * 8. contains() * 9. remove() * 10. displayList() * 11. merge() */
public class ShoppingList { private String[] items; private int numItems; //how many items currently in the array /** * Constructs a new shopping list with the specified * capacity, and no items in it. * @param capacity the maximum number of items that can be on the list */ public ShoppingList(int capacity) { items = new String[capacity]; numItems = 0; } /** * Constructs a new shopping list that duplicates an old shopping list * @param old */ public ShoppingList(ShoppingList old) {
//A loop here to copy items from the old list's array //to the new list's array } /** * Returns the number of items on this list * @return the number of items on this list */ public int getItemCount() { return -1; }
/** * Returns the maximum number of items that can be stored in this list * @return the maximum number of items that can be stored in this list */ public int getCapacity() { return -1; } /** * Returns whether the list is empty (contains no items) * @return true if there are no items on the list, and false otherwise */ public boolean isEmpty() { return false; } /** * Returns whether the list contains a specified item * @param item the item to search for in the list * @return true if the item appears in the list at least once, and false otherwise */ public boolean contains(String item) { return false; } /** * Adds an item to the list if there is room. * @param item item to be added to the list * @return true is the add was successful, false otherwise * (if there is not enough room) */ public boolean add(String item) { if(numItems == items.length) return false; items[numItems] = item; numItems++; return true; } /** * Removes the first occurrence of the specified item from the list, if it exists * @param item the item to be removed * @return true if the item was removed, and false otherwise */ public boolean remove(String item) { return false; }
/** * Prints the contents of the list, one item per line */ public void displayList() { } /** * Returns an array consisting of the items on the list. * @return an array consisting of all the items that are on the list */ public String[] getItems() { // NOTE: This does NOT return the actual array instance variable, because doing so // would allow the client to alter the array in unexpected ways. // This only returns an array containing the actual items. So, if the capacity // of the list is 10, but there are only 3 items, then this will return an array // of size 3 containing only those three items. // That means that this could return an array of size 0, which is perfectly fine...no big deal. return null; } /** * Combines two lists together into a new list. The capacity * of the new list should be the combined capacities of this list * and the other list. The items on this list should appear before * the items on the other list. Does not make any changes to this list or * the other list. * * @param other the other list to be combined with this list * @return a new ShoppingList object with all the combined items */ public ShoppingList merge(ShoppingList other) { return null; }
@Override public String toString() { return "ShoppingList [items=" + Arrays.toString(items) + ", numItems=" + numItems + "]"; } } -------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
ListTester.java:
public class ListTester { public static void main(String[] args) { ShoppingList s = new ShoppingList(4); System.out.println(s); s.add("appples"); s.add("bananas"); System.out.println(s); System.out.println(s.add("Apple"));
// boolean attempt = s.add("oranges"); // System.out.println(attempt); // // ShoppingList t = new ShoppingList(s); // System.out.println(t); // Test regularly and thoroughly. Every time you write a method, immediately test it, including... // EDGE CASES. Here are some examples (but not a complete list. You should always be thinking // about edge cases.) // 1. What happens when adding to a list that is full? Check the array and the item count // 2. What happens when removing from a list that is empty? Check the array and the item count // 3. What happens if we create a list with capacity 0? Do adding and removing still work? }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
