Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.62 KB | None | 0 0
  1. //Calculator.java
  2. package calculator;
  3. import javax.swing.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.*;
  7.  
  8.  
  9.  
  10.  
  11. public class Calculator extends JFrame implements ActionListener{
  12.     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;
  13.     JTextField txt = new JTextField(15);
  14.     Inner logic = new Inner();
  15.     JPanel panel = new JPanel();
  16.     String op = "";
  17.     double total1;
  18.     String fnum, snum;
  19.     public Calculator(String title){
  20.         super(title);
  21.         this.init();
  22.         this.setSize(225, 300);
  23.         this.setVisible(true);
  24.          
  25.     }
  26.     void init()
  27.     {
  28.         JButton ln = new JButton("ln");
  29.         JButton sqrt = new JButton("\u221A");
  30.         JButton rec = new JButton("1/x");
  31.         JButton btn0 = new JButton("0");
  32.         JButton btn1 = new JButton("1");
  33.         JButton btn2 = new JButton("2");
  34.         JButton btn3 = new JButton("3");
  35.         JButton btn4 = new JButton("4");
  36.         JButton btn5 = new JButton("5");
  37.         JButton btn6 = new JButton("6");
  38.         JButton btn7 = new JButton("7");
  39.         JButton btn8 = new JButton("8");
  40.         JButton btn9 = new JButton("9");
  41.         JButton btnC = new JButton("C");
  42.         JButton btnP = new JButton(".");
  43.         JButton plus = new JButton("+");
  44.         JButton multiplication = new JButton("*");
  45.         JButton divide = new JButton("/");
  46.         JButton minus = new JButton("-");
  47.         JButton equal = new JButton("=");
  48.         JButton sine = new JButton("sin");
  49.         JButton cosine = new JButton("cos");
  50.         JButton tan  = new JButton("tan");
  51.         JButton csc  = new JButton("csc");
  52.         JButton sec  = new JButton("sec");
  53.         JButton cot = new JButton("cot");
  54.         ln.addActionListener(this);
  55.         rec.addActionListener(this);
  56.         btn0.addActionListener(this);
  57.         btn1.addActionListener(this);
  58.         btn2.addActionListener(this);
  59.         btn3.addActionListener(this);
  60.         btn4.addActionListener(this);
  61.         btn5.addActionListener(this);
  62.         btn6.addActionListener(this);
  63.         btn7.addActionListener(this);
  64.         btn8.addActionListener(this);
  65.         btn9.addActionListener(this);
  66.         btnC.addActionListener(this);
  67.         btnP.addActionListener(this);
  68.         plus.addActionListener(this);
  69.         minus.addActionListener(this);
  70.         equal.addActionListener(this);
  71.         sine.addActionListener(this);
  72.         cosine.addActionListener(this);
  73.         tan.addActionListener(this);
  74.         csc.addActionListener(this);
  75.         sec.addActionListener(this);
  76.         cot.addActionListener(this);
  77.        
  78.         txt.setBounds(35,15,360,50);
  79.         btn7.setBounds(35,85,50,50);
  80.         btn8.setBounds(95,85,50,50);
  81.         btn9.setBounds(155,85,50,50);
  82.         btn4.setBounds(35,145,50,50);
  83.         btn5.setBounds(95,145,50,50);
  84.         btn6.setBounds(155,145,50,50);
  85.         btn1.setBounds(35,205,50,50);
  86.         btn2.setBounds(95,205,50,50);
  87.         btn3.setBounds(155,205,50,50);
  88.         btnC.setBounds(340,85,50,50);
  89.         btnP.setBounds(95,265,50,50);
  90.         plus.setBounds(280,145,50,50);
  91.         multiplication.setBounds(340,145,50,50);
  92.         minus.setBounds(280,205,50,50);
  93.         divide.setBounds(340,205,50,50);
  94.         sine.setBounds(280, 265, 110, 50);
  95.         cosine.setBounds(340, 265, 50, 50);
  96.         tan.setBounds(280,315, 170, 50);
  97.         csc.setBounds(340, 325, 50, 50);
  98.         sec.setBounds(280, 375,230, 50);
  99.         cot.setBounds(340, 385, 50, 50);
  100.         equal.setBounds(280,315,170,50);
  101.         panel.add(txt);
  102.         panel.add(btn1);
  103.         panel.add(btn2);
  104.         panel.add(btn3);
  105.         panel.add(plus);
  106.         panel.add(btn4);
  107.         panel.add(btn5);
  108.         panel.add(btn6);
  109.         panel.add(minus);
  110.         panel.add(btn7);
  111.         panel.add(btn8);
  112.         panel.add(btn9);
  113.         panel.add(multiplication);
  114.         panel.add(btnC);
  115.         panel.add(btn0);
  116.         panel.add(btnP);
  117.         panel.add(divide);
  118.         panel.add(rec);
  119.         panel.add(sqrt);
  120.         panel.add(ln);
  121.         panel.add(equal);
  122.         panel.add(sine);
  123.         panel.add(cosine);
  124.         panel.add(tan);
  125.         panel.add(csc);
  126.         panel.add(sec);
  127.         panel.add(cot);
  128.         this.add(panel);
  129.         setVisible(true);
  130.     }
  131.     public void actionPerformed(ActionEvent ea)
  132.     {
  133.             String e = ea.getActionCommand();
  134.             String input = txt.getText();
  135.             if(e.equals("C"))
  136.             {
  137.                 txt.setText("");
  138.                 //logic.setTotal("");
  139.             }
  140.             else if(e.equals("1"))
  141.             {
  142.                 txt.setText(input + "1");
  143.             }
  144.             if(e.equals("2"))
  145.             {
  146.                 txt.setText(input + "2");
  147.             }
  148.             if(e.equals("3"))
  149.             {
  150.                 txt.setText(input + "3");
  151.             }
  152.             if(e.equals("4"))
  153.             {
  154.                 txt.setText(input + "4");
  155.             }
  156.             if(e.equals("5"))
  157.             {
  158.                 txt.setText(input + "5");
  159.             }
  160.             if(e.equals("6"))
  161.             {
  162.                 txt.setText(input + "6");
  163.             }
  164.             if(e.equals("7"))
  165.             {
  166.                 txt.setText(input + "7");
  167.             }
  168.             if(e.equals("8"))
  169.             {
  170.                 txt.setText(input + "8");
  171.             }
  172.             if(e.equals("9"))
  173.             {
  174.                 txt.setText(input + "9");
  175.             }
  176.             if(e.equals("."))
  177.             {
  178.                 txt.setText(input + ".");
  179.             }
  180.            
  181.             if(e.equals("+"))
  182.             {
  183.                 fnum = txt.getText();
  184.                 op = "+";
  185.                 txt.setText("");
  186.             }
  187.             if(e.equals("-"))
  188.             {
  189.                 fnum = txt.getText();
  190.                 op = "-";
  191.                 txt.setText("");
  192.             }
  193.             if(e.equals("*"))
  194.             {
  195.                 fnum = txt.getText();
  196.                 logic.setTotal(fnum);
  197.                 op = "*";
  198.                 txt.setText("");
  199.                 JOptionPane.showMessageDialog(null, fnum);
  200.             }
  201.             if(e.equals("/"))
  202.             {
  203.                 fnum = txt.getText();
  204.                 op = "/";
  205.                 txt.setText("");
  206.             }
  207.             if(e.equals("sin"))
  208.             {
  209.                 fnum = txt.getText();
  210.                 op = "sin";
  211.             }
  212.             if(e.equals("cos"))
  213.             {
  214.                 fnum = txt.getText();
  215.                 op = "cos";
  216.             }
  217.             if(e.equals("tan"))
  218.             {
  219.                 fnum = txt.getText();
  220.                 op = "tan";
  221.             }
  222.             if(e.equals("csc"))
  223.             {
  224.                 fnum = txt.getText();
  225.                 op = "csc";
  226.             }
  227.             if(e.equals("sec"))
  228.             {
  229.                 fnum = txt.getText();
  230.                 op = "sec";
  231.             }
  232.             if(e.equals("cot"))
  233.             {
  234.                 fnum = txt.getText();
  235.                 op = "cot";
  236.             }
  237.             if(e.equals("ln"))
  238.             {
  239.                 fnum = txt.getText();
  240.                 op = "ln";
  241.             }
  242.             if(e.equals("1/x"))
  243.             {
  244.                 fnum = txt.getText();
  245.                 op = "1/x";
  246.             }
  247.             if(e.equals("\u221A"))
  248.             {
  249.                 fnum = txt.getText();
  250.                 op = "sqrt";
  251.             }
  252.             else if(e.equals("="))
  253.             {
  254.                   snum = txt.getText();
  255.                   if(op.equals("+"))
  256.                   {
  257.                       logic.setTotal(fnum);
  258.                       logic.add(snum);
  259.                       total1 = logic.total;
  260.                   }
  261.                   else if(op.equals("*"))
  262.                   {
  263.                        logic.setTotal(fnum);
  264.                        logic.multiplication(snum);
  265.                        total1 = logic.total;
  266.                   }
  267.                   else if(op.equals("/")){
  268.                       logic.setTotal(fnum);
  269.                       logic.divide(snum);
  270.                       total1 = logic.total;
  271.                   }
  272.                   else if(op.equals("-"))
  273.                   {  
  274.                       logic.setTotal(fnum);
  275.                       logic.substract(snum);
  276.                       total1 = logic.total;
  277.                   }
  278.                   else if(op.equals("sin"))
  279.                   {
  280.                        logic.total = logic.convertToNumber(fnum);
  281.                        logic.sine();
  282.                        total1 = logic.convertToNumber(logic.getTotalString());
  283.                   }
  284.                   else if(op.equals("cos"))
  285.                   {
  286.                       logic.total = logic.convertToNumber(fnum);
  287.                       logic.cosine();
  288.                       total1 = logic.convertToNumber(logic.getTotalString());
  289.                   }
  290.                   else if(op.equals("tan"))
  291.                   {
  292.                       logic.total = logic.convertToNumber(fnum);
  293.                       logic.tangent();
  294.                       total1 = logic.convertToNumber(logic.getTotalString());
  295.                   }
  296.                   else if(op.equals("csc"))
  297.                   {
  298.                       logic.total = logic.convertToNumber(fnum);
  299.                       logic.csc();
  300.                       total1 = logic.convertToNumber(logic.getTotalString());
  301.                   }
  302.                   else if(op.equals("sec"))
  303.                   {
  304.                       logic.total = logic.convertToNumber(fnum);
  305.                       logic.sec();
  306.                       total1 = logic.convertToNumber(logic.getTotalString());
  307.                   }
  308.                   else if(op.equals("cot"))
  309.                   {
  310.                       logic.total = logic.convertToNumber(fnum);
  311.                       logic.cot();
  312.                       total1 = logic.convertToNumber(logic.getTotalString());
  313.                   }
  314.                   else if(op.equals("sqrt"))
  315.                   {
  316.                       logic.total = logic.convertToNumber(fnum);
  317.                       logic.sqrt();
  318.                       total1 = logic.convertToNumber(logic.getTotalString());
  319.                   }
  320.                   else if(op.equals("ln"))
  321.                   {
  322.                       logic.total = logic.convertToNumber(fnum);
  323.                       logic.ln();
  324.                       total1 = logic.convertToNumber(logic.getTotalString());
  325.                   }
  326.                   else if(op.equals("1/x"))
  327.                   {
  328.                       logic.total = logic.convertToNumber(fnum);
  329.                       logic.recipocal();
  330.                       total1 = logic.convertToNumber(logic.getTotalString());
  331.                   }
  332.                   txt.setText(""+total1);
  333.             }
  334.         }
  335. }
  336.  
  337. //Inner.java
  338.  
  339.  
  340. package calculator;
  341.  
  342. public class Inner extends Calculators{
  343.     public double total;
  344.     public Inner()
  345.     {
  346.         total = 0;
  347.     }
  348.     public String getTotalString()
  349.     {
  350.         return "" + total;
  351.     }
  352.     public void setTotal(String n)
  353.     {
  354.         total = convertToNumber(n);
  355.     }
  356.     public void add(String n)
  357.     {
  358.         total += convertToNumber(n);
  359.     }
  360.     public void substract(String n)
  361.     {
  362.         total -= convertToNumber(n);
  363.     }
  364.     public void divide(String n)
  365.     {
  366.         total /= convertToNumber(n);
  367.     }
  368.     public void multiplication(String n)
  369.     {
  370.         total *=convertToNumber(n);
  371.     }
  372.     public void sqrt()
  373.     {
  374.         total = Math.sqrt(total);
  375.     }
  376.     public double convertToNumber(String n)
  377.     {
  378.         return Double.parseDouble(n);
  379.     }
  380.     public void sine()
  381.     {
  382.         total = super.sine(total);
  383.     }
  384.     public void cosine()
  385.     {
  386.         total = super.cosine(total);
  387.     }
  388.     public void tangent()
  389.     {
  390.         total =  super.tangent(total);
  391.     }
  392.     public void csc()
  393.     {
  394.         total = super.csc(total);
  395.     }
  396.     public void sec()
  397.     {
  398.         total = super.sec(total);
  399.     }
  400.     public void cot()
  401.     {
  402.         total = super.cot(total);
  403.     }
  404.     public void ln()
  405.     {
  406.         total = super.logarithm(total);
  407.     }
  408.     public void recipocal()
  409.     {
  410.         total = super.rec(total);
  411.     }
  412. }
  413.  
  414.  
  415. //Calculators.java
  416.  
  417.  
  418. package calculator;
  419. import java.lang.Math;
  420. public class Calculators {
  421.     public double sine(double s)
  422.     {
  423.         return Math.sin(s);
  424.     }
  425.     public double cosine(double s)
  426.     {
  427.         return Math.cos(s);
  428.     }
  429.     public double tangent(double s)
  430.     {
  431.         return Math.tan(s);
  432.     }
  433.     public double csc (double s )
  434.     {
  435.         return 1.0 / Math.sin(s);
  436.     }
  437.     public double sec ( double s )
  438.     {
  439.         return 1.0 / Math.cos(s);
  440.     }
  441.     public double cot ( double s )
  442.     {
  443.         return 1.0 / Math.tan(s);
  444.     }
  445.     public double logarithm(double s)
  446.     {
  447.         return Math.log(s);
  448.     }
  449.     public double rec(double s)
  450.     {
  451.         return 1/s;
  452.     }
  453. }
  454.  
  455.  
  456. //main.java
  457.  
  458. package calculator;
  459.  
  460. import javax.swing.*;
  461. public class Main {
  462.  
  463.     public static void main(String[] args) {
  464.         // TODO Auto-generated method stub
  465.         Calculator window = new Calculator("Calculator");
  466.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  467.         window.setVisible(true);
  468.     }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement