calculator JAVA


SUBMITTED BY: Guest

DATE: Dec. 1, 2013, 12:31 p.m.

FORMAT: Java

SIZE: 8.1 kB

HITS: 955

  1. Pertama, kita perlu ktahui ada pakage apa saja dlm sbuah aplikasi yg akan kita bangun, dan dalam aplikasi yg ini hanya ada 3 pakage, yaitu : pakege form, pakage entitas, pakage main..
  2. disetiap pakage akan berisi class'nya masing..
  3. pakage form = Berisi semua source form yg akan kita bangun termasuk even2'a..
  4. pakage entitas = Berisi semua source entitas utk menghubungkan antara entitas dgn form..
  5. pakage main = sbnarnya ini tidak begitu penting, hanya utk running program slm msih dalam tahap pembangunan..
  6. Tahap pertama Desain Form :
  7. [Image: 79302091.png]
  8. Langsung memberikan nama variable disetiap Swing..
  9. Tahap kedua setting running program melalui class main :
  10. Code:
  11. package calculatorujicoba;
  12. import Tampilan.TampilanUtama;
  13. /*
  14. * Programmer : Fajar Musti Prawira
  15. *
  16. * Created on 11 Nov 11, 21:28:41
  17. */
  18. public class CalculatorUjicoba {
  19. /**
  20. * @param args the command line arguments
  21. */
  22. public static void main(String[] args) {
  23. // TODO code application logic here
  24. TampilanUtama Tu = new TampilanUtama();
  25. Tu.setVisible(true);
  26. Tu.setLocationRelativeTo(null);
  27. }
  28. }
  29. Tahap ketiga Buat class entitas :
  30. PHP Code:
  31. package Entitas;
  32. /*
  33. * Programmer : Fajar Musti Prawira
  34. *
  35. * Created on 11 Nov 11, 21:28:41
  36. */
  37. public class EntitiCalcu {
  38. int operator = 0;
  39. double nilai1;
  40. double nilai2;
  41. double result;
  42. public void setNilai(String nl){
  43. if (!nl.equals("")){
  44. if(operator==0){
  45. nilai1= Double.valueOf(nl);
  46. }else{
  47. nilai2= Double.valueOf(nl);
  48. }
  49. }
  50. }
  51. public void setOperator(int operator) {
  52. this.operator = operator;
  53. }
  54. public double getResult() {
  55. return result;
  56. }
  57. public void setResult(double hasil) {
  58. this.result = hasil;
  59. }
  60. public void proses(){
  61. switch(operator){
  62. case 1:
  63. result = nilai1 + nilai2;
  64. break;
  65. case 2:
  66. result = nilai1 - nilai2;
  67. break;
  68. case 3:
  69. result = nilai1 * nilai2;
  70. break;
  71. case 4:
  72. result = nilai1 / nilai2;
  73. break;
  74. case 5:
  75. result = nilai1 % nilai2;
  76. break;
  77. case 6:
  78. result = 1/nilai1;
  79. break;
  80. }
  81. nilai1=result;
  82. }
  83. }
  84. Sebelum kita memberikan event di setiap swing, maka trlbh dahulu kita membuat methode..
  85. Code:
  86. //Menurunkan
  87. EntitiCalcu model = new EntitiCalcu();
  88. String oper="";
  89. //Ambil nilai
  90. public void getOper(javax.swing.JButton button){
  91. oper+=button.getText();
  92. model.setNilai(oper);
  93. resultHasil.setText(oper);
  94. }
  95. private void getOperator(int opt){
  96. model.setOperator(opt);
  97. oper="";
  98. }
  99. private void proses(){
  100. DecimalFormat df = new DecimalFormat("#,###.########");
  101. model.proses();
  102. oper="";
  103. resultHasil.setText(df.format(model.getResult())+"");
  104. }
  105. Lanjutkan Memberikan event di setiap butoon..
  106. sesuai namanya yaaa..
  107. Code:
  108. private void bnt_tambahActionPerformed(java.awt.event.ActionEvent evt) {
  109. // TODO add your handling code here:
  110. getOperator(1);
  111. }
  112. private void bnt_kurangActionPerformed(java.awt.event.ActionEvent evt) {
  113. // TODO add your handling code here:
  114. getOperator(2);
  115. }
  116. private void bnt_kaliActionPerformed(java.awt.event.ActionEvent evt) {
  117. // TODO add your handling code here:
  118. getOperator(3);
  119. }
  120. private void bnt_bagiActionPerformed(java.awt.event.ActionEvent evt) {
  121. // TODO add your handling code here:
  122. getOperator(4);
  123. }
  124. private void bnt_persenActionPerformed(java.awt.event.ActionEvent evt) {
  125. // TODO add your handling code here:
  126. getOperator(5);
  127. }
  128. private void bnt_samadengaActionPerformed(java.awt.event.ActionEvent evt) {
  129. // TODO add your handling code here:
  130. proses();
  131. }
  132. private void bnt_1ActionPerformed(java.awt.event.ActionEvent evt) {
  133. // TODO add your handling code here:
  134. getOper(bnt_1);
  135. }
  136. private void bnt_2ActionPerformed(java.awt.event.ActionEvent evt) {
  137. // TODO add your handling code here:
  138. getOper(bnt_2);
  139. }
  140. private void bnt_3ActionPerformed(java.awt.event.ActionEvent evt) {
  141. // TODO add your handling code here:
  142. getOper(bnt_3);
  143. }
  144. private void bnt_4ActionPerformed(java.awt.event.ActionEvent evt) {
  145. // TODO add your handling code here:
  146. getOper(bnt_4);
  147. }
  148. private void bnt_5ActionPerformed(java.awt.event.ActionEvent evt) {
  149. // TODO add your handling code here:
  150. getOper(bnt_5);
  151. }
  152. private void bnt_6ActionPerformed(java.awt.event.ActionEvent evt) {
  153. // TODO add your handling code here:
  154. getOper(bnt_6);
  155. }
  156. private void bnt_7ActionPerformed(java.awt.event.ActionEvent evt) {
  157. // TODO add your handling code here:
  158. getOper(bnt_7);
  159. }
  160. private void bnt_8ActionPerformed(java.awt.event.ActionEvent evt) {
  161. // TODO add your handling code here:
  162. getOper(bnt_8);
  163. }
  164. private void bnt_9ActionPerformed(java.awt.event.ActionEvent evt) {
  165. // TODO add your handling code here:
  166. getOper(bnt_9);
  167. }
  168. private void bnt_titikActionPerformed(java.awt.event.ActionEvent evt) {
  169. // TODO add your handling code here:
  170. getOper(bnt_titik);
  171. }
  172. private void bnt_0ActionPerformed(java.awt.event.ActionEvent evt) {
  173. // TODO add your handling code here:
  174. getOper(bnt_0);
  175. }
  176. private void bnt_cActionPerformed(java.awt.event.ActionEvent evt) {
  177. // TODO add your handling code here:
  178. if(oper.length()>1){
  179. oper = oper.substring(0, oper.length()-1);
  180. model.setNilai(oper);
  181. resultHasil.setText(oper);
  182. }else{
  183. oper = "";
  184. model.setNilai(oper);
  185. resultHasil.setText("0");
  186. }
  187. }
  188. private void bnt_ACActionPerformed(java.awt.event.ActionEvent evt) {
  189. // TODO add your handling code here:
  190. oper ="";
  191. model.setOperator(0);
  192. model.setResult(0);
  193. resultHasil.setText("0");
  194. }
  195. SELESAI, GOOD JOB Bahagia

comments powered by Disqus