Results 1 to 9 of 9
- 03-20-2011, 08:00 PM #1
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
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.
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?Java 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); } }
Thanks in advance,
Gary
Java 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()); } }
- 03-20-2011, 08:17 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What is the argument to the setter methods?
Also, in your setters you are not actually setting the countAdd and countSubtract instance variables, you are just changing the currentCount.
It may also be nice to make it so when you set the current count it updates add and subtract.
This may not be exactly what you want but I do believe it is correct. If it isn't you can then just understand why you are getting an error(because the method isn't taking an argument)Java Code:public void setCurrent(int currentCount){ this.currentCount = currentCount; counterAdd = currentCount + 1; counterSubtract = currentCount - 1; }
- 03-27-2011, 08:00 PM #3
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
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.
Here is the class code to run the program - I have only gotten this far.Java 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; } }
I get the error message "cannot find symbol method counterAdd". What does this mean?Java 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()); } }
Thanks,
Gary
-
It's saying that the class Counter doesn't have a method "counterAdd()", and all you have to do is check your code and see that the error is correct -- there is no method named counterAdd().
- 03-27-2011, 08:17 PM #5
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
I guess I don't really understand what a method is - I modified the code below. I thought this would be the method? Obviously I am wrong, but what am I missing?
public void setCurrent(int currentCount)
{
this.currentCount = currentCount;
counterAdd = currentCount + 1;
counterSubtract = currentCount - 1;
}
public void setCounterAdd()
{this.counterAdd = counterAdd;
}
-
Don't do anything until you learn what this is as it is key to creating Java programs, and all you'll be doing is guessing -- which never works with programming. Please check out these decent basic tutorials: Methods
- 03-27-2011, 08:33 PM #7
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
I think where I am getting confused is that we have just jumped into setters and getters. This is an online college class so there is no actual teacher teaching anything, just book assignments. I understand the concept of methods, just not sure what I am doing wrong and where to actually put it.
Why is this not a method?
public void setCounterAdd()
{this.counterAdd = counterAdd;
counterAdd = currentCount + 1;
}
And thanks for your help. I realize that this is a basic question but learning online with no teacher is very tough. I am brand new to programming and the learning curve is pretty steep. Thanks.
-
That is a method, but it doesn't make sense since your trying to set a field, the counterAdd field but your method takes no parameter. If you need to set a field you need to be able to pass the value that you're going to set into the method via a parameter. Have you read the link I gave you so you know what a parameter is? You will need to study the tutorials to get a basic understanding of Java and its vocabulary for us to be able to help you.
- 03-27-2011, 08:48 PM #9
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
Do getters & setters effect the performance?
By malaguena in forum New To JavaReplies: 6Last Post: 03-12-2011, 07:46 PM -
Reading arrays from different classes without getters and setters
By Psyclone in forum New To JavaReplies: 7Last Post: 02-02-2010, 11:01 AM -
Positions and values, getters, setters
By Malus in forum New To JavaReplies: 10Last Post: 01-23-2010, 05:55 PM -
Getters and Setters
By lheviathan in forum New To JavaReplies: 4Last Post: 11-02-2009, 01:47 AM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks