Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.79 KB | None | 0 0
  1. /*********************************************
  2. Save this file as MyCalculator.java
  3. to compile it use
  4.     javac MyCalculator.java
  5. to use the calcuator do this
  6.     java MyCalculator
  7.  
  8. **********************************************/  
  9. import java.awt.*;  
  10. import java.awt.event.*;  
  11. /*********************************************/  
  12.  
  13. public class MyCalculator extends Frame  
  14. {  
  15.  
  16. public boolean setClear=true;  
  17. double number, memValue;  
  18. char op;  
  19.  
  20. String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };  
  21. String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };  
  22. String memoryButtonText[] = {"MC", "MR", "MS", "M+" };  
  23. String specialButtonText[] = {"Backspc", "C", "CE" };  
  24.  
  25. MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];  
  26. MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];  
  27. MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];  
  28. MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];  
  29.  
  30. Label displayLabel=new Label("0",Label.RIGHT);  
  31. Label memLabel=new Label(" ",Label.RIGHT);  
  32.  
  33. final int FRAME_WIDTH=325,FRAME_HEIGHT=325;  
  34. final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;  
  35. final int TOPX=30, TOPY=50;  
  36. ///////////////////////////  
  37. MyCalculator(String frameText)//constructor  
  38. {  
  39. super(frameText);  
  40.  
  41. int tempX=TOPX, y=TOPY;  
  42. displayLabel.setBounds(tempX,y,240,HEIGHT);  
  43. displayLabel.setBackground(Color.BLUE);  
  44. displayLabel.setForeground(Color.WHITE);  
  45. add(displayLabel);  
  46.  
  47. memLabel.setBounds(TOPX,  TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);  
  48. add(memLabel);  
  49.  
  50. // set Co-ordinates for Memory Buttons  
  51. tempX=TOPX;  
  52. y=TOPY+2*(HEIGHT+V_SPACE);  
  53. for(int i=0; i<memoryButton.length; i++)  
  54. {  
  55. memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);  
  56. memoryButton[i].setForeground(Color.RED);  
  57. y+=HEIGHT+V_SPACE;  
  58. }  
  59.  
  60. //set Co-ordinates for Special Buttons  
  61. tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);  
  62. for(int i=0;i<specialButton.length;i++)  
  63. {  
  64. specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);  
  65. specialButton[i].setForeground(Color.RED);  
  66. tempX=tempX+2*WIDTH+H_SPACE;  
  67. }  
  68.  
  69. //set Co-ordinates for Digit Buttons  
  70. int digitX=TOPX+WIDTH+H_SPACE;  
  71. int digitY=TOPY+2*(HEIGHT+V_SPACE);  
  72. tempX=digitX;  y=digitY;  
  73. for(int i=0;i<digitButton.length;i++)  
  74. {  
  75. digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);  
  76. digitButton[i].setForeground(Color.BLUE);  
  77. tempX+=WIDTH+H_SPACE;  
  78. if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}  
  79. }  
  80.  
  81. //set Co-ordinates for Operator Buttons  
  82. int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;  
  83. int opsY=digitY;  
  84. tempX=opsX;  y=opsY;  
  85. for(int i=0;i<operatorButton.length;i++)  
  86. {  
  87. tempX+=WIDTH+H_SPACE;  
  88. operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);  
  89. operatorButton[i].setForeground(Color.RED);  
  90. if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}  
  91. }  
  92.  
  93. addWindowListener(new WindowAdapter()  
  94. {  
  95. public void windowClosing(WindowEvent ev)  
  96. {System.exit(0);}  
  97. });  
  98.  
  99. setLayout(null);  
  100. setSize(FRAME_WIDTH,FRAME_HEIGHT);  
  101. setVisible(true);  
  102. }  
  103. //////////////////////////////////  
  104. static String getFormattedText(double temp)  
  105. {  
  106. String resText=""+temp;  
  107. if(resText.lastIndexOf(".0")>0)  
  108.     resText=resText.substring(0,resText.length()-2);  
  109. return resText;  
  110. }  
  111. ////////////////////////////////////////  
  112. public static void main(String []args)  
  113. {  
  114. new MyCalculator("Calculator - JavaTpoint");  
  115. }  
  116. }  
  117.  
  118. /*******************************************/  
  119.  
  120. class MyDigitButton extends Button implements ActionListener  
  121. {  
  122. MyCalculator cl;  
  123.  
  124. //////////////////////////////////////////  
  125. MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
  126. {  
  127. super(cap);  
  128. setBounds(x,y,width,height);  
  129. this.cl=clc;  
  130. this.cl.add(this);  
  131. addActionListener(this);  
  132. }  
  133. ////////////////////////////////////////////////  
  134. static boolean isInString(String s, char ch)  
  135. {  
  136. for(int i=0; i<s.length();i++) if(s.charAt(i)==ch) return true;  
  137. return false;  
  138. }  
  139. /////////////////////////////////////////////////  
  140. public void actionPerformed(ActionEvent ev)  
  141. {  
  142. String tempText=((MyDigitButton)ev.getSource()).getLabel();  
  143.  
  144. if(tempText.equals("."))  
  145. {  
  146.  if(cl.setClear)  
  147.     {cl.displayLabel.setText("0.");cl.setClear=false;}  
  148.  else if(!isInString(cl.displayLabel.getText(),'.'))  
  149.     cl.displayLabel.setText(cl.displayLabel.getText()+".");  
  150.  return;  
  151. }  
  152.  
  153. int index=0;  
  154. try{  
  155.         index=Integer.parseInt(tempText);  
  156.     }catch(NumberFormatException e){return;}  
  157.  
  158. if (index==0 && cl.displayLabel.getText().equals("0")) return;  
  159.  
  160. if(cl.setClear)  
  161.             {cl.displayLabel.setText(""+index);cl.setClear=false;}  
  162. else  
  163.     cl.displayLabel.setText(cl.displayLabel.getText()+index);  
  164. }//actionPerformed  
  165. }//class defination  
  166.  
  167. /********************************************/  
  168.  
  169. class MyOperatorButton extends Button implements ActionListener  
  170. {  
  171. MyCalculator cl;  
  172.  
  173. MyOperatorButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
  174. {  
  175. super(cap);  
  176. setBounds(x,y,width,height);  
  177. this.cl=clc;  
  178. this.cl.add(this);  
  179. addActionListener(this);  
  180. }  
  181. ///////////////////////  
  182. public void actionPerformed(ActionEvent ev)  
  183. {  
  184. String opText=((MyOperatorButton)ev.getSource()).getLabel();  
  185.  
  186. cl.setClear=true;  
  187. double temp=Double.parseDouble(cl.displayLabel.getText());  
  188.  
  189. if(opText.equals("1/x"))  
  190.     {  
  191.     try  
  192.         {double tempd=1/(double)temp;  
  193.         cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
  194.     catch(ArithmeticException excp)  
  195.                         {cl.displayLabel.setText("Divide by 0.");}  
  196.     return;  
  197.     }  
  198. if(opText.equals("sqrt"))  
  199.     {  
  200.     try  
  201.         {double tempd=Math.sqrt(temp);  
  202.         cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
  203.             catch(ArithmeticException excp)  
  204.                     {cl.displayLabel.setText("Divide by 0.");}  
  205.     return;  
  206.     }  
  207. if(!opText.equals("="))  
  208.     {  
  209.     cl.number=temp;  
  210.     cl.op=opText.charAt(0);  
  211.     return;  
  212.     }  
  213. // process = button pressed  
  214. switch(cl.op)  
  215. {  
  216. case '+':  
  217.     temp+=cl.number;break;  
  218. case '-':  
  219.     temp=cl.number-temp;break;  
  220. case '*':  
  221.     temp*=cl.number;break;  
  222. case '%':  
  223.     try{temp=cl.number%temp;}  
  224.     catch(ArithmeticException excp)  
  225.         {cl.displayLabel.setText("Divide by 0."); return;}  
  226.     break;  
  227. case '/':  
  228.     try{temp=cl.number/temp;}  
  229.         catch(ArithmeticException excp)  
  230.                 {cl.displayLabel.setText("Divide by 0."); return;}  
  231.     break;  
  232. }//switch  
  233.  
  234. cl.displayLabel.setText(MyCalculator.getFormattedText(temp));  
  235. //cl.number=temp;  
  236. }//actionPerformed  
  237. }//class  
  238.  
  239. /****************************************/  
  240.  
  241. class MyMemoryButton extends Button implements ActionListener  
  242. {  
  243. MyCalculator cl;  
  244.  
  245. /////////////////////////////////  
  246. MyMemoryButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
  247. {  
  248. super(cap);  
  249. setBounds(x,y,width,height);  
  250. this.cl=clc;  
  251. this.cl.add(this);  
  252. addActionListener(this);  
  253. }  
  254. ////////////////////////////////////////////////  
  255. public void actionPerformed(ActionEvent ev)  
  256. {  
  257. char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1);  
  258.  
  259. cl.setClear=true;  
  260. double temp=Double.parseDouble(cl.displayLabel.getText());  
  261.  
  262. switch(memop)  
  263. {  
  264. case 'C':  
  265.     cl.memLabel.setText(" ");cl.memValue=0.0;break;  
  266. case 'R':  
  267.     cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));break;  
  268. case 'S':  
  269.     cl.memValue=0.0;  
  270. case '+':  
  271.     cl.memValue+=Double.parseDouble(cl.displayLabel.getText());  
  272.     if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0")  )  
  273.         cl.memLabel.setText(" ");  
  274.     else  
  275.         cl.memLabel.setText("M");    
  276.     break;  
  277. }//switch  
  278. }//actionPerformed  
  279. }//class  
  280.  
  281. /*****************************************/  
  282.  
  283. class MySpecialButton extends Button implements ActionListener  
  284. {  
  285. MyCalculator cl;  
  286.  
  287. MySpecialButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
  288. {  
  289. super(cap);  
  290. setBounds(x,y,width,height);  
  291. this.cl=clc;  
  292. this.cl.add(this);  
  293. addActionListener(this);  
  294. }  
  295. //////////////////////  
  296. static String backSpace(String s)  
  297. {  
  298. String Res="";  
  299. for(int i=0; i<s.length()-1; i++) Res+=s.charAt(i);  
  300. return Res;  
  301. }  
  302.  
  303. //////////////////////////////////////////////////////////  
  304. public void actionPerformed(ActionEvent ev)  
  305. {  
  306. String opText=((MySpecialButton)ev.getSource()).getLabel();  
  307. //check for backspace button  
  308. if(opText.equals("Backspc"))  
  309. {  
  310. String tempText=backSpace(cl.displayLabel.getText());  
  311. if(tempText.equals(""))  
  312.     cl.displayLabel.setText("0");  
  313. else  
  314.     cl.displayLabel.setText(tempText);  
  315. return;  
  316. }  
  317. //check for "C" button i.e. Reset  
  318. if(opText.equals("C"))  
  319. {  
  320. cl.number=0.0; cl.op=' '; cl.memValue=0.0;  
  321. cl.memLabel.setText(" ");  
  322. }  
  323.  
  324. //it must be CE button pressed  
  325. cl.displayLabel.setText("0");cl.setClear=true;  
  326. }//actionPerformed  
  327. }//class  
  328.  
  329. /*********************************************
  330. Features not implemented and few bugs
  331.  
  332. i)  No coding done for "+/-" button.
  333. ii) Menubar is not included.
  334. iii)Not for Scientific calculation
  335. iv)Some of the computation may lead to unexpected result
  336.    due to the representation of Floating point numbers in computer
  337.    is an approximation to the given value that can be stored
  338.    physically in memory.
  339. ***********************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement