Answer:
Code is in java
Explanation:
Code is self explanatory, added come comments to explain more about the code.
public class CounterDriver {
public static void main(String[] args){
// Creating counter first object
Counter counter1 = new Counter();
//performing 2 clicks
counter1.click();
counter1.click();
// displaying counter 1 click count
System.out.println("Total Number of counter1 clicks :"+counter1.getCount());
// resetting counter 1 click count
counter1.reset();
// again displaying click count which will be 0 after reset
System.out.println("Total Number of counter1 clicks :"+counter1.getCount());
// Same operation goes with counter 2
// the only difference is that it performs 3 clicks
Counter counter2 = new Counter();
counter2.click();
counter2.click();
counter2.click();
System.out.println("Total Number of counter2 clicks :"+counter2.getCount());
counter2.reset();
System.out.println("Total Number of counter2 clicks :"+counter2.getCount());
}
}
class Counter{
int count;
// defining constructor which will initialize count variable to 0
public Counter(){
this.count=0;
}
// adding click method to increment count whenever it's called
public void click(){
this.count++;
}
// getCount method will return total count
public int getCount(){
return this.count;
}
// reset method will set count to 0
public void reset(){
this.count = 0;
}
}