Question: Implement the three methods you wrote for ArrayBag for LinkedBag: boolean remove (T item); T removeRandom(); T getRandom(); Then test them using Bags.java. The output

Implement the three methods you wrote for ArrayBag for LinkedBag:

boolean remove (T item); T removeRandom(); T getRandom();

Then test them using Bags.java. The output will be similar to that for the ArrayBag (but with the order reversed since operations for the linked bag are performed at the front):

Output is below

Here's what's in our bag [lime, coconut, kiwi, grape, orange]

Does our bag contain the word 'kiwi'? yes Does our bag contain the word 'mango'? no

Selecting an item (always same) lime lime

Selecting a random item (varies) coconut grape

Removing 'grape' from the bag [coconut, kiwi, lime, orange]

Removing an item (always front one) coconut [kiwi, lime, orange]

Removing a random item kiwi [lime, orange]

Let's empty the bag [] Trying to get a random item null Trying to remove a random item null Trying to remove 'kiwi' false

Code that is provided is below

/*

* Bags

*

* program for testing methods in ArrayBag and LinkedBag

*/

public class Bags

{

public static void main (String [] args)

{

try

{

Bag words = new ArrayBag();

//Bag words = new LinkedBag();

// adding to bag

String [] fruits = {"orange", "grape", "kiwi", "coconut", "lime"};

for (int i = 0; i < fruits.length; i++)

{

words.add(fruits[i]);

}

System.out.println(" Here's what's in our bag " + words);

// seeing if bag contains item

boolean result = words.contains("kiwi");

System.out.println(" Does our bag contain the word 'kiwi'? " +

(result ? "yes" : "no"));

result = words.contains("mango");

System.out.println("Does our bag contain the word 'mango'? " +

(result ? "yes" : "no"));

// retrieving item

System.out.println(" Selecting an item (always same)");

System.out.println(words.get());

System.out.println(words.get());

// retrieving random item

System.out.println(" Selecting a random item (varies)");

System.out.println(words.getRandom());

System.out.println(words.getRandom());

// removing specific item

words.remove("grape");

System.out.println(" Removing 'grape' from the bag " + words);

// removing item

System.out.println(" Removing an item (always end one)");

System.out.println(words.remove());

System.out.println(words);

// removing random item

System.out.println(" Removing a random item");

System.out.println(words.removeRandom());

System.out.println(words + " ");

// testing methods on empty bag

System.out.println("Let's empty the bag");

words.remove();

words.remove();

System.out.println(words);

System.out.println("Trying to get a random item " +

words.getRandom());

System.out.println("Trying to remove a random item " +

words.removeRandom());

System.out.println("Trying to remove 'kiwi' " +

words.remove("kiwi"));

}

catch (IllegalArgumentException e)

{

System.out.println(e.getMessage());

}

}

}

public interface Bag 
{ 
 void add (T item); 
 T remove(); 
 T get(); 
 boolean contains (T item); 
 int size(); 
 public String toString(); 
 
 boolean remove (T item); 
 T removeRandom(); 
 T getRandom(); 
} 

/* 
 * LinkedBag 
 * 
 * linked structure implementation of a bag 
 * 
 * unordered collection with duplicates allowed 
 */ 
 
public class LinkedBag implements Bag 
{ 
 /* 
 * inner class to represent node 
 */ 
 
 private class Node 
 { 
 private T data; 
 private Node next; 
 
 public Node(T item) 
 { 
 data = item; 
 next = null; 
 } 
 } 
 
 // instance variables for bag 
 private Node head; 
 private int size; 
 
 /* 
 * creates bag 
 */ 
 
 public LinkedBag() 
 { 
 head = null; 
 size = 0; 
 } 
 
 /* 
 * adds item to bag 
 */ 
 
 public void add(T item) 
 { 
 checkForNull(item); 
 Node newest = new Node (item); 
 newest.next = head; 
 head = newest; 
 size++; 
 } 
 
 /* 
 * removes item from bag 
 * 
 * returns removed item, null if empty 
 */ 
 
 public T remove() 
 { 
 if (size == 0) 
 { 
 return null; 
 } 
 
 T removed = head.data; 
 head = head.next; 
 size--; 
 return removed; 
 } 
 
 /* 
 * returns item from bag 
 */ 
 
 public T get() 
 { 
 if (size == 0) 
 { 
 return null; 
 } 
 
 return head.data; 
 } 
 
 /* 
 * returns true if item in bag, false otherwise 
 */ 
 
 public boolean contains(T item) 
 { 
 Node current = head; 
 
 for (int i = 0; i < size; i++) 
 { 
 if (current.data.equals(item)) 
 { 
 return true; 
 } 
 
 current = current.next; 
 } 
 
 return false; 
 } 
 
 /* 
 * returns size of bag 
 */ 
 
 public int size() 
 { 
 return size; 
 } 
 
 /* 
 * returns string representation of contents of bag 
 */ 
 
 public String toString() 
 { 
 if (size == 0) 
 { 
 return "[ ]"; 
 } 
 
 String c = "["; 
 Node current = head; 
 
 while (current.next != null) 
 { 
 c += current.data + ", "; 
 current = current.next; 
 } 
 
 return c + current.data + "]"; 
 } 
 
 /* methods to be added */ 
 
 /* 
 * removes specified item from bag 
 * 
 * returns false if item not present 
 */ 
 
 public boolean remove (T item) 
 { 
 return false; 
 } 
 
 /* 
 * removes random item from bag 
 * 
 * returns null if bag empty 
 */ 
 
 public T removeRandom () 
 { 
 return null; 
 } 
 
 /* 
 * returns random item from bag 
 * 
 * returns null if bag empty 
 */ 
 
 public T getRandom () 
 { 
 return null; 
 } 
 
 /* end of added methods */ 
 
 /* 
 * checks if item is null and throws exception if so 
 */ 
 
 private void checkForNull (T item) 
 { 
 if (item == null) 
 { 
 throw new IllegalArgumentException("null not a possible value!"); 
 } 
 } 
} 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!