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
sertanlavr [38]
3 years ago
5

Write the definition of a class Counter containing:

Computers and Technology
1 answer:
Dmitriy789 [7]3 years ago
4 0

Answer:

Answer is in java language.

Explanation:

Java Code

a) Creating instance variable

public class Counter {

   int counter;

   int limit;

}

Code Explanation

create a public class and declare variables inside the class block. By doing this, it will create instance variable of that class.

b) Constructor

public Counter(int first, int second){

       this.counter=first;

       this.limit=second;

   }

Code Explanation

Constructor are method having no return type and having exact name as Class name. Constructor are used to initialize the class instance before calling any other method.

c) Increment Method

public void increment(){

       if(this.counter<this.limit){

           counter++;

       }

   }

Code Explanation

void tells the compiler that there is nothing to return from current method. If block is checking the condition that if counter is less then the limit then increment the counter by 1.

d) Decrements Method

public void decrement(){

       if(this.counter>0){

           this.counter--;

       }

   }

Code Explanation

Decrement method will check if counter value is greater then 0 then decrement counter value by 1 in if block.

e) getValue method

public int getValue(){

       return this.counter;

   }

Code Explanation

getValue method will take no parameter as input and return the current counter value by using return statement.

Full Code Example

public class Counter {

   int counter;

   int limit;

   public Counter(int first, int second){

       this.counter=first;

       this.limit=second;

   }

   public void increment(){

       if(this.counter<this.limit){

           counter++;

       }

   }

   public void decrement(){

       if(this.counter>0){

           this.counter--;

       }

   }

public int getValue(){

       return this.counter;

   }

}

You might be interested in
Match the careers with the career clusters.
creativ13 [48]
<h2>Answer and Explanation:</h2>

The picture shows the right careers with their respective career clusters.

4 0
3 years ago
Read 2 more answers
How did New York Governor Hugh Carey handle Sostre’s situation?
Ira Lisetskai [31]

Answer:

The governor found a way to free Sostre without assessing whether or not he was guilty or innocent of drug crime in buffalo.

Explanation:

4 0
3 years ago
Read 2 more answers
The purpose of a good web page design is to make it
meriva

Answer:

Hi , so your answer is that a good web page design is to make it easy to use and meaningful and able to help people .

Explanation:

Really hope i helped , have a nice day :)

5 0
3 years ago
Read 2 more answers
In two to three paragraphs, come up with a way that you could incorporate the most technologically advanced gaming into your onl
Marina CMI [18]

nobody cares about plagiarism dude

7 0
3 years ago
Which statement regarding hard drives is incorrect?
Veseljchak [2.6K]

Answer: I think the answer is

c. solid state drives are less expensive than magnetic hard drives

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • What would 128 kbps look like on a phone? i.e. 4g, 3g, 2g, etc
    9·1 answer
  • For a loop counter, the appropriate data type would be:
    14·1 answer
  • A digital designer might do computer animation or video games true or false
    13·2 answers
  • Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the ga
    12·2 answers
  • Who initially developed what is now known as the internet?
    5·1 answer
  • Consider the following code segment.
    12·1 answer
  • Which view in the View tab of the ribbon is the easiest place to add a header or a footer? Normal view Custom Views Page Layout
    5·2 answers
  • What is the median of 6, 16, 9, 20, 45, 30, and 32?<br> A. 9<br> B. 16<br> C. 20<br> D. 22
    7·1 answer
  • What is the purpose of a hyperlink in a presentation?
    11·1 answer
  • (a) What is the meaning of word 'computer (b) Cive a reason for the evolution of computer (c) Write a weakness of a computer (d)
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!