Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Code: Create a class, RolloverCounter, that can be used to create RolloverCounter objects. Each RolloverCounter object is a simple counter that counts from 0

JAVA Code:

Create a class, RolloverCounter, that can be used to create RolloverCounter objects. Each RolloverCounter object is a simple counter that counts from 0 up to some specified maximum value (unique to each counter). When the count of a RolloverCounter exceeds its maximum, it will "roll over" back to 0. If the count of a RolloverCounter falls below 0, it will "roll over" to its maximum.

For example, consider a RolloverCounter that is created with a maximum of 4:

RolloverCounter myCount = new RolloverCounter(4); //create the counter with count of 0, maximum of 4

If it is incremented several times the count will proceed as follows:

myCount.increment(); //count will be 1 myCount.increment(); //count will be 2 myCount.increment(); //count will be 3 myCount.increment(); //count will be 4 myCount.increment(); //count will exceed maximum! roll over to count of 0

If it is decremented several times the count will proceed as follows:

myCount.decrement(); //count will fall below 0! roll over to count of 4 (maximum) myCount.decrement(); //count will be 3 myCount.decrement(); //count will be 2 myCount.decrement(); //count will be 1 myCount.decrement(); //count will be 0

Your task is to create the code for the RolloverCounter class in RolloverCounter.java. The class should include:

1) private variables to store the counter's count and maximum 2) a constructor method to create a counter with a count set to 0 and a maximum value specified by a value passed in as an argument 3) methods to increment, decrement, reset, and get the current count

You can download the provided RolloverCounter.java file that contains comments with instructions for each part. There is also a provided CounterTest.java file that contains a test client program that should work if the RolloverCounter.java file is completed successfully. This file is provided to test the RolloverCounter class, you should not modify it. CounterTest is the main program, the RolloverCounter is a class that you are creating to define that new data type and its functionality.

When you are ready to submit, submit both the CounterTest.java file and your completed RolloverCounter.java file.

Starter Code:

/* * Write the code for the RolloverCounter class below * * In this RolloverCounter.java file you will implement a RolloverCounter class that should include: 1) A private variable to store the current count. 2) Another private variable to store the maximum value this counter can count up to. 3) A constructor with a single integer parameter used to set the maximum counter value. The count should be set to 0. 4) An increment() method that increases the count value by 1, but sets it back to 0 if the count goes above the maximum. no parameters, returns nothing 5) A decrement() method that decreases the count value by 1, but sets it to the maximum value if it goes below 0. no parameters, returns nothing 6) A getCount() method that returns an integer of the current count value. no parameters. 7) A reset() method that sets the count value back to 0. no parameters, returns nothing.

Notes: + This class is meant to be like the values on a car's odometer readout that tick upwards through the digits 0 to 9 and then roll back to 0. In this case, the maximum can be any positive number. + The count should always be a number between 0 and the maximum value that was set when the counter was created. */

public class RolloverCounter { //TODO: Write the code for the class here. //private variables //constructor //methods

}

Test Code:

/* * a program that uses the RolloverCounter class to test its functionality * You don't need to change anything in the program...it is just here to help you test your code in the RolloverCounter * When this program is run the output should be: ************************************************* creating new counters... creating counter c1 with max value = 5... creating counter c2 with max value = 3... incrementing the counts 10 times and printing counts... c1: 1 c2: 1 c1: 2 c2: 2 c1: 3 c2: 3 c1: 4 c2: 0 c1: 5 c2: 1 c1: 0 c2: 2 c1: 1 c2: 3 c1: 2 c2: 0 c1: 3 c2: 1 c1: 4 c2: 2 decrementing the counts 7 times and printing counts... c1: 3 c2: 1 c1: 2 c2: 0 c1: 1 c2: 3 c1: 0 c2: 2 c1: 5 c2: 1 c1: 4 c2: 0 c1: 3 c2: 3 resetting counters... c1: 0 c2: 0 ***************************************************** */

public class CounterTest { public static void main(String args[]) { System.out.println("creating new counters..."); System.out.println("creating counter c1 with max value = 5..."); RolloverCounter c1 = new RolloverCounter(5); System.out.println("creating counter c2 with max value = 3..."); RolloverCounter c2 = new RolloverCounter(3); System.out.println(" incrementing the counts 10 times and printing counts..."); for (int i=1; i<=10; i++) { c1.increment(); c2.increment(); System.out.println("c1: " + c1.getCount() + "\tc2: " + c2.getCount()); } System.out.println(" decrementing the counts 7 times and printing counts..."); for (int i=1; i<=7; i++) { c1.decrement(); c2.decrement(); System.out.println("c1: " + c1.getCount() + "\tc2: " + c2.getCount()); } System.out.println(" resetting counters..."); c1.reset(); c2.reset(); System.out.println("c1: " + c1.getCount() + "\tc2: " + c2.getCount()); } //end main } //end CounterTest

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

b. Does senior management trust the team?

Answered: 1 week ago

Question

How will the members be held accountable?

Answered: 1 week ago