Question

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 should be no input method or other mutator methods. The only method that can set the counter is the one that sets it to zero. Also, include a toString method and an equals method. Write a program (or programs) to test all the methods in your class definition

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Counter
{
    private int value;

    /**
     Sets the counter to zero
     */

    public void setZero()
    {
        value = 0;
    }

    /**
     Increment counter by one
     */

    public void increment()
    {
        value++;
    }

    /**
     Decrements value by one, but does not go below zero
     */

    public void decrement()
    {
        value--;
        if(value < 0) {
            value = 0;
        }
    }

    /**
     Returns the counter's value.
     */

    public int getValue()
    {
        return value;
    }

    /**
     Displays the counter's value.
     */

    public void printCounter()
    {
        System.out.println(value);
    }

    public static void main(String[] args)
    {
        Counter testCounter = new Counter();

        testCounter.setZero();
        System.out.println("After setZero() testCounter = " + testCounter.getValue());

        int i;
        for(i = 0; i < 10; i++)
        {
            testCounter.increment();
        }

        System.out.println("After 10 increments testCounter = " + testCounter.getValue());
        testCounter.printCounter();

        for(i = 0; i < 11; i++)
        {
            testCounter.decrement();
        }

        System.out.println("After 11 decrements testCounter = " + testCounter.getValue());
        testCounter.printCounter();
    }
}
Add a comment
Know the answer?
Add Answer to:
Java HW Define a class called Counter whose objects count things. An object of this class...
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
  • 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...

  • 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...

  • In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...

    In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...

  • Please give the code without errors Define a class named Doctor whose objects are records for...

    Please give the code without errors Define a class named Doctor whose objects are records for a clinic's doctors. Derive this class from the class Person given in Listing 8.1. It's also posted here in this module. A Doctor record has the doctor's name-defined in the class Person,-a specialty as a String (Pediatrician, Obstetrician, General Practitioner, and so on--these are just examples. The type is String.), and an office-visit fee (use the type double). Give your class a constructor and...

  • JAVA Create a Java project to implement a simple Name class. This class will have the...

    JAVA Create a Java project to implement a simple Name class. This class will have the following class variable: First Name, Middle Name, Last Name, and Full Name Create the accessor/getter and mutator/setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Create a main() method to test your project.

  • 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...

  • Starting Out With Java early Objects by Tony Gladdis The class to be modified is found...

    Starting Out With Java early Objects by Tony Gladdis The class to be modified is found in Code Listing 6-29 section on pgs 417-418 in the book 'Starting Out With Java early Objects by Tony Gladdis the 5th Edition' 7. RetailItem Class Modification Modify this chapter’s RetailItem class (which uses an inner class named CostData) to include accessor and mutator methods for getting and setting an item’s wholesale and retail cost. Demonstrate the methods in a program. Thanks

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

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