Software Construction Development - NUCES-FAST - Assignment # 1 - Solution
Software Construction Development - NUCES-FAST - Assignment # 1 - Solution
SCD-THEORY-ASSIGNMENT-1
STANDARD CALCULATOR:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public StandardCalculator() {
textField1.setEditable(false);
textField1.setHorizontalAlignment(JTextField.RIGHT);
a0Button.addActionListener(new NumberListener("0"));
a1Button.addActionListener(new NumberListener("1"));
a2Button.addActionListener(new NumberListener("2"));
a3Button.addActionListener(new NumberListener("3"));
a4Button.addActionListener(new NumberListener("4"));
a5Button.addActionListener(new NumberListener("5"));
a6Button.addActionListener(new NumberListener("6"));
a7Button.addActionListener(new NumberListener("7"));
a8Button.addActionListener(new NumberListener("8"));
a9Button.addActionListener(new NumberListener("9"));
decimalButton.addActionListener(new NumberListener("."));
plusButton.addActionListener(new OperatorListener("+"));
minusButton.addActionListener(new OperatorListener("-"));
multiplyButton.addActionListener(new OperatorListener("*"));
divideButton.addActionListener(new OperatorListener("/"));
@Override
public void actionPerformed(ActionEvent e) {
if (isOperatorPressed) {
textField1.setText(value);
isOperatorPressed = false;
} else {
textField1.setText(textField1.getText() + value);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(textField1.getText());
operator = operatorValue;
isOperatorPressed = true;
}
}
double result = 0;
switch (operator) {
case "+":
result = firstOperand + secondOperand;
break;
case "-":
result = firstOperand - secondOperand;
break;
case "*":
result = firstOperand * secondOperand;
break;
case "/":
if (secondOperand != 0) {
result = firstOperand / secondOperand;
} else {
textField1.setText("Error");
return;
}
break;
}
textField1.setText(String.valueOf(result));
isOperatorPressed = true; // Ready for next operation
}
MenuBar Class :
import javax.swing.*;
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m1.addSeparator();
m1.add(mi5);
m1.add(mi6);
m1.addSeparator();
m1.add(mi7);
m1.add(mi8);
m1.add(mi9);
m1_sub.add(mi10);
m1_sub.add(mi11);
m1_sub.add(mi12);
m1_sub.add(mi13);
m1.add(m1_sub);
m2.add(mi14);
m2.add(mi15);
m2.addSeparator();
m2.add(m2_sub);
m3.add(mi16);
m3.addSeparator();
m3.add(mi17);
mi1.setSelected(true);
mi7.setSelected(true);
return mb;
}
}
-4+4:
ALL THE FEATURES ARE WORKING IN THIS STANDARD CALCULATOR .
- SQR ROOT OF 81 :
- %:
- RECIPROCAL :
- CLEAR :
- BACKSPACE :
- PLUS-MINUS :
ALL MEMORY FUNCTIONS ARE WORKING :
STORED IN MEMORY :
NOW PERFORMING OTHER OPTION WITHOUT STORING
FOR E.G : 3 X 5 :
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public HistoryCalculator() {
// Set the textField properties
textField1.setEditable(false); // Non-editable
textField1.setHorizontalAlignment(JTextField.RIGHT);// Right-
aligned text
// Add implementations for backButton, MC, MR, MS, M+, M-, and C
backButton.addActionListener(e -> backspace());
MCButton.addActionListener(e -> memoryClear());
MRButton.addActionListener(e -> memoryRecall());
MSButton.addActionListener(e -> memoryStore());
mPlusButton.addActionListener(e -> memoryAdd());
mMinusButton.addActionListener(e -> memorySubtract());
cButton.addActionListener(e -> clearAll());
@Override
public void actionPerformed(ActionEvent e) {
if (isOperatorPressed) {
textField1.setText(value);
isOperatorPressed = false;
} else {
textField1.setText(textField1.getText() + value);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(textField1.getText());
operator = operatorValue;
isOperatorPressed = true;
}
}
double result = 0;
switch (operator) {
case "+":
result = firstOperand + secondOperand;
break;
case "-":
result = firstOperand - secondOperand;
break;
case "*":
result = firstOperand * secondOperand;
break;
case "/":
if (secondOperand != 0) {
result = firstOperand / secondOperand;
} else {
textField1.setText("Error");
return;
}
break;
}
try {
double operand1 = Double.parseDouble(tokens[0]);
String operator = tokens[1];
double operand2 = Double.parseDouble(tokens[2]);
double result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
if (operand2 != 0) {
result = operand1 / operand2;
} else {
textField1.setText("Error");
return;
}
break;
}
} catch (NumberFormatException e) {
textField1.setText("Error");
}
}
// Memory functions
private void memoryClear() {
memory = 0; // Clears the memory
}
// Calculate percentage
private void calculatePercentage() {
double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(value / 100));
}
public ScientificCalculator() {
plusButton.addActionListener(new OperatorListener("+"));
minusButton.addActionListener(new OperatorListener("-"));
multiplyButton.addActionListener(new OperatorListener("*"));
divideButton.addActionListener(new OperatorListener("/"));
equalsToButton.addActionListener(e -> calculateResult());
// Scientific functions
sinButton.addActionListener(e -> scientificOperation("sin"));
cosButton.addActionListener(e -> scientificOperation("cos"));
tanButton.addActionListener(e -> scientificOperation("tan"));
logButton.addActionListener(e -> scientificOperation("log"));
piButton.addActionListener(e ->
appendToDisplay(String.valueOf(Math.PI)));
// Memory functions
MCButton.addActionListener(e -> memoryClear());
MRButton.addActionListener(e -> memoryRecall());
MSButton.addActionListener(e -> memoryStore());
mPlusButton.addActionListener(e -> memoryAdd());
mMinusButton.addActionListener(e -> memorySubtract());
@Override
public void actionPerformed(ActionEvent e) {
appendToDisplay(value);
}
}
@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(textField1.getText());
operator = operatorValue;
textField1.setText("");
}
}
switch (operator) {
case "+":
result = firstOperand + secondOperand;
break;
case "-":
result = firstOperand - secondOperand;
break;
case "*":
result = firstOperand * secondOperand;
break;
case "/":
if (secondOperand != 0) {
result = firstOperand / secondOperand;
} else {
textField1.setText("Error");
return;
}
break;
}
textField1.setText(String.valueOf(result));
firstOperand = result; // Continue with the result for further
operations
}
// Scientific operations
private void scientificOperation(String operation) {
double value = Double.parseDouble(textField1.getText());
double result = 0;
switch (operation) {
case "sin":
result = radiansMode ? Math.sin(value) :
Math.sin(Math.toRadians(value));
break;
case "cos":
result = radiansMode ? Math.cos(value) :
Math.cos(Math.toRadians(value));
break;
case "tan":
result = radiansMode ? Math.tan(value) :
Math.tan(Math.toRadians(value));
break;
case "log":
result = Math.log10(value);
break;
}
textField1.setText(String.valueOf(result));
}
// Memory functions
private void memoryClear() {
memory = 0;
}
// Factorial calculation
private void calculateFactorial() {
int value = Integer.parseInt(textField1.getText());
int result = 1;
for (int i = 1; i <= value; i++) {
result *= i;
}
textField1.setText(String.valueOf(result));
}
// Calculate square
private void calculateSquare() {
double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(value * value));
}