Reputation: 351
OK so I have an issue with a homework problem. I'm building a class that be used in a tester class provided by my instructor. Its nothing more than a basic counter. I've got the counter set up to where numbers passed by the tester to my class work fine and output is as expected. However my class must have the initial count set to 0 and a default increment/decrement of 1.
Here is my class:
public class Counter
{
private int count;
private int stepValue;
/**
* This method transfers the values called from CounterTester to instance variables
* and increases/decreases by the values passed to it. It also returns the value
* of count.
*
* @param args
*/
public Counter (int initCount, int value)
{
count=initCount;
stepValue=value;
}
public void increase ()
{
count = count + stepValue;
}
public void decrease ()
{
count = count - stepValue;
}
public int getCount()
{
return count;
}
}
Here is the tester class:
public class CounterTester
{
/**
* This program is used to test the Counter class and does not expect any
* command line arguments.
*
* @param args
*/
public static void main(String[] args)
{
Counter counter = new Counter();
counter.increase();
System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());
counter.increase();
System.out.println("Expected Count: 2 -----> Actual Count: " + counter.getCount());
counter.decrease();
System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());
counter = new Counter(3, 10);
System.out.println("Expected Count: 3 -----> Actual Count: " + counter.getCount());
counter.increase();
System.out.println("Expected Count: 13 ----> Actual Count: " + counter.getCount());
counter.decrease();
System.out.println("Expected Count: 3 -----> Actual Count: " + counter.getCount());
counter.decrease();
System.out.println("Expected Count: -7 ----> Actual Count: " + counter.getCount());
}
}
Upvotes: 0
Views: 3328
Reputation: 36449
You don't have a no arg constructor in your Counter class for the first time you instantiate it, so do
Counter counter = new Counter(0,1);
Which should set the initial and step values.
Or you can provide a no-arg constructor:
public class Counter
{
private int count;
private int stepValue;
public Counter () { //no argument constructor - must be explictly made now
count=0;
stepValue = 1;
}
public Counter (int initCount, int value)
{
count=initCount;
stepValue=value;
}
//rest of code
}
Upvotes: 2