0% found this document useful (0 votes)
35 views49 pages

Software Construction Development - NUCES-FAST - Assignment # 1 - Solution

Uploaded by

k224818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views49 pages

Software Construction Development - NUCES-FAST - Assignment # 1 - Solution

Uploaded by

k224818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

22K-4818

SCD-THEORY-ASSIGNMENT-1

STANDARD CALCULATOR:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StandardCalculator {

private JPanel mainpanel;


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;

private double firstOperand = 0;


private double secondOperand = 0;
private String operator = "";
private boolean isOperatorPressed = false;
private double memory = 0;

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("/"));

equalsToButton.addActionListener(e -> calculateResult());

CEButton.addActionListener(e -> clearEntry());

rootButton.addActionListener(e -> calculateSquareRoot());


percentageButton.addActionListener(e -> calculatePercentage());
OneUponXButton.addActionListener(e -> calculateReciprocal());
plusminusButton.addActionListener(e -> toggleSign());

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());
}

private class NumberListener implements ActionListener {


private String value;

public NumberListener(String value) {


this.value = value;
}

@Override
public void actionPerformed(ActionEvent e) {
if (isOperatorPressed) {
textField1.setText(value);
isOperatorPressed = false;
} else {
textField1.setText(textField1.getText() + value);
}
}
}

// Listener for operator buttons


private class OperatorListener implements ActionListener {
private String operatorValue;

public OperatorListener(String operatorValue) {


this.operatorValue = operatorValue;
}

@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(textField1.getText());
operator = operatorValue;
isOperatorPressed = true;
}
}

private void calculateResult() {


secondOperand = Double.parseDouble(textField1.getText());

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
}

private void clearEntry() {


textField1.setText("");
firstOperand = 0;
secondOperand = 0;
operator = "";
isOperatorPressed = false;
}

private void backspace() {


String currentText = textField1.getText();
if (!currentText.isEmpty()) {
textField1.setText(currentText.substring(0,
currentText.length() - 1));
}
}

private void clearAll() {


textField1.setText("");
firstOperand = 0;
secondOperand = 0;
operator = "";
memory = 0;
isOperatorPressed = false;
}

private void memoryClear() {


memory = 0;
}

private void memoryRecall() {


textField1.setText(String.valueOf(memory));
}

private void memoryStore() {


memory = Double.parseDouble(textField1.getText());
}

private void memoryAdd() {


memory += Double.parseDouble(textField1.getText());
}

private void memorySubtract() {


memory -= Double.parseDouble(textField1.getText());
}

private void calculateSquareRoot() {


double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(Math.sqrt(value)));
}

private void calculatePercentage() {


double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(value / 100));
}

private void calculateReciprocal() {


double value = Double.parseDouble(textField1.getText());
if (value != 0) {
textField1.setText(String.valueOf(1 / value));
} else {
textField1.setText("Error");
}
}

private void toggleSign() {


double value = Double.parseDouble(textField1.getText());
value = -value;
textField1.setText(String.valueOf(value));
}

public static void main(String[] args) {


JFrame frame = new JFrame("Calculator");
HistoryCalculator calculator = new HistoryCalculator();
frame.setContentPane(calculator.mainpanel);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}

MenuBar Class :
import javax.swing.*;

public class CalculatorMenuBar {

public static JMenuBar createMenuBar() {


JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("View");
JMenu m2 = new JMenu("Edit");
JMenu m3 = new JMenu("Help");

JMenuItem mi1 = new JRadioButtonMenuItem("Standard");


JMenuItem mi2 = new JRadioButtonMenuItem("Scientific");
JMenuItem mi3 = new JRadioButtonMenuItem("Programmer");
JMenuItem mi4 = new JRadioButtonMenuItem("Statistics");
JMenuItem mi5 = new JMenuItem("History");
JMenuItem mi6 = new JMenuItem("Digit Grouping");
JMenuItem mi7 = new JRadioButtonMenuItem("Basic");
JMenuItem mi8 = new JRadioButtonMenuItem("Unit Conversion");
JMenuItem mi9 = new JRadioButtonMenuItem("Date Calculation");

JMenu m1_sub = new JMenu("Worksheets");


JMenuItem mi10 = new JRadioButtonMenuItem("Mortgage");
JMenuItem mi11 = new JRadioButtonMenuItem("Vehicle Lease");
JMenuItem mi12 = new JRadioButtonMenuItem("Fuel Economy (mpg)");
JMenuItem mi13 = new JRadioButtonMenuItem("Fuel Economy (L/100
km)");

JMenuItem mi14 = new JMenuItem("Copy");


JMenuItem mi15 = new JMenuItem("Paste");
JMenu m2_sub = new JMenu("History");

JMenuItem mi16 = new JMenuItem("View Help");


JMenuItem mi17 = new JMenuItem("About Calculator");

ButtonGroup viewGroup1 = new ButtonGroup();


viewGroup1.add(mi1);
viewGroup1.add(mi2);
viewGroup1.add(mi3);
viewGroup1.add(mi4);

ButtonGroup viewGroup2 = new ButtonGroup();


viewGroup2.add(mi7);
viewGroup2.add(mi8);
viewGroup2.add(mi9);
viewGroup2.add(mi10);
viewGroup2.add(mi11);
viewGroup2.add(mi12);
viewGroup2.add(mi13);

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 :

- MEMORY STORE : 2X5 = 10 , THEN STORE IN MEMORY

STORED IN MEMORY :
NOW PERFORMING OTHER OPTION WITHOUT STORING
FOR E.G : 3 X 5 :

NOW RECALLING MEMORY WITH MR :


- NOW LETS CLEAR MEMORY (MC) AND THEN TRY RECALLING (MR) :
SAME MEMORY ADD AND SUBTRACT ARE ALSO IN WORKING CONDITION

- Test Memory Add (M+) :


Input a number, e.g., 10
Press the memory add button (usually labeled as M+).

Adds into the previous stored 10 with newly 10 so :


Memory Subtract :

Subtracting 5 now with M- :

Now Recalling (MR) :


HISTORY CALCULATOR :

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class HistoryCalculator {


JPanel mainpanel;
private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JList<String> list1; // JList for history display
private DefaultListModel<String> historyModel; // Model to hold history
entries

private double firstOperand = 0;


private double secondOperand = 0;
private String operator = "";
private boolean isOperatorPressed = false;
private double memory = 0; // Memory storage for MC, MR, MS, M+, M-

public HistoryCalculator() {
// Set the textField properties
textField1.setEditable(false); // Non-editable
textField1.setHorizontalAlignment(JTextField.RIGHT);// Right-
aligned text

// Initialize history model and JList


historyModel = new DefaultListModel<>();
list1.setModel(historyModel);

// Add action listeners to number buttons


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("."));

// Add action listeners to operator buttons


plusButton.addActionListener(new OperatorListener("+"));
minusButton.addActionListener(new OperatorListener("-"));
multiplyButton.addActionListener(new OperatorListener("*"));
divideButton.addActionListener(new OperatorListener("/"));

// Add action listener for equals


equalsToButton.addActionListener(e -> calculateResult());

// Add action listener for clear (CE)


CEButton.addActionListener(e -> clearEntry());

// Add other functionalities


rootButton.addActionListener(e -> calculateSquareRoot());
percentageButton.addActionListener(e -> calculatePercentage());
OneUponXButton.addActionListener(e -> calculateReciprocal());
plusminusButton.addActionListener(e -> toggleSign());

// 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());

// Add mouse listener to JList for selecting and reusing history


list1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // Double-click to select an
entry
String selectedEntry = list1.getSelectedValue();
if (selectedEntry != null) {
// Split the expression by operator and calculate
the result
evaluateHistoryEntry(selectedEntry);
}
}
}
});
}

// Listener for number buttons


private class NumberListener implements ActionListener {
private String value;

public NumberListener(String value) {


this.value = value;
}

@Override
public void actionPerformed(ActionEvent e) {
if (isOperatorPressed) {
textField1.setText(value);
isOperatorPressed = false;
} else {
textField1.setText(textField1.getText() + value);
}
}
}

// Listener for operator buttons


private class OperatorListener implements ActionListener {
private String operatorValue;

public OperatorListener(String operatorValue) {


this.operatorValue = operatorValue;
}

@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(textField1.getText());
operator = operatorValue;
isOperatorPressed = true;
}
}

// Calculate the result when '=' is pressed


private void calculateResult() {
secondOperand = Double.parseDouble(textField1.getText());

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;
}

// Display the result


textField1.setText(String.valueOf(result));

// Add the expression (without the result) to the history


String historyEntry = firstOperand + " " + operator + " " +
secondOperand;
historyModel.addElement(historyEntry); // Add the entry to the
JList model

isOperatorPressed = true; // Ready for next operation


}

// Evaluate an expression from the history and display the result


private void evaluateHistoryEntry(String expression) {
// Split the expression into operands and operator
String[] tokens = expression.split(" ");
if (tokens.length < 3) {
textField1.setText("Error");
return;
}

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;
}

// Display the result in the text field


textField1.setText(String.valueOf(result));

} catch (NumberFormatException e) {
textField1.setText("Error");
}
}

// Clear the display


private void clearEntry() {
textField1.setText("");
firstOperand = 0;
secondOperand = 0;
operator = "";
isOperatorPressed = false;
}

// Backspace functionality to delete the last digit


private void backspace() {
String currentText = textField1.getText();
if (!currentText.isEmpty()) {
textField1.setText(currentText.substring(0,
currentText.length() - 1));
}
}

