Question

In: Computer Science

Define a class called Counter whose internal "count" variable is a basic integer counter. This class...

Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1.

Include an accessor method that returns the current count value (getCount()). There should be no input / setter method or other mutator methods. The only method that can set the counter is the one that sets it to zero (and the constructor).

This class should also override the Object class toString() and equals() methods - refer to the Java APIs for more guidance on how to override this (Chapter 8 also describes this in a little detail, but the API may be more useful): https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html Finally, write a main method (in this class) to test all the methods in your class definition.

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

class Counter {
    int count;

    //Argumented constructor
    Counter(int count) {
        this.count = count;
    }

    //Accessor
    public int getCount(){
        return this.count;
    }

    //Increment and decremenet
    void increment() {
        this.count++;
    }

    void decrement() {
        if (this.count > 0)
            this.count--;
    }

    //Reset count to 0
    void reset() {
        this.count = 0;
    }

    @Override
    public String toString(){
        return "(Count : "+this.count+")";
    }

    @Override
    public boolean equals(Object obj){
        Counter c = (Counter) obj;
        return this.count == c.count;
    }
}


class Main {
    public static void main(String[] args) {
        Counter c = new Counter(5);
        System.out.println(c);
        c.increment();
        c.increment();
        System.out.println(c);
        Counter d = new Counter(8);
        d.decrement();
        System.out.println(c+" == "+d+" : "+c.equals(d));
        c.reset();
        System.out.println("C reset. Count is "+c);
    }
}

Related Solutions

Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
PYTHON A Class for a Deck of Cards We will create a class called Card whose...
PYTHON A Class for a Deck of Cards We will create a class called Card whose objects we will imagine to be representations of playing cards. Each object of the class will be a particular card such as the '3' of clubs or 'A' of spades. For the private data of the class we will need a card value ('A', '2', '3', ... 'Q', 'K') and a suit (spades, hearts, diamond, clubs). Before we design this class, let's see a...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These...
Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These should be declared as public and you should not use automatic properties to declare them. Your class should have a constructor that takes one integer argument. In the constructor you will code if statements to set one of the three class variables (indicators) to 1 if the number sent to the constructor is either equal to zero, negative, or positive. In the class provide...
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and...
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds: 00:00   00:01...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT