I have written a java calculator program that doesn\'t do calculations and would
ID: 3560537 • Letter: I
Question
I have written a java calculator program that doesn't do calculations and would like to have it work like a real calculator. Here is the code:
package calculator;
import javax.swing.*;
import java.awt.*;
public class Calc extends JFrame {
public Calc()
{
super.setTitle("Calculator");
new Font("SansSerif", Font.BOLD, 16);
JTextField jtext = new JTextField(" 0 ");
jtext.setSize(250,25);
jtext.setHorizontalAlignment(JTextField.RIGHT);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(jtext, BorderLayout.NORTH);
JPanel buttonsPanel = new JPanel(new GridLayout(4,4,5,5));
buttonsPanel.setBackground(Color. GRAY);
JButton JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");;
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton decimal = new JButton(".");
JButton equal = new JButton("=");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");
JButton minus = new JButton("-");
JButton add = new JButton("+");
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(divide);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(multiply);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(minus);
buttonsPanel.add(decimal);
buttonsPanel.add(zero);
buttonsPanel.add(equal);
buttonsPanel.add(add);
seven. setBackground(Color.BLACK);
seven.setForeground(Color.WHITE);
eight.setBackground(Color.WHITE);
eight.setForeground(Color.BLACK);
nine.setBackground(Color.BLACK);
nine.setForeground(Color.WHITE);
divide.setBackground(Color.WHITE);
divide.setForeground(Color.BLACK);
four.setBackground(Color.WHITE);
four.setForeground(Color.BLACK);
five.setBackground(Color.BLACK);
five.setForeground(Color.WHITE);
six.setBackground(Color.WHITE);
six.setForeground(Color.BLACK);
multiply.setBackground(Color.BLACK);
multiply.setForeground(Color.WHITE);
one.setBackground(Color.BLACK);
one.setForeground(Color.WHITE);
two.setBackground(Color.WHITE);
two.setForeground(Color.BLACK);
three.setBackground(Color.BLACK);
three.setForeground(Color.WHITE);
minus.setBackground(Color.WHITE);
minus.setForeground(Color.BLACK);
decimal.setBackground(Color.WHITE);
decimal.setForeground(Color.BLACK);
zero.setBackground(Color.BLACK);
zero.setForeground(Color.WHITE);
equal.setBackground(Color.WHITE);
equal.setForeground(Color.BLACK);
add.setBackground(Color.BLACK);
add.setForeground(Color.WHITE);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(textPanel, BorderLayout.NORTH);
mainPanel.add(buttonsPanel, BorderLayout.CENTER);
add(mainPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calc frame = new Calc();
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
Please don't change the GUI, just add the math and number portion to it. Full points will be awarded to a working code only!
Explanation / Answer
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); Maths Maths = new Maths(); double answer = 0; double inputA, inputB; char operator; boolean done = false; while (done == false) { System.out.print("Please enter your sum: "); inputA = input.nextDouble(); operator = input.next().charAt(0); inputB = input.nextDouble(); switch (operator) { case '+': answer = Maths.add(inputA, inputB); break; case '-': answer = Maths.subtract(inputA, inputB); break; case '*': answer = Maths.multiply(inputA, inputB); break; case '/': answer = Maths.divide(inputA, inputB); break; case '^': answer = Maths.power(inputA, inputB); break; } System.out.println(answer); } input.close(); } } public class Maths { double add(double a, double b) { double answer = a+b; return answer; } double subtract(double a, double b) { double answer = a-b; return answer; } double multiply(double a, double b) { double answer = a*b; return answer; } double divide(double a, double b) { double answer = a/b; return answer; } double power(double a, double b){ double answer =a; for (int x=2; xRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.