Question: Language: Java Here is the code used to fulfill the project: First is BagInterface.java package nodebasedarray; public interface BagInterface { /** Gets the current number
Language: Java

Here is the code used to fulfill the project:
First is BagInterface.java
package nodebasedarray;
public interface BagInterface
Second is Coin.java
package nodebasedarray;
public class Coin implements Comparable
Coin(int coinValue){ setCoinValue(coinValue); } public void setCoinValue(int coinValue) { if(coinValue==10||coinValue==25||coinValue==50) this.coinValue=coinValue; else this.coinValue = 10; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Coin other = (Coin) obj; if (this.coinValue != other.coinValue) { return false; } return true; }
public int getCoinValue() { return coinValue; }
@Override public String toString() { return coinValue+" cents"; //To change body of generated methods, choose Tools | Templates. }
@Override public int compareTo(Coin o) { return coinValue-o.coinValue ; }
}
Third is NodeBasedBag.java
package nodebasedarray;
public class NodeBasedBag
Node firstNode; int numberOfNodes = 0;
public NodeBasedBag() { firstNode = null; numberOfNodes = 0; }
@Override public int getCurrentSize() { return numberOfNodes; }
@Override public boolean isEmpty() { return numberOfNodes == 0; }
/* @Override public boolean add(T newEntry) { // add to beginning of chain: Node newNode = new Node(newEntry); newNode.next = firstNode; // make new node reference rest of chain // (firstNode is null if chain is empty) firstNode = newNode; // new node is at beginning of chain numberOfNodes++; return true; }
*/ @Override public boolean add (Coin newEntry){ Node newNode = new Node(newEntry); newNode.next = firstNode; firstNode = newNode; Node currentNode = newNode.next; if (currentNode == null) { currentNode = firstNode; numberOfNodes++; return true; } while (currentNode.next != null){ if ((currentNode.item.getCoinValue()
} currentNode.next = newNode; numberOfNodes++; return true; } @Override public Coin remove() { Coin result = null; if (firstNode != null) { result = firstNode.item; firstNode = firstNode.next; // remove first node from chain numberOfNodes--; } // end if return result; }
private Node getReferenceTo(Coin anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { found = true; } else { currentNode = currentNode.next; } } // end while return currentNode; } // end getReferenceTo
@Override public boolean remove(Coin anEntry) { boolean result = false; Node nodeN = getReferenceTo(anEntry); if (nodeN != null) { nodeN.item = firstNode.item; // replace located entry with entry // in first node remove(); // remove first node result = true; } // end if return result; }
@Override public void clear() { while (!isEmpty()) { remove(); } }
@Override public int getFrequencyOf(Coin anEntry) { int frequency = 0; int counter = 0; Node currentNode = firstNode; while ((counter
@Override public boolean contains(Coin anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { found = true; } else { currentNode = currentNode.next; } } // end while return found; }
@Override public Coin[] toArray() { Coin[] result = (Coin[]) new Object[numberOfNodes]; // unchecked cast int index = 0; Node currentNode = firstNode; while ((index
@Override public boolean isFull() { return false; }
class Node { public Coin getItem() { return item; } public Node getMin() { //bag is empty if (firstNode == null) { return null; } //One item in the bag if (firstNode.next == null) { return firstNode; }
Node currentNode = firstNode.next; Node minNode = firstNode; while (currentNode != null) { if (((Integer) currentNode.item).compareTo((Integer) minNode.item)
public Node getNext() { return next; }
public void setNext(Node next) { this.next = next; } Coin item; Node next;
Node(Coin item) { this(item, null); }
Node(Coin item, Node next) { this.item = item; this.next = next; } } }
Fourth is NodeBasedBagTest.java - this is where the code is going to be ran
package nodebasedarray;
import java.util.Arrays; import java.util.Scanner;
public class NodeBasedBagTest {
/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here NodeBasedBag
do { System.out.println("[1] To add a new item to the bag"); System.out.println("[2] To remove an item from the bag"); System.out.println("[3] To remove a specific item from the bag"); System.out.println("[4] To clear the bag"); System.out.println("[5] To get a frequency of a given item in the bag"); System.out.println("[6] To check if an item exist in the bag"); System.out.println("[7] To print bag content"); System.out.println("[8] To Exit"); System.out.println("Enter your choice:"); Scanner in = new Scanner(System.in); choice = in.nextInt(); switch (choice) { case 1: System.out.println("Enter item:"); int coinValue = in.nextInt(); Coin c = new Coin(coinValue); if (bag.add(c)) { System.out.println("Item " + c.getCoinValue() + " is added successfully to the bag"); } break; case 2: if (bag.isEmpty()) { System.out.println("Bag is empty!"); } else { System.out.println("Item " + bag.remove().getCoinValue() + " is removed successfully"); } break; case 3: System.out.println("Enter the item you want to remove from the bag:"); coinValue = in.nextInt(); c = new Coin(coinValue); if (bag.contains(c)) { bag.remove(c); System.out.println("Item " + c.getCoinValue() + " is removed from the bag"); } else { System.out.println("The item " + coinValue + " doesnt exist in the bag"); } break; case 4: bag.clear(); System.out.println("Bag is empty now!"); break; case 5: System.out.println("Enter the item you are looking for:"); coinValue = in.nextInt(); c = new Coin(coinValue); System.out.println("The item " + coinValue + " is found " + bag.getFrequencyOf(c) + " times"); break; case 6: System.out.println("Enter the item you are looking for:"); coinValue = in.nextInt(); c = new Coin(coinValue); if (bag.contains(c)) { System.out.println("The item " + coinValue + " is in the bag"); } else { System.out.println("The item " + coinValue + " is NOT in the bag"); } break; case 7: if (bag.isEmpty()) { System.out.println("Bag is empty!"); } else { System.out.println("Bag content:"); System.out.println(Arrays.toString(bag.toArray())); } break; case 8: System.out.println("GoodBye!"); break; default: System.out.println("Invalid choice! Enter a number in the range [1-8]"); break; } } while (choice != 8); }
}
Thank you in advance
In this project you will write the code to implement a dynamic (node-based) sorted bag. Use the Node-based BAG code as a starting point and add the required code to fully implement it. For this assignment we will use a bag of coins. Use the same coin class you used in the previous homework. Sorted bag of coins is a bag that contains a chain of coins sorted in ascending order. Meaning: first node is always pointing to the smallest coin in the bag. Other coins are linked in an ordered fashion not based on the order they were entered ifi The SortedBag should support the following Operations (a) Add: When called the following steps happen 1. the user is asked to enter the value of the coin then the coin created with the specific value and added to the bag 2. The coin should be added such that the bag remains sorted. If the coin is the smallest value in the bag, then it should be the first node 3. If the coin has the biggest value then it should be the last node 4. Otherwise the coin should be placed properly such that the bag remains sorted (b) Removing a coin: When called, you should remove the last coin added to the bag (c) Removing a specific coin: When called you should ask the user to specify the value of the coin you want to remove, based in that you search the bag and remove a coin of the same value. If the coin doesn't exist then you display a message indicating that the specific coin doesn't exist in the bag (d) Clear the bag: This method resets every thing i.e. clears the bag (e) Get frequency: When called, you should ask the user to specify the coin value Based on that, you return the number of occurrences of that value (f) Check if Exist: When called, you ask the user the coin value he/she want to check You search for it, if the coin of that value exist you display a message indicating its existence, otherwise you display a message that its not in the bag (g) Print contents: You print the bag contents as is. Note that just printing the content will naturally print then sorted, since they are added in a way that maintains their order i.e. No need to call Arrays.sort() The menu that appears to the user should like this [1] Adding a coin [2] Removing a coin [3] Removing a specific coin 4] Clear the bag [5] Get the frequency of a given coin [6] Check if specific coin exists in the bag 7] Print bag contents [8] Exit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
