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());      ...
In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList...
In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList ofBestFriend objects. Then, create a menu that will continue to display itself, and process the requests of the user, until the user requests to exit the menu (loop). The menu should have 5 menu options: 1.) Add a BestFriend to the arrayList called myBestFriends; 2.) Change a BestFriend in the arrayList; 3.) Remove a BestFriend from the arrayList; 4.) Display all the objects in...
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...
Written in C++ Define a class for a type called Fraction. This class is used to...
Written in C++ Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator. Also include a member function that returns the values of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the...
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT