Simple tally


Asked by maham237 @ in Computers And Technology viewed by 215 People


Write a class called Counter that represents a simple tally counter, which might be used to count people as they enter a room. The Counter class should contain a single integer as instance data, representing the count. Write a constructor to initialize the count to zero. Write a method called click that increments the count and another method called get Count that returns the current count. Include a method called reset that resets the counter to zero. Finally, create a driver class called CounterTest that creates two Counter objects and tests their methods.

Answered by maham237 @



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;

}

}


Similar Questions

Overcoming fixed mindset: a step-by-step guide to cultivating a growth mindset

Asked by wiki @ in Health viewed by 1233 persons

Which of the following would best complete this list?

Asked by wiki @ in Social Studies viewed by 704 persons

What had the king decided to do before he saw the spider

Asked by vanshika149 @ in English viewed by 1092 persons

Describe the karez in your own words

Asked by rajesh064 @ in English viewed by 1252 persons

What is 8 + (x + 5)?

Asked by jaylord7 @ in Mathematics viewed by 1048 persons

What is the topic of the info grapher

Asked by jesus643 @ in History viewed by 1341 persons

Elephant kills 11 in nepal , woman rescued from a friendly dolphine

Asked by kavin044 @ in English viewed by 1217 persons

(-3,0);slope =2/3

Asked by timothy2 @ in Biology viewed by 1592 persons

Which is an example of situational irony in "wherefore art thou romeo?”

Asked by noah5213 @ in English viewed by 1522 persons

How to enhance knowledge regarding environmental factors through capsim?

Asked by wiki @ in Business viewed by 1694 persons

Most viewed questions in Computers And Technology


Codehs python answer key

Asked by maham237 @ in Computers And Technology viewed by 2083 persons


3.4 6 t shirt shop codehs answers

Asked by maham237 @ in Computers And Technology viewed by 1855 persons



Security infrastructure design document

Asked by wiki @ in Computers And Technology viewed by 1802 persons


Codehs python answers

Asked by maham237 @ in Computers And Technology viewed by 1168 persons


In the business world people are often measured by their

Asked by maham237 @ in Computers And Technology viewed by 1099 persons




Banana fee fi fo fana song

Asked by maham237 @ in Computers And Technology viewed by 796 persons


List 10 www standards that the w3c develops and maintains

Asked by maham237 @ in Computers And Technology viewed by 782 persons


Open the excel workbook revenue xls from the default directory

Asked by maham237 @ in Computers And Technology viewed by 745 persons



In order to protect your computer from the newest viruses

Asked by maham237 @ in Computers And Technology viewed by 739 persons