// Clear all (C button)


private void clearAll() {
textField1.setText("");
firstOperand = 0;
secondOperand = 0;
operator = "";
memory = 0;
isOperatorPressed = false;
}

// Memory functions
private void memoryClear() {
memory = 0; // Clears the memory
}

private void memoryRecall() {


textField1.setText(String.valueOf(memory)); // Recalls memory to
display
}

private void memoryStore() {


memory = Double.parseDouble(textField1.getText()); // Stores the
current display value to memory
}
private void memoryAdd() {
memory += Double.parseDouble(textField1.getText()); // Adds
current display value to memory
}

private void memorySubtract() {


memory -= Double.parseDouble(textField1.getText()); // Subtracts
current display value from memory
}

// Calculate square root


private void calculateSquareRoot() {
double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(Math.sqrt(value)));
}

// Calculate percentage
private void calculatePercentage() {
double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(value / 100));
}

// Calculate reciprocal (1/x)


private void calculateReciprocal() {
double value = Double.parseDouble(textField1.getText());
if (value != 0) {
textField1.setText(String.valueOf(1 / value));
} else {
textField1.setText("Error");
}
}

// Toggle between positive and negative


private void toggleSign() {
double value = Double.parseDouble(textField1.getText());
value = -value;
textField1.setText(String.valueOf(value));
}

public static void main(String[] args) {


JFrame frame = new JFrame("History Calculator");
HistoryCalculator calculator = new HistoryCalculator();
frame.setContentPane(calculator.mainpanel);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar()); // Assuming
there's a CalculatorMenuBar class for the menu bar
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setVisible(true);
}
}
SCIENTIFIC CALCULATOR :
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

public class ScientificCalculator {


private JPanel mainpanel;
private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JButton closeRoundButton;
private JButton openRoundButton;
private JButton inButton;
private JButton invButton;
private JButton aButton;
private JRadioButton degreesRadioButton;
private JRadioButton radiansRadioButton;
private JRadioButton gradsRadioButton;
private JButton nFactorialButton;
private JButton xSqaureButton;
private JButton sinButton;
private JButton sinhButton;
private JButton intButton;
private JButton yButton2;
private JButton yButton;
private JButton cosButton;
private JButton coshButton;
private JButton dmsButton;
private JButton a3Button2;
private JButton xButton;
private JButton tanButton;
private JButton tanhButton;
private JButton piButton;
private JButton a10Button;
private JButton logButton;
private JButton modButton;
private JButton expButton;
private JButton fEButton;
private JButton emptybutton;

private double memory = 0;


private String operator = "";
private double firstOperand = 0;
private boolean radiansMode = true; // Default to radians mode

public ScientificCalculator() {

ButtonGroup modeGroup = new ButtonGroup();


modeGroup.add(degreesRadioButton);
modeGroup.add(radiansRadioButton);
modeGroup.add(gradsRadioButton);

// Set default selection to Radians


radiansRadioButton.setSelected(true);
// Add action listeners for common buttons
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("/"));
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)));

rootButton.addActionListener(e -> calculateSquareRoot());


OneUponXButton.addActionListener(e -> reciprocal());
nFactorialButton.addActionListener(e -> calculateFactorial());
xSqaureButton.addActionListener(e -> calculateSquare());

// Memory functions
MCButton.addActionListener(e -> memoryClear());
MRButton.addActionListener(e -> memoryRecall());
MSButton.addActionListener(e -> memoryStore());
mPlusButton.addActionListener(e -> memoryAdd());
mMinusButton.addActionListener(e -> memorySubtract());

// Mode switching (Degrees, Radians, Grads)


degreesRadioButton.addActionListener(e -> radiansMode = false);
radiansRadioButton.addActionListener(e -> radiansMode = true); //
Default to radians
gradsRadioButton.addActionListener(e -> radiansMode = false);
}

// Number button listener


private class NumberListener implements ActionListener {
private String value;

public NumberListener(String value) {


this.value = value;
}

@Override
public void actionPerformed(ActionEvent e) {
appendToDisplay(value);
}
}

// Operator button listener


private class OperatorListener implements ActionListener {
private String operatorValue;
public OperatorListener(String operatorValue) {
this.operatorValue = operatorValue;
}

@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(textField1.getText());
operator = operatorValue;
textField1.setText("");
}
}

// Append text to display field


private void appendToDisplay(String text) {
textField1.setText(textField1.getText() + text);
}

// Calculate result based on the operator


private void calculateResult() {
double secondOperand = Double.parseDouble(textField1.getText());
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));
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;
}

private void memoryRecall() {


textField1.setText(String.valueOf(memory));
}

private void memoryStore() {


memory = Double.parseDouble(textField1.getText());
}

private void memoryAdd() {


memory += Double.parseDouble(textField1.getText());
}

