Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8521579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:52:04+00:00 2026-06-11T06:52:04+00:00

Ok so I am going to try this again. I have a very basic

  • 0

Ok so I am going to try this again. I have a very basic calculator application. I am currently having trouble figuring out how to get my plus() and minus() methods to work. Whenever I run the code, the plus() method is adding currentValue + currentValue. I want it to act like a calculator and add two different integers together. Any suggestions?

digitButton(buttons, 0);
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e)
  {
    myAccumulator.plus();
    updateDisplay();
  }
});
buttons.add(plus);
return buttons;

this is where it is getting called.

And this is my class that I wrote with my plus () method.

public class BasicAccumulator implements Accumulator {

    private int digit;
    private int currentValue;

    public void BasicAccumulator(int digit, int currentValue)
    {
        this.digit = digit;
        this.currentValue = currentValue;
    }

    public void addDigit(int digit)
    {
        currentValue = currentValue * 10 + digit;
    }

    public void plus()
    {
        if (currentValue != 0)
            digit = currentValue;
            currentValue = currentValue + digit;

    }

    public void minus()
    {
        currentValue = currentValue - digit;
    }

    public void clear()
    {
        currentValue = 0;
    }

    public int displayValue()
    {
        return currentValue;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:52:06+00:00Added an answer on June 11, 2026 at 6:52 am

    Think about it. At the time you press +, you presumably have not yet entered the second number. That is, if your calculator is intended to work like most calculators out there…

    Presumably you have the following sequence of events..

    1. Type in digits: digits go in the current accumulator to create a number
    2. Press +: digits are stored away
    3. Type in digits, digits are accumulated as in 1
    4. Press =: current accumulated number is added to previous one.

    For this, I would conceptually store 3 things in the accumulator:

    • The ‘left’ value (left-hand side of the equation) – ie the number that was stored after + was pressed
    • The current operation – nothing initially, but will be set to a value that indicates whether + or -.
    • The current accumulated value (i’ll just call it ‘right’)

    So… Initially, left is zero and operation is nothing (ie ‘clear’).

    State: left(0), operation(empty), right(0)
    

    Type in some digits… These are built up in ‘right’.

    State: left(0), operation(empty), right(22)
    

    Press +. Now, you apply any pending operation to left. In this case the operation is empty, so you just copy the value and clear ‘right’. Then you set the operation to ‘+’

    State: left(22), operation(+), right(0)
    

    You type in more digits, once again building up right.

    State: left(22), operation(+), right(20)
    

    You hit + again. Now the operation is applied. You add right to left and clear right.

    State: left(42), operation(+), right(0)
    

    etc etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very hard to explain but I'm going to try. We run a
OK, I'm going to try my best to explain my problem. I have this
Ok I am going to try this again. I got more information now. I
I'm going to try to explain this best I can I will provide more
I'm going to try and explain this as simply as I can, it's most
Ok, I'm going to try to make this more clear because my last question
Struggling with this one. I have set up a basic email/enquiry form for a
I'm going to try to explain this in simple terms, because it's probably shorter
Hi everyone I am having a problem trying to get this to work. Basically
When I try to check the do not show this screen again box and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.