Results 1 to 3 of 3
  1. #1
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default set variables with just one method?

    I have a Counter class and a driver file for that class. Counter class contains methods to increase the counter by one and decrease by one. I have to use only one method to set the counter to 0, however, up till now I have always set the value like so:

    count1.addOne(x)

    When I compile my code I get a "no argument error" whenever I try to use the increment/decrement methods:



    Counter count1 = new Counter();
    count1.setCount();
    count1.addOne();
    count1.addOne();
    count1.addOne();

    count1.minusOne();
    count1.minusOne();

    This is my program. The Counter class being first, the driver class after.

    Java Code:
    public class Counter 
    {
    	private static int count; 
    	public Counter()
    	{
    			count = 0;
    	}
    	
    	public void setCount()
    	{
    		count = 0;
    	}
    	
    		public void addOne(int count)
    	{
    		count++;
    	
    	}
    	
    		public void minusOne(int count)
    	{
    		 if (count > 0){ 
             count-=1;
    	  }
      }
    		public int getCounter()
    	{
    	
    		return count;
    	}
    	public boolean equal(Counter other)
    	{
    		return count == other.count;
    	}
    	public void printCount()
    	{
    		System.out.println("Current count is: " + count);
    	}
    	
    	public String stringCount()
    	
    	{
    		return "Count is at " + count;
    	}
    }
    // end class
    
    
    
    
    public class Counter_Test
     {
    public static void main(String args[])
    	{
    		Counter count1 = new Counter();
    		count1.setCount();
    		count1.addOne();
    		count1.addOne();
    		count1.addOne();
    		
    		count1.minusOne();
    		count1.minusOne();
    		
    		System.out.println(count1.getCounter());
    		count1.printCount();
    		count1.setCount();
    		count1.addOne();
    		count1.minusOne();
    		count1.minusOne();
    		count1.printCount();
    		
    		Counter count2 = new Counter();
    		System.out.println(count2.stringCount());
    		System.out.println(count1.equal(count2));
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: set variables with just one method?

    I get a "no argument error" whenever I try to use the increment/decrement methods:
    Yes, none of the listed methods take arguments so when you call them you should not pass an arg.
    However the setCount method needs to have a value to set the instance variable to. If the method is going to "clear" the value in the variable, clearCount() would be a better name for the method.

    Note: Passing an arg to a method named: addOne() that is going to add 1 to an instance variable doesn't make any sense. What would the method do with it?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: set variables with just one method?

    I presume this is just for practice since a Counter class (imho) is overkill. However, I recommend you do the following:

    1. Change your equal(Counter count) to equals(Object obj). Then do an instanceof and cast to Counter for the
    comparison.
    2. As long as you have an equals method, you may as well add a hashCode method. Returning count should be sufficient.
    See the Object class for the signature.

    The above comply with overriding equals and hashCode in Object. If you every want to use this or any other
    homegrown class in a map you will need to do this.

    3. Don't make count static. It should be an instance field so that each Counter class is unique.


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. calling variables from a method....
    By jwood in forum New To Java
    Replies: 19
    Last Post: 07-22-2013, 04:03 AM
  2. Help with instance variables in a method
    By mstratmann in forum New To Java
    Replies: 3
    Last Post: 05-07-2013, 11:13 AM
  3. Replies: 1
    Last Post: 11-06-2012, 08:25 PM
  4. Calling variables from another method
    By jwl in forum Advanced Java
    Replies: 1
    Last Post: 10-16-2012, 04:45 AM
  5. Final variables in method
    By noobplus in forum New To Java
    Replies: 2
    Last Post: 03-04-2012, 03:32 PM

Tags for this Thread

Posting Permissions

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