Results 1 to 9 of 9
  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default 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.

    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);
       }
     }
    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

    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());
    
     	}
     	}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

    Java Code:
    public void setCurrent(int currentCount){
      this.currentCount = currentCount;
      counterAdd = currentCount + 1;
      counterSubtract = currentCount - 1;
    }
    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)

  3. #3
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default 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.
    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;
       }
    
       }
    Here is the class code to run the program - I have only gotten this far.

    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());
    
    
        }
    
    }
    I get the error message "cannot find symbol method counterAdd". What does this mean?

    Thanks,

    Gary

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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().

  5. #5
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    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;
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gnng View Post
    I guess I don't really understand what a method is

    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

  7. #7
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  9. #9
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    Thanks - I'll read up. As I said, I thought I had a pretty good understanding but putting into the OOP mode with setters and getters is messing me up.

Similar Threads

  1. Do getters & setters effect the performance?
    By malaguena in forum New To Java
    Replies: 6
    Last Post: 03-12-2011, 08:46 PM
  2. Replies: 7
    Last Post: 02-02-2010, 12:01 PM
  3. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 06:55 PM
  4. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 02:47 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 11:57 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
  •