Class Counter { private in count; public Counter(int intialCount){ count = initi
ID: 3881689 • Letter: C
Question
Class Counter {
private in count;
public Counter(int intialCount){
count = initialCount;
}
public Count(){
count = 0;
}
public void increment(){
count += 1;
}
public void reset(){
count = 0;
}
public int getValue(){
return count;
}
}
1. How many constructors does this class have?
2. How many methods does this class have?
3. Write a declaration that declares a Counter variable named ctr and initializes it to contain a Counter object that contains the number 0.
4. Write a statement that increments the value stored in the ctr object.
5. Write a statement that changes the values stored in the ctr object to zero.
6. Write a statement that prints the values stored in the ctr object.
Explanation / Answer
1. How many constructors does this class have?
Answer: 2
2. How many methods does this class have?
Answer: 3
3. Write a declaration that declares a Counter variable named ctr and initializes it to contain a Counter object that contains the number 0.
Answer: Counter ctr = new Counter();
4. Write a statement that increments the value stored in the ctr object.
Answer: ctr.increment();
5. Write a statement that changes the values stored in the ctr object to zero.
Answer: ctr.reset();
6. Write a statement that prints the values stored in the ctr object.
Answer: System.out.println(ctr.getValue());