private void memorySubtract() {


memory -= Double.parseDouble(textField1.getText());
}

// Calculate square root


private void calculateSquareRoot() {
double value = Double.parseDouble(textField1.getText());
textField1.setText(String.valueOf(Math.sqrt(value)));
}

// Reciprocal function (1/x)


private void reciprocal() {
double value = Double.parseDouble(textField1.getText());
if (value != 0) {
textField1.setText(String.valueOf(1 / value));
} else {
textField1.setText("Error");
}
}

// 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));
}

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Scientific Calculator");
ScientificCalculator calculator = new ScientificCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}
PROGRAMMER CALCULATOR :
import javax.swing.*;

public class ProgrammerCalculator {


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JButton aButton;
private JButton bButton;
private JButton cButton1;
private JButton dButton;
private JButton eButton;
private JButton fButton;
private JButton modButton;
private JButton button8;
private JButton roRButton;
private JButton xorButton;
private JButton rshButton;
private JButton andButton;
private JButton button13;
private JButton button14;
private JButton roLButton;
private JButton orButton;
private JButton lshButton;
private JButton notButton;
private JRadioButton decRadioButton;
private JRadioButton octRadioButton;
private JRadioButton binRadioButton;
private JRadioButton qwordRadioButton;
private JRadioButton dwordRadioButton;
private JRadioButton wordRadioButton;
private JRadioButton byteRadioButton;
private JRadioButton hexRadioButton;

private JPanel mainpanel;

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Programmer Calculator");
ProgrammerCalculator calculator = new ProgrammerCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}
STATISTICS CALCULATOR :
import javax.swing.*;

public class StatisticsCalculator {


private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CADButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JButton σn1Button;
private JTextField a0TextField;
private JPanel mainpanel;

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Statistics Calculator");
StatisticsCalculator calculator = new StatisticsCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}
UNIT CONVERSION CALCULATOR :
import javax.swing.*;

public class UnitConversionCalculator {


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JComboBox comboBox1;
private JTextField textField2;
private JComboBox comboBox2;
private JTextField textField3;
private JComboBox comboBox3;

private JPanel mainpanel;

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Scientific Calculator");
UnitConversionCalculator calculator = new
UnitConversionCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}
DATE CALCULATION CALCULATOR :
import javax.swing.*;

public class DateCalculationCalculator {


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JComboBox comboBox1;
private JTextField textField2;
private JTextField textField3;
private JTextField textField4;
private JTextField textField5;
private JButton calculateButton;

private JPanel mainpanel;

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Date Calculator");
DateCalculationCalculator calculator = new
DateCalculationCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}
MORTGAGE CALCULATOR :
import javax.swing.*;

public class MortgageCalculator {


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JComboBox comboBox1;
private JTextField textField2;
private JTextField textField3;
private JTextField textField4;
private JTextField textField5;
private JButton caculateButton;

private JPanel mainpanel;

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Mortgage Calculator");
MortgageCalculator calculator = new MortgageCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}
FUEL ECONOMY (MPG) CALCULATOR :
import javax.swing.*;

public class FuelMPGCalculator {


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JButton equalsToButton;
private JComboBox comboBox1;
private JTextField textField2;
private JTextField textField3;
private JButton calculateButton;

private JPanel mainpanel;


public JPanel getMainPanel() {
return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Fuel MPG Calculator");
FuelMPGCalculator calculator = new FuelMPGCalculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}

FUEL ECONONMY (L/100) CALCULATOR :


import javax.swing.*;

public class FuelL100Calculator {


private JTextField textField1;
private JButton backButton;
private JButton MCButton;
private JButton MRButton;
private JButton MSButton;
private JButton mPlusButton;
private JButton mMinusButton;
private JButton a7Button;
private JButton a4Button;
private JButton CEButton;
private JButton cButton;
private JButton plusminusButton;
private JButton rootButton;
private JButton percentageButton;
private JButton OneUponXButton;
private JButton divideButton;
private JButton multiplyButton;
private JButton a9Button;
private JButton a6Button;
private JButton a8Button;
private JButton a5Button;
private JButton a1Button;
private JButton a2Button;
private JButton a3Button;
private JButton minusButton;
private JButton a0Button;
private JButton decimalButton;
private JButton plusButton;
private JComboBox comboBox1;
private JTextField textField2;
private JTextField textField3;
private JButton calculateButton;
private JButton equalsToButton;

private JPanel mainpanel;

public JPanel getMainPanel() {


return mainpanel;
}

public static void main(String[] args) {


JFrame frame = new JFrame("Fuel L/100 Calculator");
FuelL100Calculator calculator = new FuelL100Calculator();
frame.setContentPane(calculator.getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setJMenuBar(CalculatorMenuBar.createMenuBar());
}
}

You might also like