0% found this document useful (0 votes)
29 views5 pages

Calculator

This document describes a Java program that creates a graphical user interface (GUI) calculator application. The calculator allows users to perform basic arithmetic operations and some scientific functions. It uses Swing components like JTextField and JButton to build the interface.

Uploaded by

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

Calculator

This document describes a Java program that creates a graphical user interface (GUI) calculator application. The calculator allows users to perform basic arithmetic operations and some scientific functions. It uses Swing components like JTextField and JButton to build the interface.

Uploaded by

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

import java.awt.

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

class Calculator extends JFrame {


private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20);
private JTextField textfield;
private boolean number = true;
private String equalOp = "=";
private CalculatorOp op = new CalculatorOp();
private final Font INPUT_FONT = new Font("helvitica",Font.BOLD, 30);

public Calculator() {
textfield = new JTextField("", 10);
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(INPUT_FONT);
textfield.setPreferredSize( new Dimension( 8, 150 ) );
textfield.setBackground(Color.decode("#353C51"));
textfield.setForeground(Color.WHITE);
textfield.setOpaque(true);
ActionListener numberListener = new NumberListener();
String buttonOrder = "123456789.0-";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
buttonPanel.setBackground(Color.decode("#353C51"));
//buttonPanel.setForeground(Color.WHITE);
buttonPanel.setOpaque(true);

for (int i = 0; i < buttonOrder.length(); i++) {


String key = buttonOrder.substring(i, i+1);
if (key.equals(" ")) {
buttonPanel.add(new JLabel(""));
} else {
JButton button = new JButton(key);
button.addActionListener(numberListener);
button.setFont(BIGGER_FONT);
buttonPanel.add(button);
button.setBackground(Color.decode("#506680"));
button.setForeground(Color.WHITE);
button.setOpaque(true);
}
}
ActionListener operatorListener = new OperatorListener();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(7, 7, 7, 7));
String[] opOrder = {"clr","=","+", "-", "*",
"/","abs","⌈x⌉","⌊x⌋","sin","cos","log","√","³√","rnd","%","exp","x^2","x^3","N!","
Σ"};
for (int i = 0; i < opOrder.length; i++) {
JButton button = new JButton(opOrder[i]);
button.addActionListener(operatorListener);
button.setFont(BIGGER_FONT);
button.setBackground(Color.decode("#2F4562"));
button.setForeground(Color.WHITE);
button.setOpaque(true);
panel.add(button);
panel.setBackground(Color.decode("#353C51"));
panel.setOpaque(true);
}
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout(4, 4));
pan.add(textfield, BorderLayout.NORTH );
pan.add(buttonPanel , BorderLayout.CENTER);
pan.add(panel , BorderLayout.EAST);
pan.setBackground(Color.decode("#353C51"));
pan.setOpaque(true);
pan.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
this.setContentPane(pan);
this.pack();
this.setTitle("Advance Calculator IVOS +6");
this.setResizable(false);
}
private void action() {
number = true;
textfield.setText("");
equalOp = "=";
op.setTotal("");
}
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String displayText = textfield.getText();
if (e.getActionCommand().equals("sin"))
{
textfield.setText("" +
Math.sin(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("cos"))
{
textfield.setText("" +
Math.cos(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("log"))
{
textfield.setText("" +
Math.log(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("√"))
{
textfield.setText("" +
Math.sqrt(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("⌈x⌉"))
{
textfield.setText("" +
Math.ceil(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("⌊x⌋"))
{
textfield.setText("" +
Math.floor(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("abs"))
{
textfield.setText("" +
Math.abs(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("³√"))
{
textfield.setText("" +
Math.cbrt(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("rnd"))
{
textfield.setText("" +
Math.round(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("exp"))
{
textfield.setText("" +
Math.exp(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("x^2"))
{
textfield.setText("" +
Math.pow(Double.valueOf(displayText).doubleValue(), 2));

}
else
if (e.getActionCommand().equals("x^3"))
{
textfield.setText("" +
Math.pow(Double.valueOf(displayText).doubleValue(), 3));

else
if (e.getActionCommand().equals("N!"))
{
String input= displayText;
long factorial=1;

for(long j=Long.parseLong(input);j>=1;j--)
{
factorial=factorial*j;
}
textfield.setText("" + factorial);

else
if (e.getActionCommand().equals("clr"))
{
textfield.setText("");
}

else
{
if (number)
{

action();
textfield.setText("");

}
else
{
number = true;
if (equalOp.equals("="))
{
op.setTotal(displayText);
}else if (equalOp.equals("+"))
{
op.add(displayText);
}
else if (equalOp.equals("-"))
{
op.subtract(displayText);
}
else if (equalOp.equals("*"))
{
op.multiply(displayText);
}
else if (equalOp.equals("/"))
{
op.divide(displayText);
}
else if (equalOp.equals("%"))
{
op.mod(displayText);
}

textfield.setText("" + op.getTotalString());
equalOp = e.getActionCommand();
}
}
}
}
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
if (number) {
textfield.setText(digit);
number = false;
} else {
textfield.setText(textfield.getText() + digit);
}
}
}
public class CalculatorOp {
private int total;
public CalculatorOp() {
total = 0;
}
public String getTotalString() {
return ""+total;
}
public void setTotal(String n) {
total = convertToNumber(n);
}
public void add(String n) {
total += convertToNumber(n);
}
public void subtract(String n) {
total -= convertToNumber(n);
}
public void multiply(String n) {
total *= convertToNumber(n);
}
public void divide(String n) {
total /= convertToNumber(n);
}
public void mod(String n) {
total %= convertToNumber(n);
}

private int convertToNumber(String n) {


return Integer.parseInt(n);
}
}
}
class SwingCalculator {
public static void main(String[] args) {
JFrame frame = new Calculator();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//https://forgetcode.com/java/216-scientific-calculator

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy