0% found this document useful (0 votes)
50 views7 pages

Head

This document describes a simple calculator application created using PHP and the MVC pattern. It includes the code for the HTML form interface, a PHP function to execute calculations, and Java classes for the calculator model and view. The model class handles calculations and data, while the view class controls button clicks and displays results. The main class instantiates the view to launch the GUI calculator.

Uploaded by

Zai Udin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Head

This document describes a simple calculator application created using PHP and the MVC pattern. It includes the code for the HTML form interface, a PHP function to execute calculations, and Java classes for the calculator model and view. The model class handles calculations and data, while the view class controls button clicks and displays results. The main class instantiates the view to launch the GUI calculator.

Uploaded by

Zai Udin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

<head><title>Aplikasi Kalkulator Sederhana</title></head> <body> <form method="post" action="<? echo $_PHP_SELF;?>"> <input type="text" name="bil1" size="10" value="<?

echo $_POST['bil1']?>"> <input type="text" name="bil2" size="10" value="<? echo $_POST['bil2']?>"> = <input type="text" name="hasil" size="10" readonly="yes" value="<? echo eksekusi();?>"> <br /><br /> <input type="submit" name="operator" value="+"> <input type="submit" name="operator" value="-"> <input type="submit" name="operator" value="/"> <input type="submit" name="operator" value="X"> </form> </body> </html> <? function eksekusi() { $op = $_POST['operator']; $bil1 = $_POST['bil1']; $bil2 = $_POST['bil2']; if($op == "+") { $hasil = $bil1 + $bil2; } elseif($op == "-") { $hasil = $bil1 - $bil2; } elseif($op == "/") { $hasil = $bil1 / $bil2; } elseif($op == "X") { $hasil = $bil1 * $bil2; } return $hasil; } ?>

dalam program sederhana ini aku cuma coba memberikan contoh beberapa operasi aritmatika antara lain penjumlahan, pengurangan, perkalian dan pembagian. penjelasan program * Form Inputan
1 2 3 4 5 6 7 <head><title>Aplikasi Kalkulator Sederhana</title></head> <body> <form method="post" action="<? echo $_PHP_SELF;?>"> <input type="text" name="bil1" size="10" value="<? echo $_POST['bil1']?>"> <input type="text" name="bil2" size="10" value="<? echo $_POST['bil2']?>"> = <input type="text" name="hasil" size="10" readonly="yes" value="<? echo

8 9 10 11 12 13 14

eksekusi();?>"> <br /><br /> <input type="submit" <input type="submit" <input type="submit" <input type="submit" </form> </body> </html>

name="operator" name="operator" name="operator" name="operator"

value="+"> value="-"> value="/"> value="X">

pada bagian ini merupakan standart penulisan code untuk html dan sedikit disisipi dengan PHP * Function eksekusi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <? function eksekusi() { $op = $_POST['operator']; $bil1 = $_POST['bil1']; $bil2 = $_POST['bil2']; if($op == "+") { $hasil = $bil1 + $bil2; } elseif($op == "-") { $hasil = $bil1 - $bil2; } elseif($op == "/") { $hasil = $bil1 / $bil2; } elseif($op == "X") { $hasil = $bil1 * $bil2; } return $hasil; } ?>

public class CalculatorModel { int operator=0; double operand1=0; double operand2=0; double result=0; public void setOperand(String opr) { if(!opr.equals("")){ if(operator==0){ operand1=Double.valueOf(opr); }else{

operand2=Double.valueOf(opr); } } } public void setOperator(int operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double hasil) { this.result = hasil; } public void process(){ switch (operator){ case 1: result = operand1 + operand2; break; case 2: result = operand1 - operand2; break; case 3: result = operand1 * operand2; break; case 4: result = operand1 / operand2; break; case 5: result = operand1 % operand2; break; case 6: result = 1/operand1; break; default: result = operand1; } operand1=result; } public void setOperand1(double operand1) { this.operand1 = operand1; } public void setOperand2(double operand2) { this.operand2 = operand2; } public void clear(){ setOperand1(0); setOperand2(0); setResult(0); setOperator(0); }

dan berikut adalah class view kalkulator. Disini hanya saya tampilkan source yang saya buat. Source yang digenerate oleh netbeans tidak saya cantumkan.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package view; import java.text.DecimalFormat; import javax.swing.JOptionPane; import model.CalculatorModel; public class CalculatorView extends javax.swing.JFrame { /** Creates new form CalculatorView */ public CalculatorView() { initComponents(); } CalculatorModel model = new CalculatorModel(); String operand=""; public void getOperand(javax.swing.JButton button){ operand+=button.getText(); resultLabel.setText(operand); } private void getOperator(int opt){ model.setOperand(operand); model.setOperator(opt); if(opt == 1 || opt == 2){ model.process(); resultLabel.setText(model.getResult()+""); }else{ resultLabel.setText(operand); } operand = ""; } private void process(){ DecimalFormat df = new DecimalFormat("#,###.########"); model.setOperand(operand); model.process(); operand = ""; resultLabel.setText(df.format(model.getResult())+""); } private void button1ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button1); } private void button2ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button2); } private void button3ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button3);

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

} private void button4ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button4); } private void button5ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button5); } private void button6ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button6); } private void button7ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button7); } private void button8ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button8); } private void button9ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button9); } private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) { getOperator(1); } private void buttonKurangActionPerformed(java.awt.event.ActionEvent evt) { getOperator(2); } private void buttonKaliActionPerformed(java.awt.event.ActionEvent evt) { getOperator(3); } private void buttonBagiActionPerformed(java.awt.event.ActionEvent evt) { getOperator(4); } private void buttonModulusActionPerformed(java.awt.event.ActionEvent evt) { getOperator(5); } private void buttonSeperActionPerformed(java.awt.event.ActionEvent evt) { getOperator(6); }

106 private void 107 buttonSamaDenganActionPerformed(java.awt.event.ActionEvent evt) { process(); 108 } 109 110 111 private void button11ActionPerformed(java.awt.event.ActionEvent evt) { 112 getOperand(button11); 113 } 114 115 private void buttonKomaActionPerformed(java.awt.event.ActionEvent evt) 116 { 117 getOperand(buttonKoma); 118 } 119 120 private void button12ActionPerformed(java.awt.event.ActionEvent evt) { if(operand.length()&gt;1){ 121 operand = operand.substring(0, operand.length()-1); 122 model.setOperand(operand); 123 124 resultLabel.setText(operand); 125 }else{ 126 operand = ""; 127 model.setOperand(operand); 128 resultLabel.setText("0"); 129 } 130 } 131 132 private void buttonACActionPerformed(java.awt.event.ActionEvent evt) { 133 operand = ""; 134 model.clear(); 135 resultLabel.setText("0"); 136 } 137 138 private void menuExitActionPerformed(java.awt.event.ActionEvent evt) { 139 if(JOptionPane.showConfirmDialog(rootPane, "Are you sure you will 140 exit this application?","Exit Application",1)==0){ 141 System.exit(0); 142 } } private void aboutMenuActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(rootPane, "This is a simple example of a calculator that uses the concept of MVC." + "View and process are placed in separate classes." + "Made by Muhamad Nur"); } }

Dan untuk menjalankannya jangan lupa buat class main


1 import view.CalculatorView; 2 3 public class Main { 4

5 6 7 8 9}

public static void main(String[] args) { new CalculatorView().setVisible(true); }

You might also like