开发者

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

开发者 https://www.devze.com 2023-03-31 17:16 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about programming within the scope defined in the help center.

Closed 8 years ago.

Improve this question

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 = waitin开发者_如何学PythongOperand - 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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号