Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Calculator.java
- package calculator;
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.*;
- public class Calculator extends JFrame implements ActionListener{
- JButton enter, clear, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnC, plus, minus, divide, multiplication, sine, cosine, tan, csc, sec, cot, equal, btnP;
- JTextField txt = new JTextField(15);
- Inner logic = new Inner();
- JPanel panel = new JPanel();
- String op = "";
- double total1;
- String fnum, snum;
- public Calculator(String title){
- super(title);
- this.init();
- this.setSize(225, 300);
- this.setVisible(true);
- }
- void init()
- {
- JButton ln = new JButton("ln");
- JButton sqrt = new JButton("\u221A");
- JButton rec = new JButton("1/x");
- JButton btn0 = new JButton("0");
- JButton btn1 = new JButton("1");
- JButton btn2 = new JButton("2");
- JButton btn3 = new JButton("3");
- JButton btn4 = new JButton("4");
- JButton btn5 = new JButton("5");
- JButton btn6 = new JButton("6");
- JButton btn7 = new JButton("7");
- JButton btn8 = new JButton("8");
- JButton btn9 = new JButton("9");
- JButton btnC = new JButton("C");
- JButton btnP = new JButton(".");
- JButton plus = new JButton("+");
- JButton multiplication = new JButton("*");
- JButton divide = new JButton("/");
- JButton minus = new JButton("-");
- JButton equal = new JButton("=");
- JButton sine = new JButton("sin");
- JButton cosine = new JButton("cos");
- JButton tan = new JButton("tan");
- JButton csc = new JButton("csc");
- JButton sec = new JButton("sec");
- JButton cot = new JButton("cot");
- ln.addActionListener(this);
- rec.addActionListener(this);
- btn0.addActionListener(this);
- btn1.addActionListener(this);
- btn2.addActionListener(this);
- btn3.addActionListener(this);
- btn4.addActionListener(this);
- btn5.addActionListener(this);
- btn6.addActionListener(this);
- btn7.addActionListener(this);
- btn8.addActionListener(this);
- btn9.addActionListener(this);
- btnC.addActionListener(this);
- btnP.addActionListener(this);
- plus.addActionListener(this);
- minus.addActionListener(this);
- equal.addActionListener(this);
- sine.addActionListener(this);
- cosine.addActionListener(this);
- tan.addActionListener(this);
- csc.addActionListener(this);
- sec.addActionListener(this);
- cot.addActionListener(this);
- txt.setBounds(35,15,360,50);
- btn7.setBounds(35,85,50,50);
- btn8.setBounds(95,85,50,50);
- btn9.setBounds(155,85,50,50);
- btn4.setBounds(35,145,50,50);
- btn5.setBounds(95,145,50,50);
- btn6.setBounds(155,145,50,50);
- btn1.setBounds(35,205,50,50);
- btn2.setBounds(95,205,50,50);
- btn3.setBounds(155,205,50,50);
- btnC.setBounds(340,85,50,50);
- btnP.setBounds(95,265,50,50);
- plus.setBounds(280,145,50,50);
- multiplication.setBounds(340,145,50,50);
- minus.setBounds(280,205,50,50);
- divide.setBounds(340,205,50,50);
- sine.setBounds(280, 265, 110, 50);
- cosine.setBounds(340, 265, 50, 50);
- tan.setBounds(280,315, 170, 50);
- csc.setBounds(340, 325, 50, 50);
- sec.setBounds(280, 375,230, 50);
- cot.setBounds(340, 385, 50, 50);
- equal.setBounds(280,315,170,50);
- panel.add(txt);
- panel.add(btn1);
- panel.add(btn2);
- panel.add(btn3);
- panel.add(plus);
- panel.add(btn4);
- panel.add(btn5);
- panel.add(btn6);
- panel.add(minus);
- panel.add(btn7);
- panel.add(btn8);
- panel.add(btn9);
- panel.add(multiplication);
- panel.add(btnC);
- panel.add(btn0);
- panel.add(btnP);
- panel.add(divide);
- panel.add(rec);
- panel.add(sqrt);
- panel.add(ln);
- panel.add(equal);
- panel.add(sine);
- panel.add(cosine);
- panel.add(tan);
- panel.add(csc);
- panel.add(sec);
- panel.add(cot);
- this.add(panel);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent ea)
- {
- String e = ea.getActionCommand();
- String input = txt.getText();
- if(e.equals("C"))
- {
- txt.setText("");
- //logic.setTotal("");
- }
- else if(e.equals("1"))
- {
- txt.setText(input + "1");
- }
- if(e.equals("2"))
- {
- txt.setText(input + "2");
- }
- if(e.equals("3"))
- {
- txt.setText(input + "3");
- }
- if(e.equals("4"))
- {
- txt.setText(input + "4");
- }
- if(e.equals("5"))
- {
- txt.setText(input + "5");
- }
- if(e.equals("6"))
- {
- txt.setText(input + "6");
- }
- if(e.equals("7"))
- {
- txt.setText(input + "7");
- }
- if(e.equals("8"))
- {
- txt.setText(input + "8");
- }
- if(e.equals("9"))
- {
- txt.setText(input + "9");
- }
- if(e.equals("."))
- {
- txt.setText(input + ".");
- }
- if(e.equals("+"))
- {
- fnum = txt.getText();
- op = "+";
- txt.setText("");
- }
- if(e.equals("-"))
- {
- fnum = txt.getText();
- op = "-";
- txt.setText("");
- }
- if(e.equals("*"))
- {
- fnum = txt.getText();
- logic.setTotal(fnum);
- op = "*";
- txt.setText("");
- JOptionPane.showMessageDialog(null, fnum);
- }
- if(e.equals("/"))
- {
- fnum = txt.getText();
- op = "/";
- txt.setText("");
- }
- if(e.equals("sin"))
- {
- fnum = txt.getText();
- op = "sin";
- }
- if(e.equals("cos"))
- {
- fnum = txt.getText();
- op = "cos";
- }
- if(e.equals("tan"))
- {
- fnum = txt.getText();
- op = "tan";
- }
- if(e.equals("csc"))
- {
- fnum = txt.getText();
- op = "csc";
- }
- if(e.equals("sec"))
- {
- fnum = txt.getText();
- op = "sec";
- }
- if(e.equals("cot"))
- {
- fnum = txt.getText();
- op = "cot";
- }
- if(e.equals("ln"))
- {
- fnum = txt.getText();
- op = "ln";
- }
- if(e.equals("1/x"))
- {
- fnum = txt.getText();
- op = "1/x";
- }
- if(e.equals("\u221A"))
- {
- fnum = txt.getText();
- op = "sqrt";
- }
- else if(e.equals("="))
- {
- snum = txt.getText();
- if(op.equals("+"))
- {
- logic.setTotal(fnum);
- logic.add(snum);
- total1 = logic.total;
- }
- else if(op.equals("*"))
- {
- logic.setTotal(fnum);
- logic.multiplication(snum);
- total1 = logic.total;
- }
- else if(op.equals("/")){
- logic.setTotal(fnum);
- logic.divide(snum);
- total1 = logic.total;
- }
- else if(op.equals("-"))
- {
- logic.setTotal(fnum);
- logic.substract(snum);
- total1 = logic.total;
- }
- else if(op.equals("sin"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.sine();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("cos"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.cosine();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("tan"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.tangent();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("csc"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.csc();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("sec"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.sec();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("cot"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.cot();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("sqrt"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.sqrt();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("ln"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.ln();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- else if(op.equals("1/x"))
- {
- logic.total = logic.convertToNumber(fnum);
- logic.recipocal();
- total1 = logic.convertToNumber(logic.getTotalString());
- }
- txt.setText(""+total1);
- }
- }
- }
- //Inner.java
- package calculator;
- public class Inner extends Calculators{
- public double total;
- public Inner()
- {
- 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 substract(String n)
- {
- total -= convertToNumber(n);
- }
- public void divide(String n)
- {
- total /= convertToNumber(n);
- }
- public void multiplication(String n)
- {
- total *=convertToNumber(n);
- }
- public void sqrt()
- {
- total = Math.sqrt(total);
- }
- public double convertToNumber(String n)
- {
- return Double.parseDouble(n);
- }
- public void sine()
- {
- total = super.sine(total);
- }
- public void cosine()
- {
- total = super.cosine(total);
- }
- public void tangent()
- {
- total = super.tangent(total);
- }
- public void csc()
- {
- total = super.csc(total);
- }
- public void sec()
- {
- total = super.sec(total);
- }
- public void cot()
- {
- total = super.cot(total);
- }
- public void ln()
- {
- total = super.logarithm(total);
- }
- public void recipocal()
- {
- total = super.rec(total);
- }
- }
- //Calculators.java
- package calculator;
- import java.lang.Math;
- public class Calculators {
- public double sine(double s)
- {
- return Math.sin(s);
- }
- public double cosine(double s)
- {
- return Math.cos(s);
- }
- public double tangent(double s)
- {
- return Math.tan(s);
- }
- public double csc (double s )
- {
- return 1.0 / Math.sin(s);
- }
- public double sec ( double s )
- {
- return 1.0 / Math.cos(s);
- }
- public double cot ( double s )
- {
- return 1.0 / Math.tan(s);
- }
- public double logarithm(double s)
- {
- return Math.log(s);
- }
- public double rec(double s)
- {
- return 1/s;
- }
- }
- //main.java
- package calculator;
- import javax.swing.*;
- public class Main {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Calculator window = new Calculator("Calculator");
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement