Question
I have to make a simple calculator with a java code. How do I fix this to make the program run successfully? I am receiving
I have to make a simple calculator with a java code. How do I fix this to make the program run successfully?
I am receiving an error of Error: Could not find or load main class calculator.Calculator
Here is my code:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package calculator; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/** * * @author Alexandrea */ public class Frame extends JFrame { private double tempNumbers1=0; private double tempNumbers2=0; private byte function = -1
/** * @param args the command line arguments */ public static void main(String[] args) { }
public Frame(){ JButton [] numberButtons = new JButton[10]; for (int i=9, = 0i>=0; i--){ numberButtons[i]=new JButton(Integer.toString(i)); } }
JButton addButton = new JButton("+"); JButton substractButton = new JButton("-"); JButton multiplyButton = new JButton("*"); JButton divideButton = new JButton("/"); JButton cButton = new JButton("C"); JButton enterButton = new JButton("Enter");
resultJText = new JTextField(); resultJText.setPreferredSize(new Dimension(165, 25)); resultJText.setBackground(Color.WHITE); resultJText.setEnabled(false); resultJText.setHorizontalAlignment(4); resultJText.setDisabledTextColor(Color.BLUE); JPanel motherPanel = new JPanel(); motherPanel.setLayout(new BoxLayout(motherPanel, BoxLayout.Y_AXIS));
JPanel textPanel = new JPanel(); textPanel.setPreferredSize(new Dimension(165, 25)); textPanel.add(resultJText); JPanel numberButtonsPanel = new JPanel(); numberButtonsPanel.setPreferredSize(new Dimension(165, 100)); for(int i = 9; i>=0; i--) { numberButtonsPanel.add(numberButtons[i]); } JPanel functionButtonPanel = new JPanel(); functionButtonPanel.setPreferredSize(new Dimension(160, 35)); functionButtonPanel.add(addButton); functionButtonPanel.add(subtractButton); functionButtonPanel.add(multiplyButton); functionButtonPanel.add(divideButton); functionButtonPanel.add(enterButton); functionButtonPanel.add(cButton); for ( int i = 0; i < 10; i++ ) { numberButtonActions[i] = new numberButtonsAction(numberButtons[i]); numberButtons[i].addActionListener(numberButtonActions[i]); } AddButton add = new AddButton(); addButton.addActionListener(add); SubtractButton subtract = new SubtractButton(); substractButton.addActionListener(subtract);
MultiplyButton multiply = new MultiplyButton(); multiplyButton.addActionListener(multiply);
DivideButton divide = new DivideButton(); divideButton.addActionListener(divide); EnterButton enter = new EnterButton(); enterButton.addActionListener(enter); CButton c = new CButton(); cButton.addActionListener(c); motherPanel.add(textPanel); motherPanel.add(numberButtonsPanel); motherPanel.add(functionButtonPanel); add(motherPanel);
setTitle("ButtonTest"); setSize(170, 280); setLocationByPlatform(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); }
private class numberButtonsAction implements ActionListener {
private String c;
public numberButtonsAction(JButton a) { this.c = a.getText(); }
public void actionPerformed(ActionEvent e) { if (!resultJText.getText().equals("0.0")) { resultJText.setText(resultJText.getText() + c); } else { resultJText.setText(""); actionPerformed(e); } } }
} private class AddButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 2; }
}
private class SubtractButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 3; }
} private class MultiplyButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 1; }
} private class DivideButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 0; }
} private class EnterButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { tempNumbers2 = Double.parseDouble(resultJText.getText());
if (function == 0) { resultJText.setText(Double.toString((Math.round((tempNumbers1 / tempNumbers2) * 100)) / 100)); } else if (function == 1) { resultJText.setText(Double.toString(tempNumbers1 * tempNumbers2)); } else if (function == 2) { resultJText.setText(Double.toString(tempNumbers2 + tempNumbers1)); } else if (function == 3) { resultJText.setText(Double.toString(tempNumbers1 - tempNumbers2)); } else { resultJText.setText(String.valueOf(tempNumbers1)); } tempNumbers1 = Double.parseDouble(resultJText.getText()); }
} private class CButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { resultJText.setText(""); tempNumbers1 = 0; tempNumbers2 = 0;
function = -1; }
}
private class DivideButton implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 0; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started