0% found this document useful (0 votes)
3 views

calc

The document presents a Java program for a graphical mini calculator using Swing. It includes functionalities for basic arithmetic operations, trigonometric calculations, and advanced operations like square, square root, and percentage. The calculator features a user interface with input fields and buttons for each operation, and handles input validation and error messages.

Uploaded by

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

calc

The document presents a Java program for a graphical mini calculator using Swing. It includes functionalities for basic arithmetic operations, trigonometric calculations, and advanced operations like square, square root, and percentage. The calculator features a user interface with input fields and buttons for each operation, and handles input validation and error messages.

Uploaded by

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

MINI CALCULATOR Akshat Abhishek

1NT23IS019

import java.awt.*;

import javax.swing.*;

import javax.swing.border.EmptyBorder;

public class CalculatorGUI extends JFrame {

private JTextField input1, input2, result;

private JButton addBtn, subBtn, mulBtn, divBtn;

private JButton sinBtn, cosBtn, tanBtn;

private JButton squareBtn, sqrtBtn, percentBtn;

private JLabel label1, label2, labelResult;

public CalculatorGUI() {

setTitle("Mini Calculator");

setSize(550, 350);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setLayout(new BorderLayout(10, 10));

// ── Input Panel ───────────────────────────────────────────

JPanel inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));

label1 = new JLabel(" Input 1:");

label2 = new JLabel(" Input 2:");

labelResult = new JLabel(" Result:");

input1 = createPaddedTextField();

input2 = createPaddedTextField();

result = createPaddedTextField();

result.setEditable(false);
inputPanel.add(label1); inputPanel.add(input1);

inputPanel.add(label2); inputPanel.add(input2);

inputPanel.add(labelResult); inputPanel.add(result);

// ── Trig Panel (North) ────────────────────────────────────

JPanel trigPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));

sinBtn = new JButton("Sin");

cosBtn = new JButton("Cos");

tanBtn = new JButton("Tan");

trigPanel.add(sinBtn);

trigPanel.add(cosBtn);

trigPanel.add(tanBtn);

// ── Arithmetic Panel ──────────────────────────────────────

JPanel arithPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));

addBtn = new JButton("Add");

subBtn = new JButton("Subtract");

mulBtn = new JButton("Multiply");

divBtn = new JButton("Divide");

arithPanel.add(addBtn);

arithPanel.add(subBtn);

arithPanel.add(mulBtn);

arithPanel.add(divBtn);

// ── Advanced Panel ────────────────────────────────────────


JPanel advPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));

squareBtn = new JButton("^2");

sqrtBtn = new JButton("√");

percentBtn = new JButton("%");

advPanel.add(squareBtn);

advPanel.add(sqrtBtn);
advPanel.add(percentBtn);

// ── Combine South Panels ──────────────────────────────────

JPanel southPanel = new JPanel(new GridLayout(2, 1, 0, 0));

southPanel.add(arithPanel);

southPanel.add(advPanel);

// ── Wire up listeners ─────────────────────────────────────

// Arithmetic

addBtn.addActionListener(e -> calculate('+'));

subBtn.addActionListener(e -> calculate('-'));

mulBtn.addActionListener(e -> calculate('*'));

divBtn.addActionListener(e -> calculate('/'));

// Trig

sinBtn.addActionListener(e -> calculateTrig("sin"));

cosBtn.addActionListener(e -> calculateTrig("cos"));

tanBtn.addActionListener(e -> calculateTrig("tan"));

// Advanced

squareBtn.addActionListener(e -> calculateAdvanced("square"));

sqrtBtn.addActionListener(e -> calculateAdvanced("sqrt"));

percentBtn.addActionListener(e -> calculateAdvanced("percent"));

// ── Assemble Frame
────────────────────────────────────────

add(trigPanel, BorderLayout.NORTH);

add(inputPanel, BorderLayout.CENTER);

add(southPanel, BorderLayout.SOUTH);

setVisible(true);

}
private JTextField createPaddedTextField() {

JTextField tf = new JTextField();

tf.setBorder(BorderFactory.createCompoundBorder(

tf.getBorder(), new EmptyBorder(5, 10, 5, 10)

));

return tf;

private void calculate(char operator) {

try {

double a = Double.parseDouble(input1.getText());

double b = Double.parseDouble(input2.getText());

double r = switch (operator) {

case '+' -> a + b;

case '-' -> a - b;

case '*' -> a * b;

case '/' -> {

if (b == 0) {

result.setText("Cannot divide by zero");

yield Double.NaN;

yield a / b;

default -> 0;

};

if (!Double.isNaN(r)) result.setText(String.valueOf(r));

} catch (NumberFormatException ex) {

result.setText("Invalid input");

}
private void calculateTrig(String func) {

try {

double deg = Double.parseDouble(input1.getText());

double rad = Math.toRadians(deg);

double r = switch (func) {

case "sin" -> Math.sin(rad);

case "cos" -> Math.cos(rad);

case "tan" -> Math.tan(rad);

default -> 0;

};

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

} catch (NumberFormatException ex) {

result.setText("Invalid input for trig");

private void calculateAdvanced(String func) {

try {

double a = Double.parseDouble(input1.getText());

double r = switch (func) {

case "square" -> a * a;

case "sqrt" -> a < 0 ? Double.NaN : Math.sqrt(a);

case "percent"-> a / 100.0;

default -> 0;

};

if (Double.isNaN(r)) {

result.setText("Error");

} else {

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

}
} catch (NumberFormatException ex) {

result.setText("Invalid input");

public static void main(String[] args) {

SwingUtilities.invokeLater(CalculatorGUI::new);

You might also like