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
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...
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 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.
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
In class need to define two fields: 1. Integer part of the number - ulong; &...
In class need to define two fields: 1. Integer part of the number - ulong; & fractional part of the number - ushort in C# Using C#, create the following methods: 1. Method for adding two prices (parameters for function is 2 numbers, integer(main currency) and fractional parts(Fractional currency)) 2. Method for adding two prices(parameter is object of same type as your class) 3. Method for adding two prices(parameter is string)
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT