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

Thread: CounterTester.java Not Decreasing in the counter????

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question CounterTester.java Not Decreasing in the counter????

    I believe I have this program CounterTester.java down but in my output the program is increasing the way I want it to but it is not decreasing the way I want it to.



    Would someone point me in the right direction?

    Thank you in advance.


    public class CounterTester
    {
    	static int myCount;
    	static int myCount2;
     
    public CounterTester(int inti) {
        myCount = 1;
        myCount2 = 10;
    }
     
    public static void main(String[] args)
    {
     
        CounterTester counter = new CounterTester(myCount);  //create a new counter with a step value of 1
     
        counter.increase(myCount); //add 1
        System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getValue());
     
        counter.increase(myCount++); //add 1
        System.out.println("Expected Count: 2 -----> Actual Count: " + counter.getValue());
     
        counter.decrease(myCount--); //subtract 1
        System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getValue());
     
        CounterTester counter1 = new CounterTester(myCount2); //create a new counter with a step value of 10
        System.out.println("Expected Count: 0 -----> Actual Count: " + counter1.getValue());
     
        counter1.increase(myCount2(10)); //add 10
        System.out.println("Expected Count: 10 ----> Actual Count: " + counter1.getValue());
     
        counter1.decrease(myCount2(myCount2)); //subtract 10
        System.out.println("Expected Count: 0 -----> Actual Count: " + counter1.getValue());
     
        counter1.decrease(myCount2(myCount2)); //subtract 10
        System.out.println("Expected Count: -10 -----> Actual Count: " + counter1.getValue());
     
    }
     
    private static int myCount2(int i) {
    	return 0;
    }
     
    private void decrease(int i) {
     
    }
     
    public int getValue() {
    	return myCount;
    }
     
    private void decrease() {
        myCount--;
    }
     
    private void increase(int i) {
        myCount++;
     
    }
     
    public String toString() {
    	return ""+myCount;
    }
     
    public void reset() {
     
    }
     
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: CounterTester.java Not Decreasing in the counter????

    You have no code in your decrease method.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CounterTester.java Not Decreasing in the counter????

    Ok. so I just added
    int i
    inside the (). I actually looking at it not I already had it once as a method. My output after making the change looks like this:

    Expected Count: 1 -----> Actual Count: 2
    Expected Count: 2 -----> Actual Count: 4
    Expected Count: 1 -----> Actual Count: 2
    Expected Count: 0 -----> Actual Count: 1
    Expected Count: 10 ----> Actual Count: 2
    Expected Count: 0 -----> Actual Count: 1
    Expected Count: -10 -----> Actual Count: 0

    Still not working as expected.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,530
    My Mood
    Mellow
    Thanks
    157
    Thanked 615 Times in 602 Posts

    Default Re: CounterTester.java Not Decreasing in the counter????

    Though very simple, the way you've set this up is bizarre and confusing - mostly confusing to you and those who do not understand the difference between static/class variables and instance variables. But still confusing to the rest of us who are wondering what you're trying to do.

    myCount and myCount2 are static or class variables (two names for the same thing), but you're treating them as though they were instance variables.

    Try rewriting your code so that the variables are declared thusly:

    private int myCount;
    private int myCount2;

    You'll get errors at first. Those errors can be overcome, and I hope you'll learn something in the process. If you can't figure out how to do what I'm asking or if you don't understand why, then come back.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CounterTester.java Not Decreasing in the counter????

    Ok GregBrannon, Thank you for your response. I made the changes but the errors that pop up is that the system wants to make them 'static'. Is that right?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,530
    My Mood
    Mellow
    Thanks
    157
    Thanked 615 Times in 602 Posts

    Default Re: CounterTester.java Not Decreasing in the counter????

    Don't let "the system" or the IDE be the boss of you or your code. You need to learn the whys and hows of doing it correctly.

    Instead of using static variables as you posted, use instance variables as I suggested, then DON'T use those variables in the main method. Either use other variables local to main() OR use constants. Using local variables is preferred, because that will allow you to change the program's behavior by changing the value of a variable rather than changing every constant, remembering why/what the constant should change to, etc.

    Start simple. Comment out most of your main() method and reduce it to a couple working lines. Something like:
        public static void main( String[] args )
        {
            int firstStepValue = 1;
            int secondStepValue = 10;
     
             //create a new counter with a step value of 1
            CounterTester counter = new CounterTester( firstStepValue );
            counter.increase( firstStepValue ); //add 1
            System.out.println( "Expected Count: 1 -----> Actual Count: " +
                                counter.getValue() );
    /*                            
            counter.increase( 1 ); //add 1
            System.out.println( "Expected Count: 2 -----> Actual Count: " +
                                counter.getValue() );
            counter.decrease( -1 ); //subtract 1
            System.out.println( "Expected Count: 1 -----> Actual Count: " +
                                counter.getValue() );
            CounterTester counter1 = new CounterTester(
                10 ); //create a new counter with a step value of 10
            System.out.println( "Expected Count: 0 -----> Actual Count: " +
                                counter1.getValue() );
            counter1.increase( 10 ); //add 10
            System.out.println( "Expected Count: 10 ----> Actual Count: " +
                                counter1.getValue() );
            counter1.decrease( -10 ); //subtract 10
            System.out.println( "Expected Count: 0 -----> Actual Count: " +
                                counter1.getValue() );
            counter1.decrease( -10 ); //subtract 10
            System.out.println( "Expected Count: -10 -----> Actual Count: " +
                                counter1.getValue() );
    */
        }
    If that little bit or working code doesn't work as expected, figure out why and fix it. (It actually does work as it should, but your expectations are wrong. Your expectations may be what you have to fix.)

    BTW - Understanding this may be your first step (one of many) in understanding a most basic Object Oriented Programming principle, what objects are and a little bit about how to use them.

Similar Threads

  1. Struggling with Java counter
    By maths94 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 7th, 2013, 11:27 PM
  2. [SOLVED] Find the the longest decreasing row of numbers in a vector
    By einar123 in forum What's Wrong With My Code?
    Replies: 27
    Last Post: September 24th, 2013, 09:55 AM
  3. Java Cupcake Counter Help
    By Hawks in forum What's Wrong With My Code?
    Replies: 12
    Last Post: February 20th, 2013, 01:47 PM
  4. Why width of dropdown is decreasing automatically on every select?
    By ayush1989 in forum Other Programming Languages
    Replies: 0
    Last Post: February 1st, 2013, 12:39 AM
  5. Increasing or decreasing!
    By pesha in forum Algorithms & Recursion
    Replies: 4
    Last Post: November 6th, 2011, 03:06 PM

Tags for this Thread