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

  • Home
  • SEARCH
  • 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 6362051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:51:27+00:00 2026-05-24T23:51:27+00:00

Wanted advice and comments on A Java Calculator of making code better readable, Useable

  • 0

Wanted advice and comments on A Java Calculator of making code better readable, Useable or coding tips what I could Change or keep.

HERE IS MY MAIN CLASS

package calculator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

 /**
*
* @author abdimaden
*/
 public class Calculator extends JFrame implements ActionListener {

/**
 * @param args the command line arguments
 */
public static JTextField display;
JPanel displayPanel = new JPanel();
JButton numb;
JButton opButton;
boolean userIsInTheMiddleOfTyping;
CalculatorBrain brain = new CalculatorBrain();

@Override
public void setFont(Font font) {
    super.setFont(font);
}

public static void main(String[] args) {
    // TODO code application logic here

    Calculator calc = new Calculator();

}

public Calculator() {
    setLayout(new FlowLayout());
    setSize(500, 500);
    setTitle("Calculator: By Cabdifitaar Aden (CMProductions)");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    Layout();
    add(displayPanel);

    setVisible(true);
}

public void Layout() {

    displayPanel.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    //display

    display = new JTextField("0", SwingConstants.RIGHT);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 5;
    displayPanel.add(display, c);
    display.setFont(new Font("arial", Font.BOLD, 36));
    display.setEditable(false);
    display.setHorizontalAlignment(JTextField.RIGHT);
    display.setPreferredSize(new Dimension(500, 30));
    display.setBackground(Color.LIGHT_GRAY);
    //display.setHorizontalTextPosition(JLabel.RIGHT_ALIGNMENT);
    display.addActionListener(this);

    //operation buttons first row MC M+ M- MR
    opButton = new JButton("MC");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("M+");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("M-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("MR");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    //operation buttons second C +/- / *
    opButton = new JButton("C");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("+/-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("/");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("operation");

    opButton = new JButton("*");
    opButton.addActionListener(this);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.setActionCommand("operation");

    // buttons third row 7 8 9 -

    numb = new JButton("7");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("8");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("9");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton("-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    // buttons third row 4 5 6 +
    opButton.setActionCommand("operation");

    numb = new JButton("4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("5");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("6");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton("+");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("operation");
    // buttons third row 1 2 3 =

    numb = new JButton("1");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 5;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 5;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton("=");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 90;
    c.gridx = 3;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 3;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("operation");

    /// last row 0 . 
    numb = new JButton("0");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 90;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 2;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton(".");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 6;
    c.gridwidth = 1;
    c.gridheight = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.setActionCommand("operation");
    // numb.addActionListener(this);
    //opButton.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    // add your event handling code here

    // only the digit buttons will trigger the fallow if statment 
    if ("digit".equals(e.getActionCommand())) {
        //see which button trigged action    
        JButton digit = (JButton) e.getSource();
        //get the text from the button
        String digitPressed = digit.getText();
        if (userIsInTheMiddleOfTyping) {
            //display.setText(digitPressed);                    
            display.setText(display.getText() + digitPressed);
        } else {
            display.setText(digitPressed);
            userIsInTheMiddleOfTyping = true;
        }

    } else if ("operation".equals(e.getActionCommand())) {
        JButton button = (JButton) e.getSource();

        if (userIsInTheMiddleOfTyping) {
            //gets tricked when an operation button is pressed
            brain.setOperand(Integer.parseInt(display.getText()));
            userIsInTheMiddleOfTyping = false;

        }
        String operation = button.getText();

        double result = brain.perforumOperation(operation);
        String newResult = Double.toString(result);
        brain.formatDisplay(newResult);


    } else if ("specialOperation".equals(e.getActionCommand())) {
        //see which button trigged action    
        JButton specialOperationButton = (JButton) e.getSource();
        //get the text from the button
        String specialOperation = specialOperationButton.getText();

        brain.performSpecialOperation(specialOperation);
        userIsInTheMiddleOfTyping = false;

    }

}
 }

CALCULATOR BRAIN HERE IS WHERE ALL THE OPERATION AND CALCULATIONS HAPPEN

package calculator;

/**
*
* @author CMP
*/
import static javax.swing.JOptionPane.*;

public class CalculatorBrain {

//    Calculator calc = new Calculator();
double operAnd;
String waitingOperation;
double waitingOperand;
String plus = "+";
String specialOperation;
double memory = 0;

/// method to set the operAnd
public void setOperand(double anOperAnd) {
    operAnd = anOperAnd;

}

public void performWaitingOperation() {

    if (plus.equals(waitingOperation)) {

        operAnd = waitingOperand + operAnd;

    } else if ("-".equals(waitingOperation)) {

        operAnd = waitingOperand - operAnd;

    } else if ("/".equals(waitingOperation)) {
        String testFor0 = Double.toString(operAnd);
        if (testFor0.equals("0.0")) {
            showMessageDialog(null, "You can not divide by 0");
        } else {
            operAnd = waitingOperand / operAnd;
        }
    } else if ("*".equals(waitingOperation)) {

        operAnd = waitingOperand * operAnd;
    } else if ("C".equals(waitingOperation)) {
        operAnd = 0;
    }
}

public double perforumOperation(String operation) {
    if (operation.equals("sqrt")) {
    } else {
        performWaitingOperation();
        waitingOperation = operation;
        waitingOperand = operAnd;

    }

    return operAnd;

}

public void performSpecialOperation(String specialOp) {
    //specialOperation include C MC M+ M- MR +/-
    specialOperation = specialOp;

    //this if statement rests the screen and operAnd value to 0
    if ("C".equals(specialOperation)) {

        operAnd = 0;
        Calculator.display.setText("0");

    } else if ("+/-".equals(specialOperation)) {
        // this if stament checks whether the number is negative or positive

        if (operAnd < 0) {

            operAnd = Math.abs(operAnd);

        } else if (operAnd > 0) {

            operAnd = -operAnd;

        }

        String DisplayValue = Double.toString(operAnd);
        formatDisplay(DisplayValue);

    } else if ("MR".equals(specialOperation)) {

        String memoryValue = Double.toString(memory);

        formatDisplay(memoryValue);
    } else if ("M+".equals(specialOperation)) {

        memory = memory + Double.parseDouble(Calculator.display.getText());
        formatDisplay(Double.toString(memory));

    } else if ("MC".equals(specialOperation)) {

        memory = 0;
        formatDisplay(Double.toString(memory));

    } else if ("M-".equals(specialOperation)) {

        memory = memory - Double.parseDouble(Calculator.display.getText());
        formatDisplay(Double.toString(memory));

    }


}

public void formatDisplay(String value) {
    String newValue = value;
    if (value.endsWith(".0")) {
        newValue = value.substring(0, value.length() - 2);
        Calculator.display.setText("" + newValue);
    } else {
        Calculator.display.setText("" + newValue);
    }

}
}

IF YOU FIND THIS HELP FULL PLEASE COMMENT OF WHY
MANY THANKS JUST LOOKING FOR A WAY TO BECOME A BETTER PROGRAMMER AND GET THE RIGHT STYLE

  • 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-05-24T23:51:28+00:00Added an answer on May 24, 2026 at 11:51 pm

    I wouldn’t use StackOverflow for that (though I’m interested whether it will work 😉

    I would rather use some plugins to make your code better readable, usable, robust, performant, correct … Suggested road-map:

    • Install checkstyle to get acquainted with coding conventions and styles. Once you are familiar with it, rather soften the rules.
    • Use Findbugs and PMD to find bugs, risky or performance-critical parts in your code.
    • Start using metrics, e.g. about dependencies (e.g. jdepend), cyclomatic complexity (e.g.JavaNCSS) and test coverage (e.g. eclemma),
    • Oh yes, and Write a lot of test cases (beforehand) for your code, e.g. to see how usable it is.

    From here, you can go to

    • continuous integration, e.g. using Jenkins together with Sonar.
    • further agile practices to improve, especially using code reviews and pair programming
    • get into clean code development (see Java tool to improve my Clean Code Development for support)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A very basic question it is but I wanted expert advice that is why
I'm about to port a smallish library from Java to Python and wanted some
I was just reading this article and wanted SO folks advice: Q: Should delete
I wanted to get some advice, I have started on a new project to
Wanted to convert <br/> <br/> <br/> <br/> <br/> into <br/>
Wanted to get some consensus around a UI feature I'm working on right now.
I wanted some of those spiffy rounded corners for a web project that I'm
I wanted to show the users Name Address (see www.ipchicken.com ), but the only
I wanted to emulate a popular flash game, Chrontron, in C++ and needed some
I wanted to generate one fix view using interface builder, but the size of

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.