Setters and Getters and the counter exercise
Here is the latest bit of homework I am working on.
Define a class called Counter whose objects count things. Ultimately I want the user to input a number and have the program add a number, subtract a number. Here is the class.
Code:
public class Counter {
private int currentCount;
private int counterAdd;
private int counterSubtract;
//Mutators
public void setCurrentCount()
{this.currentCount = currentCount;
}
public void setCounterAdd()
{this.counterAdd = currentCount++;
}
public void setCounterSubtract()
{if(counterSubtract >0) currentCount--;
}
//Accessor
public int getCurrentCount()
{return currentCount;
}
public void outputCurrentCount()
{System.out.println("The count is " + currentCount);
}
}
Now the program to run it. I only have gotten this far - I want the user to input a number and for that new number (currentCount) to be displayed, which also means that it has a new value so that the other math will work. I get the error message towards the end - see my notes below to see the msg. What am I doing wrong?
Thanks in advance,
Gary
Code:
import java.util.*;
public class CounterTester {
public static void main(String[] args){
Counter test = new Counter();
// test to see that the counter is set to zero
test.setCurrentCount();
test.getCurrentCount();
test.outputCurrentCount();//This should display 0
// now I want to allow the user to enter a positive number
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a positive number");
int currentCount = keyboard.nextInt();
test.setCurrentCount(currentCount); // here is where I get the error message that setCurrentCount() in counter cannot be applied to (int)
System.out.println("The current count is " + o.getCurrentCount());
}
}
Back to work, still not solved
I had to take a week off on this so jumping back in. Still not sure what I am doing wrong. Here is the class code.
Code:
public class Counter {
private int currentCount;
private int counterAdd;
private int counterSubtract;
private int reset;
// Default Constructor
public Counter()
{
currentCount = 0;
counterAdd = currentCount + 1;
counterSubtract = currentCount -1;
reset = 0;
}
//Mutators
public void setCurrent(int currentCount)
{
this.currentCount = currentCount;
counterAdd = currentCount + 1;
counterSubtract = currentCount - 1;
}
//Accessor
public int getCurrentCount()
{return currentCount;
}
public int getCounterAdd()
{return counterAdd;
}
public void outputCurrentCount()
{System.out.println("The count is " + currentCount);
}
//tostring
public String toString()
{
return "Current count is " + currentCount;
}
}
Here is the class code to run the program - I have only gotten this far.
Code:
import java.util.*;
public class CounterTester {
public static void main(String[] args) {
// Make a new counter
Counter counter = new Counter();
System.out.println("Initial value is " + counter.getCurrentCount());
// Test the increment and toString() methods.
counter.counterAdd();
counter.counterAdd();
System.out.println(
"After two increments, value is " + counter.toString());
}
}
I get the error message "cannot find symbol method counterAdd". What does this mean?
Thanks,
Gary