Task:: Subject: Day: Instructor: Program: Time Allowed: Max. Marks: 10 No. of Pages: 1 Assignment No
Task:: Subject: Day: Instructor: Program: Time Allowed: Max. Marks: 10 No. of Pages: 1 Assignment No
Instructions:
Name the solution file with your name and ID and upload.
TASK:
Software Specification
Software Design
package calcualtor;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
double first;
double second;
double result;
String answer;
String operation;
/**
*/
EventQueue.invokeLater(new Runnable() {
try {
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
});
/**
*/
public calcualtor() {
initialize();
/**
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(textField);
textField.setColumns(10);
btn0.addActionListener(new ActionListener() {
textField.setText(number);
});
frame.getContentPane().add(btn0);
btnDot.addActionListener(new ActionListener() {
textField.setText(number);
});
btnDot.setForeground(Color.BLACK);
frame.getContentPane().add(btnDot);
btnPercentage.addActionListener(new ActionListener() {
first= Double.parseDouble(textField.getText());
textField.setText("");
operation = "%";
}
});
btnPercentage.setForeground(Color.BLACK);
frame.getContentPane().add(btnPercentage);
btnEqual.addActionListener(new ActionListener() {
String answer;
second= Double.parseDouble(textField.getText());
if(operation == "+")
answer = String.format("%.2f",result);
textField.setText(answer);
answer = String.format("%.2f",result);
textField.setText(answer);
}
else if(operation == "/")
answer = String.format("%.2f",result);
textField.setText(answer);
answer = String.format("%.2f",result);
textField.setText(answer);
answer = String.format("%.2f",result);
textField.setText(answer);
});
btnEqual.setForeground(Color.BLACK);
frame.getContentPane().add(btnEqual);
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener() {
first= Double.parseDouble(textField.getText());
textField.setText("");
operation = "+";
});
btnAdd.setForeground(Color.BLACK);
frame.getContentPane().add(btnAdd);
btn3.addActionListener(new ActionListener() {
textField.setText(number);
});
btn3.setForeground(Color.BLACK);
btn2.addActionListener(new ActionListener() {
textField.setText(number);
});
btn2.setForeground(Color.BLACK);
frame.getContentPane().add(btn2);
btn1.addActionListener(new ActionListener() {
textField.setText(number);
});
frame.getContentPane().add(btn1);
btn4.addActionListener(new ActionListener() {
textField.setText(number);
});
btn4.setForeground(Color.BLACK);
frame.getContentPane().add(btn4);
btn5.addActionListener(new ActionListener() {
textField.setText(number);
});
btn5.setBounds(70, 213, 50, 50);
btn5.setForeground(Color.BLACK);
frame.getContentPane().add(btn5);
btn6.addActionListener(new ActionListener() {
textField.setText(number);
});
btn6.setForeground(Color.BLACK);
frame.getContentPane().add(btn6);
btnSubtract.addActionListener(new ActionListener() {
first= Double.parseDouble(textField.getText());
textField.setText("");
operation = "-";
}
});
btnSubtract.setForeground(Color.BLACK);
frame.getContentPane().add(btnSubtract);
btn9.addActionListener(new ActionListener() {
textField.setText(number);
});
btn9.setForeground(Color.BLACK);
frame.getContentPane().add(btn9);
btn8.addActionListener(new ActionListener() {
textField.setText(number);
});
btn8.setForeground(Color.BLACK);
frame.getContentPane().add(btn8);
btn7.addActionListener(new ActionListener() {
textField.setText(number);
});
btn7.setForeground(Color.BLACK);
frame.getContentPane().add(btn7);
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first= Double.parseDouble(textField.getText());
textField.setText("");
operation = "*";
});
btnMultiply.setForeground(Color.BLACK);
frame.getContentPane().add(btnMultiply);
btn00.addActionListener(new ActionListener() {
textField.setText(number);
});
btn00.setForeground(Color.BLACK);
frame.getContentPane().add(btn00);
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
textField.setText(null);
});
btnClear.setForeground(Color.BLACK);
frame.getContentPane().add(btnClear);
btnBackSpace.addActionListener(new ActionListener() {
if(textField.getText().length()>0)
strbuild.deleteCharAt(textField.getText().length()-1);
backspace = strbuild.toString();
textField.setText(backspace);
}
}
});
btnBackSpace.setForeground(Color.BLACK);
frame.getContentPane().add(btnBackSpace);
btnDivide.addActionListener(new ActionListener() {
first= Double.parseDouble(textField.getText());
textField.setText("");
operation = "/";
});
btnDivide.setForeground(Color.BLACK);
frame.getContentPane().add(btnDivide);
}
GitHub link of your project.
https://github.com/talhashahid01/calculattor/compare/master?expand=1#diff-
d14e400eefdb8e78565fa58fc1cab56f2274210e46b9ef0dadc3e99a4aa84d8b