1. addition, subtraction, multiplication, division
public class MyMath {public static final int DEFAULT_SCALE = 20;public static double add(double num1, double num2) {BigDecimal first = getBigDecimal(num1);BigDecimal second = getBigDecimal(num2);return first.add(second).doubleValue();}public static double substract(double num1, double num2) {BigDecimal first = getBigDecimal(num1);BigDecimal second = getBigDecimal(num2);return first.subtract(second).doubleValue();}public static double multiply(double num1, double num2) {BigDecimal first = getBigDecimal(num1);BigDecimal second = getBigDecimal(num2);return first.multiply(second).doubleValue();}public static double divide(double num1, double num2) {BigDecimal first = getBigDecimal(num1);BigDecimal second = getBigDecimal(num2);return first.divide(second,DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();}private static BigDecimal getBigDecimal(double num){return new BigDecimal(num);}}
2. UI
Public class calframe extends jframe {/*****/Private Static final long serialversionuid = 1l; private textfield; private final int frame_width = 360; private final int frame_height = 250; private calservice service = new calservice (); Private string [] mopstrings = {"MC", "Mr", "Ms", "m + "}; private string [] NOP = {"7", "8", "9", "/", "SQRT", "4", "5", "6 ", "*", "%", "1", "2", "3", "-", "1/X", "0 ", "+ /-",". "," + "," = "}; Private string [] ROP = {" back "," CE "," C "}; Public calframe () {super (); initialize ();} private void initialize () {This. settitle ("My calculator"); this. setresizable (false); jpanel = new jpanel (); textfield = new textfield ("0"); textfield. setbackground (color. white); textfield. seteditable (false); panel. setlayout (New borderlayout (10, 1); panel. add (textfield, borderlayout. north); panel. se Tpreferredsize (New dimensionuiresource (frame_width, frame_height); jpanel Panel1 = new jpanel (); panel1.setlayout (New gridlayout (5, 1, 0, 5 )); jbutton [] mbutton = new jbutton [mopstrings. length]; for (INT I = 0; I <this. mopstrings. length; I ++) {jbutton B = new jbutton (this. mopstrings [I]); B. addactionlistener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent arg0) {// todo auto-gen Erated method stubstring response string = arg0.getactioncommand (); string result = NULL; try {result = service. callmethod (struct string, textfield. gettext ();} catch (exception e) {// todo: handle exceptionsystem. out. println (E. getmessage ();} If (Response string. indexof ("MC") = 0) {} If (result! = NULL) {textfield. settext (result) ;}}); B. setbackground (color. red); mbutton [I] = B;} For (jbutton B: mbutton) {panel1.add (B );} //////////////////////////////////////// ////////////// panel panel2 = new Panel (); panel2.setlayout (New borderlayout (1, 5); Panel panel21 = new Panel (); jbutton [] result = new jbutton [ROP. length]; for (INT I = 0; I <this. drop. length; I ++) {jbutton B = new jbutton (this. ROP [I]); B. addactionlistener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent arg0) {// todo auto-generated method stub}); B. setbackground (color. red); Result [I] = B;} panel21.setlayout (New gridlayout (,); For (jbutton button: result) {panel21.add (button );} panel panel22 = new Panel (); panel22.setlayout (New gridlayout (4,5, 3,5); jbutton [] result2 = new jbutton [NOP. length]; string [] redbutton = {"/", "*", "-", "+", "="}; For (INT I = 0; I <this. nop. length; I ++) {jbutton b2 = new jbutton (this. NOP [I]); b2.addactionlistener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent arg0) {// todo auto-generated method stub}); arrays. sort (redbutton); If (arrays. binarysearch (redbutton, NOP [I])> = 0) {b2.setforeground (color. red);} else {b2.setforeground (color. blue) ;}result2 [I] = b2 ;}for (jbutton button: result2) {panel22.add (button) ;} panel2.add (panel21, borderlayout. north); panel2.add (panel22, borderlayout. center); panel. add (Panel1, borderlayout. west); panel. add (panel2, borderlayout. center); this. add (panel );}}
3. Business
public class CalService {private String firstNum = null;private String secondNum = null;private String numString = "0123456789.";private String opString = "+-*/";public String callMethod(String cmd, String text)throws Exception {if (cmd.equals("C")) {return clearAll();}else if (cmd.equals("CE")) {return clear(text);} else if (cmd.equals("Back")) {return backSpace(text);} else if (numString.indexOf(cmd) != -1) {return catNum(cmd, text);} else if (opString.indexOf(cmd) != -1) {return setOp(cmd, text);} else if (cmd.equals("=")) {return cal(text, false);} else if (cmd.equals("+/-")) {return setNegative(text);} else if (cmd.equals("1/x")) {return setReciprocal(text);} else if (cmd.equals("sqrt")) {return sqrt(text);} else if (cmd.equals("%")) {return cal(text, true);} else {return mCmd(cmd, text);}}private String backSpace(String text) {// TODO Auto-generated method stubreturn null;}private String catNum(String cmd, String text) {// TODO Auto-generated method stubreturn null;}private String setOp(String cmd, String text) {// TODO Auto-generated method stubreturn null;}private String setNegative(String text) {// TODO Auto-generated method stubreturn null;}private String setReciprocal(String text) {// TODO Auto-generated method stubreturn null;}private String sqrt(String text) {// TODO Auto-generated method stubreturn null;}private String cal(String text, boolean b) {// TODO Auto-generated method stubreturn null;}private String mCmd(String cmd, String text) {// TODO Auto-generated method stubreturn null;}private String clear(String text) {// TODO Auto-generated method stubreturn null;}private String clearAll() {// TODO Auto-generated method stubthis.firstNum = "0";this.secondNum = null;return this.firstNum;}}
4. Main class
public static void main(String[] args) {// TODO Auto-generated method stubCalFrame frame = new CalFrame();frame.pack();frame.setVisible(true);frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);}