The asisgnment is listed below. When I generate mine, I cannot get counter afer
ID: 3790637 • Letter: T
Question
The asisgnment is listed below. When I generate mine, I cannot get counter afer the State after Second Test to correctly populate to 2 & 2
Write a counter class in its own file. This will allow your counter to be used by any program.
The counter must be able to increment and decrement by one. It must never go below zero. toString and equals must be implemented and tested correctly. (toString and equals are covered in depth in your text) System.out.println("”+c1) must work and if(c1.equals(c2)) must work where c1 and c2 are instances of your counter. Include an override of the default constructor that sets the counter to zero and a constructor that allows you to set the count.
Possible Example Output:
--------------------Configuration: --------------------
This program creates and uses Counters
Initial state
counter1 is at 0
counter2 is at 0.
counter1 equals counter2.
Error - Attempted to subtract 1 widget from 0 widgets.
Number of widgets reset to 0.
State after first test
counter1 is at 1.
counter2 is at 0.
counter1 does not equal counter2.
State after second test
counter1 is at 2.
counter2 is at 2.
counter1 equals counter2.
Process completed.
My code:
public class NewCounter {
public static void main(String[] args)
{
// Make a new counter
Counter counter1 = new Counter();
Counter counter2 = new Counter();
// TODO code application logic here
System.out.println("This program creates and uses Counters");
System.out.println();
System.out.println("Initial State");
System.out.println("counter1 is at " + counter1);
System.out.println("counter2 is at " + counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + " counter2");
counter1.Decrement();
counter1.setcounter(1);
counter2.Increment();
System.out.println("");
System.out.println("State after first test");
System.out.println("counter1 is at " + counter1);
System.out.println("counter2 is at " + counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + " counter2");
System.out.println("");
System.out.println("State after second test");
System.out.println("counter1 is at " + counter1);
System.out.println("counter2 is at " + counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + " counter2");
System.out.println("");
System.out.println("Process Complete");
}
}
counter code
public class Counter
{
//private variable
private int count;
//initializes the count to zero
public Counter()
{
count = 0;
}
void setcounter(int i)
{
if(i>0) count=i;
}
public Counter(int i)
{
if(i>0) count=i;
}
//adds 1
void Increment()
{
count++;
}
//subtracts 1
void Decrement()
{
if(count!=0)
count--;
else
System.out.println("");
System.out.println("Error - Attempted to subtract 1 widget from 0 widgets.");
}
// Returns the current count as a string.
@Override
public String toString()
{
return count + "";
}
public boolean equals(Counter other)
{
return(count==other.count);
}
// Resets the count back to 0.
public void reset()
{
count = 0;
System.out.println("Number of widgets reset to 0.");
}
}
My results
This program creates and uses Counters
Initial State
counter1 is at 0
counter2 is at 0
counter1 equals counter2
Error - Attempted to subtract 1 widget from 0 widgets.
State after first test
counter1 is at 1
counter2 is at 1
counter1 equals counter2
State after second test
counter1 is at 1
counter2 is at 1
counter1 equals counter2
Process Complete
Explanation / Answer
Hi,
Please see below the updated classes and output. Plesae comment for any queries/feedbacks.
Thanks,
Anita
NewCounter.java
public class NewCounter {
public static void main(String[] args)
{
// Make a new counter
Counter counter1 = new Counter();
Counter counter2 = new Counter();
// TODO code application logic here
System.out.println("This program creates and uses Counters");
System.out.println();
System.out.println("Initial State");
System.out.println("counter1 is at " + counter1);
System.out.println("counter2 is at " + counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + " counter2");
counter1.Decrement();
counter1.setcounter(1);
System.out.println("");
System.out.println("State after first test");
System.out.println("counter1 is at " + counter1);
System.out.println("counter2 is at " + counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + " counter2");
counter1.Increment(); //incrementing counter1 from 1 to 2.
counter2.Increment(); //incrementing counter2 from 0 to 1.
counter2.Increment(); //incrementing counter2 from 1 to 2.
System.out.println("");
System.out.println("State after second test");
System.out.println("counter1 is at " + counter1);
System.out.println("counter2 is at " + counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + " counter2");
System.out.println("");
System.out.println("Process Completed");
}
}
Counter.java
public class Counter
{
//private variable
private int count;
//initializes the count to zero
public Counter()
{
count = 0;
}
void setcounter(int i)
{
if(i>0) count=i;
}
public Counter(int i)
{
if(i>0) count=i;
}
//adds 1
void Increment()
{
count++;
}
//subtracts 1
void Decrement()
{
if(count!=0)
count--;
else
System.out.println("");
System.out.println("Error - Attempted to subtract 1 widget from 0 widgets.");
}
// Returns the current count as a string.
@Override
public String toString()
{
return count + "";
}
public boolean equals(Counter other)
{
return(count==other.count);
}
// Resets the count back to 0.
public void reset()
{
count = 0;
System.out.println("Number of widgets reset to 0.");
}
}
Sample output:
This program creates and uses Counters
Initial State
counter1 is at 0
counter2 is at 0
counter1 equals counter2
Error - Attempted to subtract 1 widget from 0 widgets.
State after first test
counter1 is at 1
counter2 is at 0
counter1 not equals counter2
State after second test
counter1 is at 2
counter2 is at 2
counter1 equals counter2
Process Completed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.