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
3) 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() { //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
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
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
Get step-by-step solutions from verified subject matter experts
