1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
____ [38]
3 years ago
9

Write a class called Counter that represents a simple tally counter, which might be used to count people as they enter a room. T

he 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.
Computers and Technology
1 answer:
Sonja [21]3 years ago
4 0

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;

   }

}

You might be interested in
1. When a program routine constantly looks a certain condition and executes a command when the condition is met, this is called:
zepelin [54]
This is Called Polling
8 0
3 years ago
F=M×A<br> What is the acceleration of 50kg object pushed with a force of 500 newton?
Ainat [17]
If F=M*A then A=F/M
So acceleration is 500N / 50kg = 10 N/kg = 10 m/s² (since 1N is 1kg·m/s²)
8 0
3 years ago
PLEASE HELP!!! THIS IS DUE TODAY!! WIL MARK BRAINLIEST!!<br> What value does Netflix bring to people
ki77a [65]

Answer:

it allows people to distract themselves from the things going on, it also entertains people

Explanation:

hope this helps

7 0
3 years ago
Read 2 more answers
Question 13(Multiple Choice Worth 5 points)
m_a_m_a [10]

Answer:

Option 2: Insert is the correct answer.

Explanation:

Word processing software provides with the options required to create and format documents. A document may consist of text, images, shapes, tables etc.

The insert tab is used to insert tables, images and shapes in the document.

Hence,

Option 2: Insert is the correct answer.

3 0
3 years ago
Write a function in Java that takes in the int[] array, sorts it using bubble sort, then returns back the sorted array.
diamong [38]
<h2>Answer </h2><h2></h2>

//Class to test the bubble sort

public class BubbleSortTest{

   

//Method to sort the array using bubbleSort

//Takes the array to be sorted as argument

public static int[] bubbleSort(int[] arr) {      

  //Create an integer to hold the length of the array

  int n = arr.length;  

   

  //Create a temporary variable to hold the values of the

                       //array

  //for swapping purposes. Initialize it to zero

  int temp = 0;  

   

  //Cycle through the array element by element

  for(int i = 0; i < n; i++){  

   

   //For each element

   for(int j = 1; j < (n-i); j++){  

     

    //if the element is greater than the element after it

    if(arr[j-1] > arr[j]){  

     //swap the elements  

     temp = arr[j-1];  

     arr[j-1] = arr[j];  

     arr[j] = temp;  

        }    //End of if        

   }   //End of inner for

    }   //End of outer for

 

 //return the sorted array

 return arr;

}

 

   //main method

    public static void main(String [] args){

  //create a sample array

        int [] myArray = {56, 23, 7, 4, 6};

   

  //Call the bubbleSort() method to sort the array

        int [] sortedArray = bubbleSort(myArray);

   

  //Print out the sorted array

        for(int i = 0; i < sortedArray.length; i++){

            System.out.print(sortedArray[i] + " ");

        }

   

    } //End of main method

 

     

}  //End of class BubbleSortTest

<h2>Sample Output</h2>

4 6 7 23 56

<h2>Explanation:</h2>

The code above has been written in Java and it contains comments explaining necessary parts of the code. Please go through the comments. A sample output has also been provided.

For clarity, the following shows the actual function alone without using or including it in a class.

=========================================================

//Method to sort the array using bubbleSort

//Takes the array to be sorted as argument

public static int[] bubbleSort(int[] arr) {  

       

       //Create an integer to hold the length of the array

       int n = arr.length;  

       

       //Create a temporary variable to hold the values of the array

       //for swapping purposes. Initialize it to zero

       int temp = 0;  

       

       //Cycle through the array element by element

       for(int i = 0; i < n; i++){  

           

           //For each element

           for(int j = 1; j < (n-i); j++){  

               

               //if the element is greater than the element after it

               if(arr[j-1] > arr[j]){  

                   //swap the elements  

                   temp = arr[j-1];  

                   arr[j-1] = arr[j];  

                   arr[j] = temp;  

            }  

                     

           }  

        }  

   

   //return the sorted array

   return arr;

}

=========================================================

6 0
3 years ago
Other questions:
  • Describe a situation involving making a copy of a computer program or an entertainment file of some sort for which you think it
    7·1 answer
  • Design two subclasses of Employee…SalariedEmployee and HourlyEmployee. A salaried employee has an annual salary attribute. An ho
    12·1 answer
  • _____ is used to temporarily hold small units of program instructions and data immediately before, during, and after execution b
    10·1 answer
  • The __________ utility has the capability to manage users, create and administer user groups, and apply user rights to those use
    8·1 answer
  • 2. Feet to Inches One foot equals 12 inches. Design a function named feetToInches that accepts a number of feet as an argument,
    8·1 answer
  • If you define a destructor, are you required to define an operator '=' and a copy constructor?
    9·1 answer
  • Because of the relative ease of developing new operating systems, a company with significant market power, such as Microsoft, ma
    7·1 answer
  • Im Gonna Give Brainly a Second Chance<br><br> Everybody deserves a second chance
    13·2 answers
  • Edhesive 7.6 lesson practice python
    11·1 answer
  • Why should we learn Ethereum? Explain.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!