In: Computer Science
• P8.2 Simulate a tally counter that can be used to admit a limited number of people. First, the limit is set with a call public void setLimit(int maximum) If the count button was clicked more often than the limit, simulate an alarm by printing out a message “Limit exceeded”.
Counter class:
public class P8_2 {
private int value = 0;
private int maximum;
public int getValue() {
return value;
}
public void count() {
if (value + 1 > maximum) {
System.out.println("Limit exceeded");
} else {
value = value + 1;
}
}
public void reset() {
value = 0;
}
public void undo() {
value -= 1;
}
public void setLimit(int maximum) {
this.maximum = maximum;
}
}
main java class:(must import counter class i.e. P8_2)
public class Main
{
public static void main(String[] args)
{
P8_2 counter = new P8_2();
counter.setLimit(7);
counter.count();
counter.count();
counter.count();
counter.count();
counter.count();
counter.count();
counter.count();
counter.count();//prints Limit
exceeded as count called more than 7
counter.count();
}
}