2 Replies Latest reply: Feb 28, 2013 8:42 AM by aksarben RSS

    removing decimal . simple calculator

    993972
      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      import java.applet.Applet;
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.text.DecimalFormat;
      import java.text.Format;
      import java.text.NumberFormat;
      /**
      *
      * @author nhiiemoOo011
      */
      public class Calculator extends Applet implements ActionListener {

      String fmt = "#.###";
      DecimalFormat deci = new DecimalFormat (fmt);
      String msg = " ", ts = " ", n1, n2;
      Frame f1 = new Frame();
      char op;
      double a, b, c, prev, memory;
      Button one, two, three, four, five, six, seven, eight, nine, ten, zero, dot;
      Button backspace, clear, eq, plus, minus, mul, div, sqrt, mc, mr, ms, mplus, mstatus;
      Panel p1 = new Panel();
      Panel p2 = new Panel();
      Panel p3 = new Panel();
      Panel p4 = new Panel();
      Panel p5 = new Panel();
      TextField t1;




      public void init() {

      }

      public Calculator() {
      f1.setTitle("San Juan's Calculator");
      setLayout(new BorderLayout());
      t1 = new TextField(30);
      t1.setColumns(40);
      t1.setEditable(false);
      t1.setForeground(Color.PINK);
      t1.setBackground(Color.DARK_GRAY);
      one = new Button("1");
      one.setBackground(Color.BLACK);
      one.setForeground(Color.PINK);
      two = new Button("2");
      two.setBackground(Color.BLACK);
      two.setForeground(Color.PINK);
      three = new Button("3");
      three.setBackground(Color.BLACK);
      three.setForeground(Color.PINK);
      four = new Button("4");
      four.setBackground(Color.BLACK);
      four.setForeground(Color.PINK);
      five = new Button("5");
      five.setBackground(Color.BLACK);
      five.setForeground(Color.PINK);
      six = new Button("6");
      six.setBackground(Color.BLACK);
      six.setForeground(Color.PINK);
      seven = new Button("7");
      seven.setBackground(Color.BLACK);
      seven.setForeground(Color.PINK);
      eight = new Button("8");
      eight.setBackground(Color.BLACK);
      eight.setForeground(Color.PINK);
      nine = new Button("9");
      nine.setBackground(Color.BLACK);
      nine.setForeground(Color.PINK);
      ten = new Button("10");
      ten.setBackground(Color.BLACK);
      ten.setForeground(Color.PINK);
      zero = new Button("0");
      zero.setBackground(Color.BLACK);
      zero.setForeground(Color.PINK);
      dot = new Button(".");
      dot.setBackground(Color.BLACK);
      dot.setForeground(Color.PINK);
      eq = new Button("=");
      eq.setBackground(Color.BLACK);
      eq.setForeground(Color.PINK);
      plus = new Button("+");
      plus.setBackground(Color.BLACK);
      plus.setForeground(Color.YELLOW);
      minus = new Button("-");
      minus.setBackground(Color.BLACK);
      minus.setForeground(Color.YELLOW);
      div = new Button(" / ");
      div.setBackground(Color.BLACK);
      div.setForeground(Color.YELLOW);
      mul = new Button("*");
      mul.setBackground(Color.BLACK);
      mul.setForeground(Color.YELLOW);
      sqrt = new Button("sqrt");
      sqrt.setBackground(Color.BLACK);
      sqrt.setForeground(Color.ORANGE);
      clear = new Button("C");
      clear.setBackground(Color.BLACK);
      clear.setForeground(Color.ORANGE);
      backspace = new Button("B");
      backspace.setBackground(Color.BLACK);
      backspace.setForeground(Color.ORANGE);
      mc = new Button("MC");
      mc.setBackground(Color.BLACK);
      mc.setForeground(Color.ORANGE);
      mr = new Button("MR");
      mr.setBackground(Color.BLACK);
      mr.setForeground(Color.ORANGE);
      ms = new Button("MS");
      ms.setBackground(Color.BLACK);
      ms.setForeground(Color.ORANGE);
      mplus = new Button("M+");
      mplus.setBackground(Color.BLACK);
      mplus.setForeground(Color.ORANGE);
      mstatus = new Button();
      mstatus.setVisible(false);


      f1.add(p1);
      f1.add(p2);
      f1.add(p3);
      f1.add(p4);
      f1.add(p5);

      p1.add(t1);
      add(p1, BorderLayout.NORTH);

      p4.setLayout(new GridLayout(1, 3, 2, 3));
      p4.add(sqrt);
      p4.add(backspace);
      p4.add(clear);
      add(p4, BorderLayout.SOUTH);


      p3.setLayout(new GridLayout(5, 2, 5, 5));
      p3.add(mc);
      p3.add(mr);
      p3.add(ms);
      p3.add(mplus);
      p3.add(mstatus);
      add(p3, BorderLayout.WEST);

      p2.setLayout(new GridLayout(4, 4, 3, 3));
      p2.add(seven);
      p2.add(eight);
      p2.add(nine);
      p2.add(four);
      p2.add(five);
      p2.add(six);
      p2.add(one);
      p2.add(two);
      p2.add(three);
      p2.add(zero);
      p2.add(dot);
      p2.add(eq);
      add(p2, BorderLayout.CENTER);

      p5.setLayout(new GridLayout(4, 5, 2, 2));
      p5.add(plus);
      p5.add(minus);
      p5.add(mul);
      p5.add(div);
      add(p5, BorderLayout.EAST);



      t1.setEditable(false);
      p1.setBackground(Color.BLACK);
      p2.setBackground(Color.BLACK);
      p3.setBackground(Color.BLACK);
      p4.setBackground(Color.BLACK);
      p5.setBackground(Color.BLACK);

      t1.addActionListener(this);
      one.addActionListener(this);
      two.addActionListener(this);
      three.addActionListener(this);
      four.addActionListener(this);
      five.addActionListener(this);
      six.addActionListener(this);
      seven.addActionListener(this);
      eight.addActionListener(this);
      nine.addActionListener(this);
      zero.addActionListener(this);
      dot.addActionListener(this);
      eq.addActionListener(this);
      plus.addActionListener(this);
      minus.addActionListener(this);
      clear.addActionListener(this);
      backspace.addActionListener(this);
      div.addActionListener(this);
      mul.addActionListener(this);
      sqrt.addActionListener(this);
      mc.addActionListener(this);
      mr.addActionListener(this);
      ms.addActionListener(this);
      mplus.addActionListener(this);

      setVisible(true);
      setSize(500,500);
      f1.setResizable(false);

      }

      public void actionPerformed(ActionEvent ae) {

      String str = ae.getActionCommand();

      if (str.equals("1")) {
      ts = ts + "1";
      t1.setText(ts);
      } else if (str.equals("2")) {
      ts = ts + "2";
      t1.setText(ts);
      } else if (str.equals("3")) {
      ts = ts + "3";
      t1.setText(ts);
      } else if (str.equals("4")) {
      ts = ts + "4";
      t1.setText(ts);
      } else if (str.equals("5")) {
      ts = ts + "5";
      t1.setText(ts);
      } else if (str.equals("6")) {
      ts = ts + "6";
      t1.setText(ts);
      } else if (str.equals("7")) {
      ts = ts + "7";
      t1.setText(ts);
      } else if (str.equals("8")) {
      ts = ts + "8";
      t1.setText(ts);
      } else if (str.equals("9")) {
      ts = ts + "9";
      t1.setText(ts);
      } else if (str.equals("0")) {
      ts = ts + "0";
      t1.setText(ts);
      } else if (str.equals(".")) {
      ts = ts + ".";
      t1.setText(ts);
      } else if (str.equals("+")) {
      n1 = t1.getText();
      ts = " ";
      a = Double.parseDouble(t1.getText().trim());
      op = '+';
      t1.setText(ts);
      } else if (str.equals("-")) {
      n1 = t1.getText();
      ts = " ";
      t1.setText(ts);
      a = Double.parseDouble(n1.trim());
      op = '-';
      } else if (str.equals(" / ")) {
      n1 = t1.getText();
      ts = " ";
      t1.setText(ts);
      a = Double.parseDouble(n1.trim());
      op = '/';
      } else if (str.equals("*")) {
      n1 = t1.getText();
      ts = " ";
      t1.setText(ts);
      a = Double.parseDouble(n1.trim());
      op = '*';
      } else if (str.equals("C")) {
      ts = " ";
      t1.setText(ts);
      n1 = " ";
      n2 = " ";
      a = 0;
      b = 0;
      c = 0;
      op = 'C';
      } else if (str.equals("B")) {
      msg = t1.getText();
      ts = msg.substring(0, msg.length() - 1);
      t1.setText(ts);
      op = 'B';
      } else if (str.equals("sqrt")) {
      n1 = t1.getText();
      a = Double.parseDouble(n1.trim());
      a = Math.sqrt(a);
      ts = " " + a;
      t1.setText(ts);
      //op='*';
      } else if (str.equals("MC")) {
      memory = 0;
      mstatus.setLabel(" ");
      } else if (str.equals("MS")) {
      n1 = t1.getText();
      if (n1 != null) {
      a = Double.parseDouble(n1.trim());
      memory = a;
      mstatus.setLabel("M");
      }
      } else if (str.equals("MR")) {
      ts = " " + memory;
      t1.setText(ts);
      } else if (str.equals("M+")) {
      n1 = t1.getText();
      if (n1 != " ") {
      a = Double.parseDouble(n1.trim());
      memory = memory + a;
      }
      } else if (str.equals("=")) {
      String n3 = " ";
      n2 = t1.getText();
      b = Double.parseDouble(n2.trim());

      String result = deci.format(c);
      t1.setText(result);
      switch (op) {
      case '+':
      c = a + b;
      break;
      case '-':
      c = a - b;
      break;
      case '/':
      c = a / b;
      break;
      case '*':
      c = a * b;
      break;
      default:
      c = b;

      }

      n3 = n3 + c;
      op = 'C';
      t1.setText(n3);
      }


      }}



      ---- here is a code i copied from a friend. i was hoping if someone can help me remove the decimal in the final answer it must be a simple calculator and when you add 1 +_ 1 answer will be 2.0 i want to remove the .0 there so it will became a whole number. please help me. this is a project. :( i tried my luck in other sites but they wont answer me back.