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 2 of 2

Thread: (Counter/Accessor) Did I do this right?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default (Counter/Accessor) Did I do this right?

    I didn't understand the problem, so I looked at this explanation: methods - Using a Counter class to increment and decrement: java - Stack Overflow

    And I came up with:

    package counterassignment5;//http://stackoverflow.com/questions/7857600/using-a-counter-class-to-increment-and-decrement-java
     
    public class CounterAssignment5 {
     
        public static void main(String[] args) {
     
            Counter count1 = new Counter();
            System.out.println("Setting the counter to zero: " + count1.start());
            System.out.println("");
     
            System.out.println("Let's change the left side.");
            System.out.println("");
     
            count1.increase();
            count1.increase();
            System.out.println("After increasing the number by 2, the value is " + count1+ ".");
     
            count1.decrease();
            System.out.println("After decreasing it by 1, the value is " + count1 +".");
     
            System.out.println("Finished counting. Counter value of left side is: ");
            count1.output();
            System.out.println("");
     
            Counter count2 = new Counter();
            System.out.println("Is " + count1 + " equal to " + count2 + "? ");
            System.out.println(count1.equals(count2));
            System.out.println("");
     
            count2.increase();
            System.out.println("Increased right-hand side by 1. Counter value of right side is: ");
            count2.output();
            System.out.println("");System.out.println("Is " + count1 + " equal to " + count2 + "? ");
            System.out.println(count1.equals(count2));
            System.out.println("");
     
            count2.reset();
            System.out.println("Value reset to " + count2 +".");
     
        }
     
    }
    (Separate Class)
    package counterassignment5;
     
    public class Counter {
     
       public int num = 0;
        public Counter(){
            this.num=0;
        }
     
     
        public int start(){
        return num;
        }
        public int increase(){
        return num++;   
        }
        public int decrease(){
        return num--;
        }
        public int reset(){
        return num = 0;    
        }
        public String toString(){
        return Integer.toString(num);
        }
        public boolean equals (Counter counter2){
            if (num == counter2.start())
            return true;
            else    
            return false;
        }
        public void output(){
            System.out.println(num);
        }}

    It works as intended, but I'm wondering if I understood the problem correctly. If I didn't...that would be bad.

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: (Counter/Accessor) Did I do this right?

    Please post relevant materials on this forum with your question. This forum does not control third party activities and can not guarantee how long third parties will keep the information available, if at all. If parts go missing later, the content of this forum will have little meaning in terms of being useful for answering the same question over in the future.

    It works as intended,
    Well that is a good sign...
    but I'm wondering if I understood the problem correctly. If I didn't...that would be bad.
    That is the first step in problem solving. I sure hope you did not save this step til last!

Similar Threads

  1. [SOLVED] Counter
    By mssim in forum Java Theory & Questions
    Replies: 2
    Last Post: November 7th, 2012, 05:32 AM
  2. Counter troubles
    By GinoGore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 09:28 AM
  3. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 08:50 AM
  4. Need Help with Accessor and Mutator Methods
    By ankur_032 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 9th, 2011, 10:00 AM
  5. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 11:49 AM