Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP ME FIX THE ERRORS PLEASE package lab 1 ; / * * * The counter class implements a counter that will roll over to

HELP ME FIX THE ERRORS PLEASE
package lab1;
/**
* The counter class implements a counter that will roll over to the initial
* value when it hits the maximum value.
*
* @author Charles Hoot
* @version 5.0
*/
public class Counter
{
private int minimum;
private int maximum;
private int currValue;
// PUT PRIVATE DATA FIELDS HERE
/**
* The default constructor for objects of class Counter. Minimum is 0 and the maximum
* is the largest possible integer.
*/
public Counter()
{
this.minimum =0;
this.maximum = Integer.MAX_VALUE;
this.currValue = minimum;
// ADD CODE FOR THE CONSTRUCTOR
}
/**
* The alternate constructor for objects of class Counter. The minimum and maximum values are given as parameters.
* The counter starts at the minimum value.
* @param min The minimum value that the counter can have
* @param max The maximum value that the counter can have
**/
public Counter(int min, int max)
{
this.minimum = min;
this.maximum = max;
this.currValue = minimum;
// ADD CODE FOR THE ALTERNATE CONSTRUCTOR
}
/**
* Determine if two counters are in the same state
*
* @param otherObject the object to test against for equality
* @return true if the objects are in the same state
*/
public boolean equals(Object otherObject)
{
boolean result = true;
if (otherObject instanceof Counter)
{
Counter othercounter =(Counter) otherObject;
result = this.currValue == othercounter.currValue;
// YOUR CODE GOES HERE
}
return result;
}
/**
* Increases the counter by one
*/
public void increase()
{
if(currValue maximum){
currValue++;
}
else {
currValue = minimum;
}
// ADD CODE TO INCREASE THE VALUE OF THE COUNTER
}
/**
* Decreases the counter by one
*/
public void decrease()
{
if(currValue > minimum){
currValue--;// ADD CODE TO INCREASE THE VALUE OF THE COUNTER
}
else {
currValue = maximum;
}
}
/**
* Get the value of the counter
*
* @return the current value of the counter
*/
public int value()
{
// CHANGE THE RETURN TO GIVE THE CURRENT VALUE OF THE COUNTER
return currValue;
}
/**
* Accessor that allows the client to determine if the counter
* rolled over on the last count
*
* @return true if the counter rolled over
*/
public boolean rolledOver()
{
// CHANGE THE RETURN TO THE ROLLOVER STATUS OF THE COUNTER
return currValue == minimum;
}
/**
* Override the toString method to provide a more informative
* description of the counter
*
* @return a descriptive string about the object
*/
public String toString()
{
// CHANGE THE RETURN TO A DESCRIPTION OF THE COUNTER
return "Counter" + currValue +"(Min: "+ minimum +", Max: "+ maximum +")" ;
}
}TESTING the constructor
Trying min max
Passes
Trying min=max
**** Fails - no exception thrown
Trying min >max
**** Fails - no exception thrown
Finished constructor testing
TESTING the toString method
Displaying the counter using tostring:
Counter1(Min: 1, Max: 9)
The counter should have the value 1
the minimum should be 1, the maximum should be 9
it should not have rolled over
Finished tostring testing
TESTING the equals method
trying two counters that should be in the same state
passes
trying two counters that should be in a different state
passes
trying two counters that should be in a different state
**** fails
Finished equals testing
TESTING the increase method
Increasing counter 1 once
passes
passes
Increasing counter 1 again
passes
passes
Increasing counter 1 a third time
passes
passes
Increasing counter 2 until it rolls over
passes
**** fails - wrong number of increases; count was 0
Increasing counter 3 until it rolls over
passes
**** fails - wrong number of increases; count was 0
Finished increase testing
TESTING the decrase method
Decreasing the counter once
passes
**** fails - should roll over
Decreasing the counter again
passes
**** fails - should not roll over
Decreasing the counter a third time
passes
**** fails - should roll over
Decrease counter 2 twice. then decrease counter 2 until it rolls over again
image text in transcribed

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

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions