Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CounterTester.java public class CounterTester { public static void main(String[] args) { Counter c1 = new Counter(); c1.setLimit(100); for (int i = 0; i < 50;


CounterTester.java


public class CounterTester
{
public static void main(String[] args)
{
  Counter c1 = new Counter();
  c1.setLimit(100);
 
  for (int i = 0; i < 50; i++)
  {
  c1.count();
  }
  System.out.println("Tally: " + c1.getValue());
  System.out.println("Expected:Tally: 50");
 
  for (int i = 0; i < 51; i++)
  {
  c1.count();
  }
  System.out.println("Tally: " + c1.getValue());
  System.out.println("Expected:Limit Exceeded");
  System.out.println("Tally: 0");
 
  Counter c2 = new Counter();
  c2.setLimit(0);
 
  for (int i = 0; i < 5; i++)
  {
  c2.count();
  }
  System.out.println("Tally: " + c2.getValue());
  System.out.println("Expected:Limit exceededLimit exceededLimit exceededLimit exceededLimit exceeded");
  System.out.println("Tally: 0");
}
}

Counter.java (has todo)

/**
This class models a tally counter.
*/
public class Counter
{
private int value;
private int max;

public void setLimit(int maximum)
{
max = maximum;
}

/**
Gets the current value of this counter.
@return the current value
*/
public int getValue()
{
return value;
}

/**
Advances the value of this counter by 1.
*/
public void count()
{
 //-----------Start below here. To do: approximate lines of code = 4
 // increments value by 1. if value exceeds limit, print "Limit Exceeded" and reset value to 0
 
 
 
 
 
 
 //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}

/**
Resets the value of this counter to 0.
*/
public void reset()
{
value = 0;
}
}

P.S: Do include output as a screenshot so we know it works well

This class models a tally counter. Difficulty: Easy See the following files: * CounterTester.java * Counter.java (has todo) Approximate total lines of code required: 4

Step by Step Solution

3.40 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

CounterTesterjava public class CounterTester public static void mainString args Counter c1 new Count... 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

Accounting for Governmental and Nonprofit Entities

Authors: Earl R. Wilson, Jacqueline L Reck, Susan C Kattelus

15th Edition

978-0256168723, 77388720, 256168725, 9780077388720, 978-007337960

More Books

Students also viewed these Programming questions