Question

Please help me with this Java problem. Would greatly appreciate comments in the code as well:...

Please help me with this Java problem. Would greatly appreciate comments in the code as well:

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem.  Let me know if you have any doubts or if you need anything to change.

Thank You !!


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


public class Counter {

    private int count = 0;

    public Counter() {
        this.count = 0;
    }

    public void incrementCounter() {
        this.count++;
    }

    public void decrementCounter() {
        this.count--;
    }

    public void resetCounter() {
        this.count = 0;
    }

    public Counter(int count) {

        this.count = count >= 0 ? count : 0;
    }


    public int getCount() {
        return count;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Counter counter = (Counter) o;
        return count == counter.count;
    }

    public static void main(String[] args) {

        Counter counterA = new Counter(13);
        Counter counterB = new Counter();
        Counter counterC = new Counter(3);

        System.out.println("counterA = " + counterA);
        System.out.println("counterB = " + counterB);
        System.out.println("counterC = " + counterC);

        System.out.println("A = B ? " + counterA.equals(counterB));
        System.out.println("A = C ? " + counterA.equals(counterC));

        for (int i = 1; i <= 10; i++) counterB.incrementCounter();
        for (int i = 1; i <= 3; i++) counterA.decrementCounter();
        System.out.println("counterA = " + counterA); 
        System.out.println("counterB = " + counterB);
        System.out.println("A = B ? " + counterA.equals(counterB));
        counterC.resetCounter();
        System.out.println("Reset counterC = " + counterC);
    }

}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Counter.java 43 public static void main (String [] args) [ 44 45 Counter counterA = new Counter(13); 46 = new Counter () Coun

Add a comment
Know the answer?
Add Answer to:
Please help me with this Java problem. Would greatly appreciate comments in the code as well:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1 Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the...

    Problem 1 Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the Wall.” Your program should print the number of bottles in English, not as a number. For example: Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer, Take one down, pass it around, Ninety-eight bottles of beer on the wall. ... One bottle of beer on the wall, One bottle of beer, Take one down, pass it around, Zero bottles of beer on...

  • Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n....

    Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n. The function computes the exclusive OR of the hashcodes (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--) of the first n strings in the array (result type int).

  • Java HW Define a class called Counter whose objects count things. An object of this class...

    Java HW Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Include an accessor method that returns the current count value and a method that outputs the count to the screen. There...

  • *MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE...

    *MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE DIRECTIONS BELOW* Write a Java class that takes a full name (first and last) as inputted by the user, and outputs the initials. Call the class Initials. The first and last names should be entered on the same input line i.e. there should be only one input to your program. For example, if the name is Jane Doe, the initials outputted will be J...

  • Complete code and answer question in comments: Package hw4; import java.util.ArrayList; public c...

    Complete code and answer question in comments: Package hw4; import java.util.ArrayList; public class WordGame { /* * Returns all strings that appear * as a consecutive horizontal or vertical sequence of letters * (left-right, right-left, up-down, or down-up) * in the array board and also appear in dict. * Note that the same word may appear multiple times * on the board, and will then be multiple times in * the returned array. * * dict is assumed to be...

  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded...

    In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded based on the efficiency of your implementation. You will not be awarded any points if you use simple nested loops to implement the below tasks. You should use one or more of the below data structures: - ArrayList : - JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - Tutorial: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html Question You are provided with...

  • Programming Lab 4 (Chapter 4) There is one checkpoint, make sure you call your professor to...

    Programming Lab 4 (Chapter 4) There is one checkpoint, make sure you call your professor to get checked. 4.1 Write a program that prompts the user for two integers and then prints The sum The difference The product The average The distance (absolute value of the difference) The maximum (the larger of the two) The minimum (the smaller of the two) Hint: The max, min, and absolute value methods are declared in the Math class. Lookup https://docs.oracle.com/javase/8/docs/api/java/lang/Math. html the API...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT