6 Replies - 332 Views - Last Post: 08 February 2012 - 12:55 PM Rate Topic: -----

Topic Sponsor:

#1 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-February 12

Tally counter

Posted 07 February 2012 - 05:46 PM

i wanna create a class that simulates a tally
counter. A tally counter object typically starts at zero and goes up increments of 1 up to 999. The only
other function of the counter should be to reset it to zero. Your class should have a single instance data
field that keeps track of the current tally. It should also include two constructors, a default constructor
that sets the tally to zero and a single-argument constructor that sets the tally to an integer value between
0 and 999. If the argument is not within that range, set the tally to zero. Write a mutator method that
increments the tally by one – if the tally is at 999, the next increment should “roll” the tally back to zero.
Another mutator should allow the tally to be reset to zero no matter what value it currently holds.
Include also an accessor method and a toString() method.
but i'm stuck with it here what i've got:
 package question1and2;

	public class TallyCounter{
		int count;
	
	public TallyCounter(){
		count=0; }
	
	
	//accessors
	public int getValue(){return count;}
	
	public String toString(){return "TallyCounter["+count+"]";}
	
	//muttators
	public void addOne(){ 
		do{
			count=count+1;;
		}while(count<=999);}
			
	public void reset(){
		if(count>999)
		count=0;}
	
	 public static void main(String[] args)
	  {
		 TallyCounter c1=new TallyCounter();
		 TallyCounter c2=new TallyCounter();
		 c2.addOne();
		  int result=c1.getValue();
		 
		 System.out.println(c1);
		 System.out.println(c2);
		 System.out.println(result);
	  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tally counter

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 640
  • View blog
  • Posts: 4,287
  • Joined: 24-October 08

Re: Tally counter

Posted 07 February 2012 - 07:34 PM

which part are you stuck with?
Was This Post Helpful? 0
  • +
  • -

#3 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-February 12

Re: Tally counter

Posted 07 February 2012 - 08:14 PM

View Postmostyfriedman, on 07 February 2012 - 07:34 PM, said:

which part are you stuck with?

I can't come up with the constructor that sets the tally to an integer value between 0 and 999.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 640
  • View blog
  • Posts: 4,287
  • Joined: 24-October 08

Re: Tally counter

Posted 07 February 2012 - 08:35 PM

Alright well first start by creating a constructor that accepts an int as argument. then inside your constructor you'll want to use an if statement to check if the passed int is a valid one. I am sure you can figure out the rest.
Was This Post Helpful? 1
  • +
  • -

#5 Sheph  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 294
  • View blog
  • Posts: 777
  • Joined: 12-October 11

Re: Tally counter

Posted 07 February 2012 - 09:46 PM

Your addOne() method is a little off. There is no need for a loop there. Just increment the count by 1 one time for each time the method is called. Then check to see if the new count is > 999 and if it is, set it equal to count%999.
public void addOne() {
   count++;
   if( count > 999 )
      count = count%999;
}
Also, the reset method shouldn't be conditional. When the user of this class calls the reset method it should reset the count to 0 regardless of the current value. I think you had these 2 methods combined a bit.
public void reset() {
   count = 0;
}

Was This Post Helpful? 1
  • +
  • -

#6 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-February 12

Re: Tally counter

Posted 08 February 2012 - 12:22 PM

tks for help. I have another ques, i've done with this class.
that what i've got.

package question1and2;

	public class TallyCounter{
		int count;
	
	//accessors
	
	public TallyCounter(){count=0;}
	//	constructor for integer value between 0 ad 999?????
	public TallyCounter(int num){
		if (count<0||count >999)
			count=0;
		else
			count=num;
	}
	 	
				
	public String toString(){return "TallyCounter["+count+"]";}
	
	//muttators 
	// increments the tally by one, if count>999, back to zero. 
	public void addOne(){ 
		count++;
		if(count>999)
			count=count%999;
	}
	//allow the tally to be reset to zero
	public void reset(){count=0;}
	
	 }



and right now i wanna create another class to check my TallyCounter class
and in this class i create two objects, using each of your constructors. Display the post-construction state of each of them. Select one of my tally counters and have it increase a random number of times. Display its state after the
increases. Show that your tally counter rolls properly after reaching 999 and that your reset works.

second class
package question1and2;

public class TallyTester {

	/**
	 * @param args
	 */
	public static void main(String[] args)
	  {
		int count=0;
		System.out.println("Two counter ccreated: ");
		 TallyCounter c1=new TallyCounter();
		 TallyCounter c2=new TallyCounter();
		 System.out.println(c1);
		 c2.addOne();
		
		 System.out.println(c2);
		 System.out.println("Random increase for counter 2... tracking below:");
		 
		 //???
		 
		 c2.reset();
		 System.out.println("Counter 2 after reset: "+c2);
		 
	  }
	}



the ques is how to icrease counter a random numbers of times..
as a result to got this
Two counters created: TallyCounter[0] and TallyCounter[998]
Random increase for counter2...tracking count below:
999
0
1
2
3
Counter 2 after reset: TallyCounter[0]
Was This Post Helpful? 0
  • +
  • -

#7 max-archy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-February 12

Re: Tally counter

Posted 08 February 2012 - 12:55 PM

Never mind, tks, i've got that!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1