Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. class cal extends Frame implements ActionListener{
  4. TextField tf;
  5. Button b11,b12,b13,b6,b7,b8,b,b49,b50,b9,b46,b3,b47,b45,b1,b2,b10,b4,b5,b14,b15;
  6. String op="";
  7. double num1=0.0;
  8. double num2=0.0;
  9. double num3=0.0;
  10. double res=0.0;
  11.  
  12. Cal(){
  13. setTitle("Calculator");
  14. setVisible(true);
  15. setSize(500,200);
  16.  
  17. Panel p1=new Panel();
  18. p1.setBackground(Color.red);
  19. Panel p2=new Panel();
  20. p2.setBackground(Color.magenta);
  21. Panel p3=new Panel();
  22. p3.setBackground(Color.GREEN);
  23. Panel p4=new Panel();
  24. p4.setBackground(Color.yellow);
  25.  
  26. BorderLayout br=new BorderLayout();
  27.  
  28. add(p1,br.NORTH);
  29. add(p2,br.CENTER);
  30. add(p3,br.EAST);
  31. p1.add(p4,br.NORTH);
  32.  
  33. tf=new TextField(30);
  34. p1.add(tf);
  35.  
  36. MenuBar mb=new MenuBar();
  37. setMenuBar(mb);
  38.  
  39. Menu m1=new Menu("view");
  40. Menu m2=new Menu("Edit");
  41. Menu m3=new Menu("Help");
  42. mb.add(m1);
  43. mb.add(m2);
  44. mb.add(m3);
  45. MenuItem mi=new MenuItem();
  46. m1.add("Standerd");
  47. m1.add("Scentific");
  48. m1.add("Programmer");
  49. m2.add("copy");
  50. m2.add("paste");
  51. m2.add("History");
  52. m3.add("View Help");
  53. m3.add("about Calculater");
  54.  
  55. CheckboxGroup a=new CheckboxGroup();
  56. Checkbox x=new Checkbox("Degrees",a,true);
  57. Checkbox x1=new Checkbox("Radians",a,true);
  58. Checkbox x2=new Checkbox("Grads",a,true);
  59.  
  60. p4.add(x);
  61. p4.add(x1);
  62. p4.add(x2);
  63.  
  64. Button b0=new Button("");
  65. b=new Button("3");
  66. b1=new Button("+");
  67. b2=new Button("-");
  68. b3=new Button("\u221A");
  69. b4=new Button("%");
  70. b5=new Button("*");
  71. b6=new Button("4");
  72. b7=new Button("C");
  73. b8=new Button("7");
  74. b9=new Button("8");
  75. b10=new Button("0");
  76. b11=new Button("1");
  77. b12=new Button("2");
  78. b13=new Button("6");
  79. b14=new Button("/");
  80. b15=new Button(".");
  81. Button b16=new Button("MC");
  82. Button b17=new Button("MR");
  83. Button b18=new Button("MS");
  84. Button b19=new Button("M+");
  85. Button b20=new Button("M-");
  86. Button b21=new Button("Inv");
  87. Button b22=new Button("In");
  88. Button b23=new Button("(");
  89. Button b24=new Button(")");
  90. Button b25=new Button("Int");
  91. Button b26=new Button("sinh");
  92. Button b27=new Button("sin");
  93. Button b28=new Button("(x*x)");
  94. Button b29=new Button("n!");
  95. Button b30=new Button("dms");
  96. Button b31=new Button("cosh");
  97. Button b32=new Button("cos");
  98. Button b33=new Button("y(x)");
  99. Button b34=new Button("y^x");
  100. Button b35=new Button("\u03c0");
  101. Button b36=new Button("tanh");
  102. Button b37=new Button("tan");
  103. Button b38=new Button("(x*x*x)");
  104. Button b39=new Button("3^x");
  105. Button b40=new Button("F-E");
  106. Button b41=new Button("Exp");
  107. Button b42=new Button("Mod");
  108. Button b43=new Button("Log");
  109. Button b44=new Button("10*x");
  110. b45=new Button("CE");
  111. b46=new Button("9");
  112. b47=new Button("=");
  113. Button b48=new Button("\u00B1");
  114. b49=new Button("\21");
  115. b50=new Button("5");
  116. Button b51=new Button("1/x");
  117.  
  118. p2.add(b0);
  119. p2.add(b21);
  120. p2.add(b22);
  121. p2.add(b23);
  122. p2.add(b24);
  123. p2.add(b25);
  124. p2.add(b26);
  125. p2.add(b27);
  126. p2.add(b28);
  127. p2.add(b29);
  128. p2.add(b30);
  129. p2.add(b31);
  130. p2.add(b32);
  131. p2.add(b33);
  132. p2.add(b34);
  133. p2.add(b35);
  134. p2.add(b35);
  135. p2.add(b36);
  136. p2.add(b37);
  137. p2.add(b38);
  138. p2.add(b39);
  139. p2.add(b40);
  140. p2.add(b41);
  141. p2.add(b42);
  142. p2.add(b43);
  143. p2.add(b44);
  144. p3.add(b16);
  145. p3.add(b17);
  146. p3.add(b18);
  147. p3.add(b19);
  148. p3.add(b20);
  149. p3.add(b45);
  150. p3.add(b7);
  151. p3.add(b8);
  152. p3.add(b9);
  153. p3.add(b46);
  154. p3.add(b14);
  155. p3.add(b50);
  156. p3.add(b49);
  157. p3.add(b14);
  158. p3.add(b4);
  159. p3.add(b5);
  160. p3.add(b6);
  161. p3.add(b50);
  162. p3.add(b13);
  163. p3.add(b51);
  164. p3.add(b1);
  165. p3.add(b2);
  166. p3.add(b3);
  167. p3.add(b11);
  168. p3.add(b12);
  169. p3.add(b10);
  170. p3.add(b);
  171. p3.add(b10);
  172. p3.add(b48);
  173. p3.add(b15);
  174. p3.add(b47);
  175.  
  176. b11.addActionListener(this);//1
  177. b12.addActionListener(this);//2
  178. b.addActionListener(this);//3
  179. b6.addActionListener(this);//4
  180. b50.addActionListener(this);//5
  181. b13.addActionListener(this);//6
  182. b8.addActionListener(this);//7
  183. b9.addActionListener(this);//8
  184. b46.addActionListener(this);//9
  185. b47.addActionListener(this);//=
  186. b1.addActionListener(this);//+
  187. b2.addActionListener(this);//-
  188. b10.addActionListener(this);//0
  189. b4.addActionListener(this);//%
  190. b5.addActionListener(this);//*
  191. b14.addActionListener(this);// /
  192. b15.addActionListener(this);//.
  193. b45.addActionListener(this);//CE
  194. b7.addActionListener(this);//b7
  195. b49.addActionListener(this);//<---
  196. b3.addActionListener(this);
  197.  
  198. GridLayout gr=new GridLayout(5,5,2,2);
  199. GridLayout gr2=new GridLayout(4,5,2,2);
  200. p2.setLayout(gr);
  201. p3.setLayout(gr2);
  202. }
  203. public void actionPerformed(ActionEvent e){
  204. if(e.getSource().equals(b11)){
  205. tf.setText(tf.getText()+e.getActionCommand());
  206. }
  207. if(e.getSource().equals(b12)){
  208. tf.setText(tf.getText()+e.getActionCommand());
  209. }
  210. if(e.getSource().equals(b13)){
  211. tf.setText(tf.getText()+e.getActionCommand());
  212. }
  213. if(e.getSource().equals(b6)){
  214. tf.setText(tf.getText()+e.getActionCommand());
  215. }
  216. if(e.getSource().equals(b8)){
  217. tf.setText(tf.getText()+e.getActionCommand());
  218. }
  219. if(e.getSource().equals(b)){
  220. tf.setText(tf.getText()+e.getActionCommand());
  221. }
  222. if(e.getSource().equals(b50)){
  223. tf.setText(tf.getText()+e.getActionCommand());
  224. }
  225. if(e.getSource().equals(b9)){
  226. tf.setText(tf.getText()+e.getActionCommand());
  227. }
  228. if(e.getSource().equals(b46)){
  229. tf.setText(tf.getText()+e.getActionCommand());
  230. }
  231. if(e.getSource().equals(b10)){
  232. tf.setText(tf.getText()+e.getActionCommand());
  233. }
  234. if(e.getSource().equals(b1)){//+
  235. if(tf.getText().isEmpty()){
  236. tf.setText("");
  237. }else{
  238. String s=tf.getText();
  239. tf.setText("");
  240. op=e.getActionCommand();
  241. num1=Double.parseDouble(s);
  242. }
  243. }
  244. if(e.getSource().equals(b2)){//-
  245. if(tf.getText().isEmpty()){
  246. tf.setText("");
  247. }else{
  248. String s=tf.getText();
  249. tf.setText("");
  250. op=e.getActionCommand();
  251. num1=Double.parseDouble(s);
  252. }
  253. }
  254. if(e.getSource().equals(b5)){//*
  255. if(tf.getText().isEmpty()){
  256. tf.setText("");
  257. }else{
  258. String s=tf.getText();
  259. tf.setText("");
  260. op=e.getActionCommand();
  261. num1=Double.parseDouble(s);
  262. }
  263. }
  264. if(e.getSource().equals(b14)){// /
  265. if(tf.getText().isEmpty()){
  266. tf.setText("");
  267. }else{
  268. String s=tf.getText();
  269. tf.setText("");
  270. op=e.getActionCommand();
  271. num1=Double.parseDouble(s);
  272. }
  273. }
  274. if(e.getSource().equals(b15)){//.
  275. if(tf.getText().isEmpty()){
  276. tf.setText("");
  277. }else{
  278. tf.setText(tf.getText()+e.getActionCommand());
  279. }
  280. }
  281. if(e.getSource().equals(b45)){//CE
  282. tf.setText("");
  283. }
  284. if(e.getSource().equals(b7)){//C
  285. tf.setText("");
  286. }
  287. if(e.getSource().equals(b49)){//<--
  288. if(tf.getText().isEmpty()){
  289. tf.setText("");
  290. }else{
  291. String s=tf.getText();
  292. String ss=s.substring(0,s.length()-1);
  293. tf.setText(ss);
  294. }
  295. }
  296. if(e.getSource().equals(b3)){//math sqrt
  297. if(tf.getText().isEmpty()){
  298. tf.setText("");
  299. }else{
  300. String s=tf.getText();
  301. num1=Double.parseDouble(s);
  302. System.out.println(s);
  303.  
  304. res=Math.sqrt(num1);
  305. tf.setText(res+"");
  306.  
  307. }
  308. }
  309. if(e.getSource().equals(b4)){//%
  310. if(tf.getText().isEmpty()){
  311. tf.setText("");
  312. }else{
  313. String s=tf.getText();
  314. num2=Double.parseDouble(s);
  315. if(op.equals("+")){
  316. res=num1*num2/100;
  317. tf.setText(res+"");
  318. res=num2;
  319. }
  320. if(op.equals("-")){
  321. res=num1*num2/100;
  322. tf.setText(res+"");
  323. res=num2;
  324. }
  325. if(op.equals("*")){
  326. res=num1*num2/100;
  327. tf.setText(res+"");
  328. res=num2;
  329. }
  330. if(op.equals("/")){
  331. res=num2/num1;
  332. tf.setText(res+"");
  333. res=num2;
  334. }
  335. }
  336.  
  337. }
  338. if(e.getSource().equals(b47)){//=
  339. if(tf.getText().isEmpty()){
  340. tf.setText("");
  341. }else{
  342. String s=tf.getText();
  343. num2=Double.parseDouble(s);
  344. if(op.equals("+")){
  345. res=num1+num2;
  346. tf.setText(res+"");
  347. }
  348. if(op.equals("-")){
  349. res=num1-num2;
  350. tf.setText(res+"");
  351. }
  352. if(op.equals("*")){
  353. res=num1*num2;
  354. tf.setText(res+"");
  355. }
  356. if(op.equals("/")){
  357. res=num1/num2;
  358. tf.setText(res+"");
  359. }
  360. }
  361. }
  362.  
  363. }
  364. }
  365. public class Calculator{
  366. public static void main(String args[]){
  367. cal a=new cal();
  368. }
  369. }
Add Comment
Please, Sign In to add comment