Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with external class

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Problem with external class

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with external class

    The error you're getting is typically due to running code that contains incomplete method stubs that were generated by Netbeans or perhaps other IDEs. Be sure you're compiling and running the latest version of your code, because I don't see a reason you'd be getting that error.

    However, you should be getting an NPE when you run the code you've posted, because the CombinationLock class provides for 2 Counter instances, creates one local to the constructor, myCounter, which is of no use, and then tries to use the other, counter, that has not been instantiated throughout the rest of the class. Initialize either counter or myCounter correctly in the constructor and then use that instance variable throughout the rest of the class.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with external class

    Quote Originally Posted by GregBrannon View Post
    The error you're getting is typically due to running code that contains incomplete method stubs that were generated by Netbeans or perhaps other IDEs. Be sure you're compiling and running the latest version of your code, because I don't see a reason you'd be getting that error.

    However, you should be getting an NPE when you run the code you've posted, because the CombinationLock class provides for 2 Counter instances, creates one local to the constructor, myCounter, which is of no use, and then tries to use the other, counter, that has not been instantiated throughout the rest of the class. Initialize either counter or myCounter correctly in the constructor and then use that instance variable throughout the rest of the class.
    I think I found the problem. At least in NetBeans. I needed to declare the object Counter on the constructor

    more like this:

    Any opinion why this happened?

    public class CombinationLock
    {
        private String S1, S2, S3;
        private String Pos1;
        private String Pos2;
        private String Pos3;
        private boolean open;
        private Counter tally = new Counter();
     
     
     
       // 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;
     
     
          // 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)
       {
     
           tally.click();
     
            if (tally.getValue() == 1)
            {
                Pos1 = aPosition ;
     
            }
            else if (tally.getValue() == 2)
            {
                Pos2 = aPosition ;
            }
            else if (tally.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()
       {
           tally.reset();
           open = false;
           System.out.println("The MasterLock is now locked");
       }
     
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with external class

    Because I'm a fan of actually constructing as much of the class in the constructor as possible, you could also:
    public class CombinationLock
    {
        private String S1, S2, S3;
        private String Pos1;
        private String Pos2;
        private String Pos3;
        private boolean open;
     
        // GB: declare it here
        private Counter tally;
     
     
     
       // 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;
     
            // GB: initialize it here
            tally = new Counter();
     
          // TO DO: write method body
       }
    // . . . plus the rest
    I'm not sure what you mean asking for opinions as to "why it happened." You're opening yourself up for criticism, unless you think it can be blamed on Netbeans. It was a mistake. Learn from it and don't do it again.

Similar Threads

  1. use external class
    By swedishzeus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2013, 06:53 AM
  2. Can't call method from external class
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 24th, 2012, 08:30 PM
  3. Replies: 5
    Last Post: October 18th, 2012, 01:43 PM
  4. Replies: 0
    Last Post: February 12th, 2011, 07:44 PM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM