FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

[Java] Calculator with GUI Pt. 5




Code:

        //For operations
        if(evt.getSource() == add) {
           answer1 = Double.parseDouble(query);
           query = "";
           display.setText("0");
           operation = 1;
        }
        else if(evt.getSource() == subtract) {
           answer1 = Double.parseDouble(query);
           query = "";
           display.setText("0");
           operation = 2;
        }
        else if(evt.getSource() == multiply) {
           answer1 = Double.parseDouble(query);
           query = "";
           display.setText("0");
           operation = 3;
        }
        else if(evt.getSource() == divide) {
           answer1 = Double.parseDouble(query);
           query = "";
           display.setText("0");
           operation = 4;
        }
        else if(evt.getSource() == modulus) {
           answer1 = Double.parseDouble(query);
           query = "";
           display.setText("0");
           operation = 5;
        }
        else if(evt.getSource() == sqrt) {
           answer1 = Math.sqrt(Double.parseDouble(query));
           display.setText(Double.toString(roundOff(answer1)));
           query = Double.toString(answer1);
        }
        else if(evt.getSource() == equals) {
           if(operation == 1) {
              answer1 += Double.parseDouble(query);
              display.setText(Double.toString(roundOff(answer1)));
              query = Double.toString(answer1);
           }
           else if(operation == 2) {
              answer1 -= Double.parseDouble(query);
              display.setText(Double.toString(roundOff(answer1)));
              query = Double.toString(answer1);
           }
           else if(operation == 3) {
              answer1 *= Double.parseDouble(query);
              display.setText(Double.toString(roundOff(answer1)));
              query = Double.toString(answer1);
           }
           else if(operation == 4) {
              answer1 /= Double.parseDouble(query);
              display.setText(Double.toString(roundOff(answer1)));
              query = Double.toString(answer1);
           }
           else if(operation == 5) {
              answer1 %= Double.parseDouble(query);
              display.setText(Double.toString(roundOff(answer1)));
              query = Double.toString(answer1);
           }
        }
        else if(evt.getSource() == reset) {
           query = "";
           display.setText("0");
        }
    }

    public double roundOff(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.####");
      return Double.valueOf(twoDForm.format(d));
   }
}




0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.