Challenge

Project

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. /*********************************************/
  4. public class MyCalculator extends Frame
  5. {
  6. public boolean setClear=true;
  7. double number, memValue;
  8. char op;
  9. String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };
  10. String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };
  11. String memoryButtonText[] = {"MC", "MR", "MS", "M+" };
  12. String specialButtonText[] = {"Backspc", "C", "CE" };
  13. MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];
  14. MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];
  15. MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];
  16. MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];
  17. Label displayLabel=new Label("0",Label.RIGHT);
  18. Label memLabel=new Label(" ",Label.RIGHT);
  19. final int FRAME_WIDTH=325,FRAME_HEIGHT=325;
  20. final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;
  21. final int TOPX=30, TOPY=50;
  22. ///////////////////////////
  23. MyCalculator(String frameText)//constructor
  24. {
  25. super(frameText);
  26. int tempX=TOPX, y=TOPY;
  27. displayLabel.setBounds(tempX,y,240,HEIGHT);
  28. displayLabel.setBackground(Color.BLUE);
  29. displayLabel.setForeground(Color.WHITE);
  30. add(displayLabel);
  31. memLabel.setBounds(TOPX, TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);
  32. add(memLabel);
  33. // set Co-ordinates for Memory Buttons
  34. tempX=TOPX;
  35. y=TOPY+2*(HEIGHT+V_SPACE);
  36. for(int i=0; i<memoryButton.length; i++)
  37. {
  38. memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);
  39. memoryButton[i].setForeground(Color.RED);
  40. y+=HEIGHT+V_SPACE;
  41. }
  42. //set Co-ordinates for Special Buttons
  43. tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);
  44. for(int i=0;i<specialButton.length;i++)
  45. {
  46. specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);
  47. specialButton[i].setForeground(Color.RED);
  48. tempX=tempX+2*WIDTH+H_SPACE;
  49. }
  50. //set Co-ordinates for Digit Buttons
  51. int digitX=TOPX+WIDTH+H_SPACE;
  52. int digitY=TOPY+2*(HEIGHT+V_SPACE);
  53. tempX=digitX; y=digitY;
  54. for(int i=0;i<digitButton.length;i++)
  55. {
  56. digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);
  57. digitButton[i].setForeground(Color.BLUE);
  58. tempX+=WIDTH+H_SPACE;
  59. if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}
  60. }
  61. //set Co-ordinates for Operator Buttons
  62. int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;
  63. int opsY=digitY;
  64. tempX=opsX; y=opsY;
  65. for(int i=0;i<operatorButton.length;i++)
  66. {
  67. tempX+=WIDTH+H_SPACE;
  68. operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);
  69. operatorButton[i].setForeground(Color.RED);
  70. if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}
  71. }
  72. addWindowListener(new WindowAdapter(
  73. new WindowAdapter()
  74. {
  75. public void windowClosing(WindowEvent ev)
  76. {System.exit(0);}
  77. });
  78. setLayout(null);
  79. setSize(FRAME_WIDTH,FRAME_HEIGHT);
  80. setVisible(true);
  81. }
  82. //////////////////////////////////
  83. static String getFormattedText(double temp)
  84. {
  85. String resText=""+temp;
  86. if(resText.lastIndexOf(".0")>0)
  87. resText=resText.substring(0,resText.length()-2);
  88. return resText;
  89. }
  90. ////////////////////////////////////////
  91. public static void main(String []args)
  92. {
  93. new MyCalculator("Calculator - JavaTpoint");
  94. }
  95. }
  96. /*******************************************/
  97. class MyDigitButton extends Button implements ActionListener
  98. {
  99. MyCalculator cl;
  100. //////////////////////////////////////////
  101. MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc)
  102. {
  103. super(cap);
  104. setBounds(x,y,width,height);
  105. this.cl=clc;
  106. this.cl.add(this);
  107. addActionListener(this);
  108. }
  109. ////////////////////////////////////////////////
  110. static boolean isInString(String s, char ch)
  111. {
  112. for(int i=0; i<s.length();i++) if(s.charAt(i)==ch) return true;
  113. return false;
  114. }
  115. /////////////////////////////////////////////////
  116. public void actionPerformed(ActionEvent ev)
  117. {
  118. String tempText=((MyDigitButton)ev.getSource()).getLabel();
  119. if(tempText.equals("."))
  120. {
  121. if(cl.setClear)
  122. {cl.displayLabel.setText("0.");cl.setClear=false;}
  123. else if(!isInString(cl.displayLabel.getText(),'.'))
  124. cl.displayLabel.setText(cl.displayLabel.getText()+".");
  125. return;
  126. }
  127. int index=0;
  128. try{
  129. index=Integer.parseInt(tempText);
  130. }catch(NumberFormatException e){return;}
  131. if (index==0 && cl.displayLabel.getText().equals("0")) return;
  132. if(cl.setClear)
  133. {cl.displayLabel.setText(""+index);cl.setClear=false;}
  134. else
  135. cl.displayLabel.setText(cl.displayLabel.getText()+index);
  136. }//actionPerformed
  137. }//class defination
  138. /********************************************/
  139. class MyOperatorButton extends Button implements ActionListener
  140. {
  141. MyCalculator cl;
  142. MyOperatorButton(int x,int y, int width,int height,String cap, MyCalculator clc)
  143. {
  144. super(cap);
  145. setBounds(x,y,width,height);
  146. this.cl=clc;
  147. this.cl.add(this);
  148. addActionListener(this);
  149. }
  150. ///////////////////////
  151. public void actionPerformed(ActionEvent ev)
  152. {
  153. String opText=((MyOperatorButton)ev.getSource()).getLabel();
  154. cl.setClear=true;
  155. double temp=Double.parseDouble(cl.displayLabel.getText());
  156. if(opText.equals("1/x"))
  157. {
  158. try
  159. {double tempd=1/(double)temp;
  160. cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}
  161. catch(ArithmeticException excp)
  162. {cl.displayLabel.setText("Divide by 0.");}
  163. return;
  164. }
  165. if(opText.equals("sqrt"))
  166. {
  167. try
  168. {double tempd=Math.sqrt(temp);
  169. cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}
  170. catch(ArithmeticException excp)
  171. {cl.displayLabel.setText("Divide by 0.");}
  172. return;
  173. }
  174. if(!opText.equals("="))
  175. {
  176. cl.number=temp;
  177. cl.op=opText.charAt(0);
  178. return;
  179. }
  180. // process = button pressed
  181. switch(cl.op)
  182. {
  183. case '+':
  184. temp+=cl.number;break;
  185. case '-':
  186. temp=cl.number-temp;break;
  187. case '*':
  188. temp*=cl.number;break;
  189. case '%':
  190. try{temp=cl.number%temp;}
  191. catch(ArithmeticException excp)
  192. {cl.displayLabel.setText("Divide by 0."); return;}
  193. break;
  194. case '/':
  195. try{temp=cl.number/temp;}
  196. catch(ArithmeticException excp)
  197. {cl.displayLabel.setText("Divide by 0."); return;}
  198. break;
  199. }//switch
  200. cl.displayLabel.setText(MyCalculator.getFormattedText(temp));
  201. //cl.number=temp;
  202. }//actionPerformed
  203. }//class
  204. ****************************************/
  205. class MyMemoryButton extends Button implements ActionListener
  206. {
  207. MyCalculator cl;
  208. /////////////////////////////////
  209. MyMemoryButton(int x,int y, int width,int height,String cap, MyCalculator clc)
  210. {
  211. super(cap);
  212. setBounds(x,y,width,height);
  213. this.cl=clc;
  214. this.cl.add(this);
  215. addActionListener(this);
  216. }
  217. ////////////////////////////////////////////////
  218. public void actionPerformed(ActionEvent ev)
  219. {
  220. char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1);
  221. cl.setClear=true;
  222. double temp=Double.parseDouble(cl.displayLabel.getText());
  223. switch(memop)
  224. {
  225. case 'C':
  226. cl.memLabel.setText(" ");cl.memValue=0.0;break;
  227. case 'R':
  228. cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));break;
  229. case 'S':
  230. cl.memValue=0.0;
  231. case '+':
  232. cl.memValue+=Double.parseDouble(cl.displayLabel.getText());
  233. if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0") )
  234. cl.memLabel.setText(" ");
  235. else
  236. cl.memLabel.setText("M");
  237. break;
  238. }//switch
  239. }//actionPerformed
  240. }//class
  241. /*****************************************/
  242. class MySpecialButton extends Button implements ActionListener
  243. {
  244. MyCalculator cl;
  245. MySpecialButton(int x,int y, int width,int height,String cap, MyCalculator clc)
  246. {
  247. super(cap);
  248. setBounds(x,y,width,height);
  249. this.cl=clc;
  250. this.cl.add(this);
  251. addActionListener(this);
  252. }
  253. //////////////////////
  254. static String backSpace(String s)
  255. {
  256. String Res="";
  257. for(int i=0; i<s.length()-1; i++) Res+=s.charAt(i);
  258. return Res;
  259. }
  260. //////////////////////////////////////////////////////////
  261. public void actionPerformed(ActionEvent ev)
  262. {
  263. String opText=((MySpecialButton)ev.getSource()).getLabel();
  264. //check for backspace button
  265. if(opText.equals("Backspc"))
  266. {
  267. String tempText=backSpace(cl.displayLabel.getText());
  268. if(tempText.equals(""))
  269. cl.displayLabel.setText("0");
  270. else
  271. cl.displayLabel.setText(tempText);
  272. return;
  273. }
  274. //check for "C" button i.e. Reset
  275. if(opText.equals("C"))
  276. {
  277. cl.number=0.0; cl.op=' '; cl.memValue=0.0;
  278. cl.memLabel.setText(" ");
  279. }
  280. //it must be CE button pressed
  281. cl.displayLabel.setText("0");cl.setClear=true;
  282. }//actionPerformed
  283. }//class
  284. /*********************************************
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.