Results 1 to 3 of 3
  1. #1
    byassir5 is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default Program not working!!

    Hello everybody!!
    I am new to Java, wrote my first program last week. My program is compiling but it is not giving me the result I was hoping for. I would appreciate it if somebody could point out to my mistake.
    Thanks.

    public class Counter
    {
    int counter=0;
    int stepValue;

    public Counter(int step)
    {
    step=stepValue;
    }

    public void increase(){
    counter = counter+stepValue;
    }
    public void decrease(){
    counter = counter-stepValue;
    }

    public int getCount(){
    return counter;
    }


    public static void main(String[] args)
    {

    Counter counter = new Counter(1); //create a new counter with a step value of 1

    counter.increase(); //add 1
    System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());

    counter.increase(); //add 1
    System.out.println("Expected Count: 2 -----> Actual Count: " + counter.getCount());

    counter.decrease(); //subtract 1
    System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());


    counter = new Counter(10); //create a new counter with a step value of 10
    System.out.println("Expected Count: 0 -----> Actual Count: " + counter.getCount());

    counter.increase(); //add 10
    System.out.println("Expected Count: 10 ----> Actual Count: " + counter.getCount());

    counter.decrease(); //subtract 10
    System.out.println("Expected Count: 0 -----> Actual Count: " + counter.getCount());

    counter.decrease(); //subtract 10
    System.out.println("Expected Count: -10 -----> Actual Count: " + counter.getCount());

    }

    }
    Last edited by JosAH; 01-18-2014 at 07:04 AM. Reason: reinstalled original text

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Program not working!!

    What is it giving you add please place your codes in
    With the right know how, anything is possible

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Program not working!!

    Java Code:
    public Counter(int step)
    {
    step=stepValue;
    }
    Here is your problem. You are setting the parameter equal to a global variable. Do the opposite.


    Java Code:
    public Counter(int step) {
            stepValue = step;
        }

Similar Threads

  1. Replies: 5
    Last Post: 02-28-2013, 02:46 AM
  2. program not working
    By Dax Le Foundry in forum New To Java
    Replies: 7
    Last Post: 08-09-2011, 08:10 PM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 1
    Last Post: 08-27-2009, 01:20 PM
  5. My program is not working
    By MICHAELABICK in forum New To Java
    Replies: 6
    Last Post: 12-22-2008, 11:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •