Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write Week06_yourName_Calculator.java using Java GUI with following as presentation only; Your program can addition, subtraction, division, and multiplication. Your program can save the content as

Write Week06_yourName_Calculator.java using Java GUI with following as presentation only;

Your program can addition, subtraction, division, and multiplication.

Your program can save the content as only one number.

your program can read back saved number.

-must have number buttons

Submit Week06_Calculator.java source file.

Submit one zip file if there is more than one file.

submit screenshot of output along with the editable code

***I am missing the number buttons to perform the math. As of right now my calc performs math but you have to type the numbers in.

Calculator.java

import javax.swing.*;

import java.awt.event.*;

public class Calculator implements ActionListener {

JTextField t1, t2, t3, t4;

JButton b1, b2, b3, b4, b5, b6;

JLabel l1, l2, l3, l4, l5;

double saved = 0, c = 0;

Calculator() {

JFrame f = new JFrame();

l1 = new JLabel("First Number");

l1.setBounds(10, 50, 100, 30);

t1 = new JTextField();

t1.setBounds(150, 50, 100, 20);

l2 = new JLabel("Second Number");

l2.setBounds(10, 100, 220, 30);

t2 = new JTextField();

t2.setBounds(150, 100, 100, 20);

l3 = new JLabel("Result");

l3.setBounds(10, 150, 220, 30);

t3 = new JTextField();

t3.setBounds(150, 150, 100, 20);

t3.setEditable(false);

l4 = new JLabel("Read Number");

l4.setBounds(10, 200, 220, 30);

t4 = new JTextField();

t4.setBounds(150, 200, 100, 20);

l5 = new JLabel("");

l5.setBounds(300, 150, 100, 20);

b1 = new JButton("+");

b1.setBounds(50, 300, 50, 50);

b2 = new JButton("-");

b2.setBounds(110, 300, 40, 50);

b3 = new JButton("*");

b3.setBounds(160, 300, 40, 50);

b4 = new JButton("/");

b4.setBounds(210, 300, 40, 50);

b5 = new JButton("Save");

b5.setBounds(260, 300, 80, 50);

b6 = new JButton("Read saved number");

b6.setBounds(350, 300, 200, 50);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

f.add(l1);

f.add(t1);

f.add(l2);

f.add(t2);

f.add(t3);

f.add(t4);

f.add(l3);

f.add(l4);

f.add(l5);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

f.add(b6);

f.setSize(600, 600);

f.setLayout(null);

f.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

String result = "", savedresult = "";

try {

String n1 = t1.getText();

String n2 = t2.getText();

double a = Double.parseDouble(n1);

double b = Double.parseDouble(n2);

if (e.getSource() == b1) {

c = a + b;

l5.setText("Addition");

result = String.valueOf(c);

} else if (e.getSource() == b2) {

c = a - b;

l5.setText("Subtraction");

result = String.valueOf(c);

} else if (e.getSource() == b3) {

c = a * b;

l5.setText("Multiplication");

result = String.valueOf(c);

} else if (e.getSource() == b4) {

if (b == 0) {

result = "Division by zero";

} else {

c = a / b;

double roundOff = (double) Math.round(c * 100) / 100;

l5.setText("Division");

result = String.valueOf(roundOff);

}

} else if (e.getSource() == b5) {

saved = c;

} else if (e.getSource() == b6) {

savedresult = String.valueOf(saved);

}

} catch (NumberFormatException ex) {

result = "Invalid Input";

} catch (ArithmeticException ex) {

result = "Invalid Operation";

}

t3.setText(result);

t4.setText(savedresult);

}

public static void main(String[] args) {

new Calculator();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

Students also viewed these Databases questions

Question

Discuss the general principles of management given by Henri Fayol

Answered: 1 week ago

Question

Detailed note on the contributions of F.W.Taylor

Answered: 1 week ago