Question: Language: Java I am having a problem where the getCoinValue method is not triggering the while loop in 3) , the getCoinValue method is located

Language: Java

I am having a problem where the getCoinValue method is not triggering the "while" loop in 3), the getCoinValue method is located in 2), please do not change anything major in the whole code, just what needs to be fixed and let me know what it was!

Thank you and here is the code:

1) BagInterface.java

package nodebasedarray;

public interface BagInterface { /** Gets the current number of entries in this bag. * @return the integer number of entries currently in the bag */ public int getCurrentSize(); /** Sees whether this bag is full. * @return true if the bag is full, or false if not */ public boolean isFull(); /** Sees whether this bag is empty. * @return true if the bag is empty, or false if not */ public boolean isEmpty(); /** Adds a new entry to this bag. * @param newEntry the object to be added as a new entry * @return true if the addition is successful, or false if not */ public boolean add(T newEntry); /** Removes one unspecified entry from this bag, if possible. * @return either the removed entry, if the removal * was successful, or null */ public T remove(); /** Removes one occurrence of a given entry from this bag, if possible. * @param anEntry the entry to be removed @return true if the removal was successful, or false if not */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** Counts the number of times a given entry appears in this bag. * @param anEntry the entry to be counted @return the number of times anEntry appears in the bag */ public int getFrequencyOf(T anEntry); /** Tests whether this bag contains a given entry. * @param anEntry the entry to locate @return true if the bag contains anEntry, or false otherwise */ public boolean contains(T anEntry); /** Creates an array of all entries that are in this bag. * @return a newly allocated array of all the entries in the bag */ public T[] toArray(); }//End Bag Interface

3) Coin.java

package nodebasedarray;

public class Coin implements Comparable{ int coinValue;

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() { //This is the method that is not working properly 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 ; }

}

3) NodeBasedBag.java - This is where the while loop is

package nodebasedarray;

public class NodeBasedBag implements BagInterface {

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){ // This is the loop that is not being triggered when I run the program after the second value if ((currentNode.item.getCoinValue() <= firstNode.item.getCoinValue()) && firstNode.item.getCoinValue() < currentNode.next.item.getCoinValue()) { firstNode.next = currentNode.next; currentNode.next = firstNode; numberOfNodes++; return true; } currentNode = currentNode.next;

} 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 < numberOfNodes) && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { frequency++; } counter++; currentNode = currentNode.next; } // end while return frequency; }

@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 < numberOfNodes) && (currentNode != null)) { result[index] = (Coin) currentNode.item; index++; currentNode = currentNode.next; } // end while return result; }

@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) < 0) { minNode = currentNode; } currentNode = currentNode.next; } return minNode; } public void setItem(Coin item) { this.item = 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; } } }

4) 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 bag = new NodeBasedBag<>(); int choice = -1;

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); }

}

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!