Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want to answer this question from my textbook, but I do not know where to star

ID: 673731 • Letter: I

Question

I want to answer this question from my textbook, but I do not know where to start. The question is:

We want to add a button to the tally counter in section 3.1 that allows an operator to undo an accidental button click. Provide a method

public void undo()

that simulates such a button. As an added precaution, make sure that clicking the undo button more often than the click button has no effect. (Hint: The call Math.max(n, 0) returns to n if n is greater than zero, zero otherwise.)

I read the book, and I have made my best attempt at the problem, but it just is not working for me.

public class TallyCounter {

private int value;

  

//Adds one to the counter

   public void click() {

       value = value + 1;

   }

  

//Returns the value

   public int getValue() {

       return value;

   }

  

//Resets if there is you want to

   public void reset() {

       value = 0;

   }

  

   public static void main(String[] args) {

      

       //This will be the TallyCounter

       String tally;

       Counter tally = new Counter();

       tally.click();

       tally.click();

       int result = ((TallyCounter) tally).getValue();

   }

}

Explanation / Answer

import java.io.*;
import java.util.*;

class Counter{
   int value;
   public Counter(){}
   int getValue(){
       return value;
   }
   void setValue(int n){
       value = n;
   }
   void reset(){
       value = 0;
   }
   void count(){
       value += 1;
   }
   void undo(){
       value -= 1;
   }
}

class main{
   public static void main(String[] args){
       String tally;
Counter tally = new Counter();
tally.click();
tally.click();
int result = ((TallyCounter) tally).getValue();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote