Hello guys,
I'm working on project for school, on the one the professor required us to use the class Counter as part of the exercise. The exercise itself is to implement a combination lock class that has a dial with 26 positions labeled A..Z. The dial needs to be set 3 times. If it is set to the correct combination, the lock can be opened. When the lock is closed again, the combination can be entered again.
So far, this is what I have, and the problem resides on the counter class.
CombinationLock.java
/** * A class to implement a combination lock with 26 dial positions * and a three-letter combination * */ public class CombinationLock { private String S1, S2, S3; private String myPositions; // A string that represents the positions set on the dial. private Counter counter; private String Pos1; private String Pos2; private String Pos3; private boolean open; public int value; // instance variable declarations go here /** * Creates a lock with a given combination consisting of three upper-case characters. * @param first the first letter of the combination * @param second the second letter of the combination * @param third the third letter of the combination */ public CombinationLock(String first, String second, String third) { S1 = first; S2 = second; S3 = third; myPositions = ""; value = 1; Counter myCounter = new Counter(); // TO DO: write method body } public String getfirst() { return S1; } public String getsecond() { return S2; } public String getthird() { return S3; } /** * Set the dial to a position * @param aPosition a String consisting of a single uppercase letter (A..Z) */ public void setPosition(String aPosition) { counter.click(); if (counter.getValue() == 1) { Pos1 = aPosition ; } else if (counter.getValue() == 2) { Pos2 = aPosition ; } else if (counter.getValue() == 3) { Pos3 = aPosition; } } /** * Try opening the lock */ public void tryToOpen() { // TO DO: write method body if(Pos1.equals(S1) && Pos2.equals(S2) && Pos3.equals(S3)) { open = true; System.out.println("Combo correct! Lock is open."); } else { open = false; System.out.println("Wrong combo. Try again."); } } /** * Check whether the lock is open * @return true or false indicating whether the lock is open */ public boolean isOpen() { return open; } /** * Close the lock and print a message indicating that the lock is now closed */ public void lock() { counter.reset(); open = false; System.out.println("The MasterLock is now locked"); } }
CombinationLockTest.java
public class CombinationLockTest { public static void main (String args[]) { CombinationLock MasterLock = new CombinationLock("J","A","P"); System.out.println("The combination to MasterLock is " + MasterLock.getfirst() + MasterLock.getsecond() + MasterLock.getthird()); String input = JOptionPane.showInputDialog("Enter first character"); MasterLock.setPosition(input); String input2 = JOptionPane.showInputDialog("Enter second character"); MasterLock.setPosition(input2); String input3 = JOptionPane.showInputDialog("Enter third character"); MasterLock.setPosition(input3); System.out.println("The user entered combo is " + input + input2 + input3); MasterLock.tryToOpen(); System.out.println(MasterLock.isOpen()); MasterLock.lock(); } }
Counter.java
/** This class models a tally counter. */ public class Counter { private int value; /** Gets the current value of this counter. @return the current value */ public int getValue() { return value; } /** Advances the value of this counter by 1. */ public void click() { value = value + 1; } /** Resets the value of this counter to 0. */ public void reset() { value = 0; } }
The error that I'm getting is
run:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at Counter.<init>(Counter.java:9)
at CombinationLock.<init>(CombinationLock.java:33)
at CombinationLockTest.main(CombinationLockTest.java: 15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Any help on this? At this point I don't know what else to do. I cannot modify anything else, and I must use the counter.java