Minggu, 30 Oktober 2016
Minggu, 16 Oktober 2016
Calculator Application Design Top-Down
Source Code
import javax.swing.*;
import java.awt.event.*;
class Calc implements ActionListener
{
JFrame f;
JTextField t;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
static double a=0,b=0,result=0;
static int operator=0;
Calc()
{
f=new JFrame("Calculator");
t=new JTextField();
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
beq=new JButton("=");
bdel=new JButton("Delete");
bclr=new JButton("Clear");
t.setBounds(30,40,280,30);
b7.setBounds(40,100,50,40);
b8.setBounds(110,100,50,40);
b9.setBounds(180,100,50,40);
bdiv.setBounds(250,100,50,40);
b4.setBounds(40,170,50,40);
b5.setBounds(110,170,50,40);
b6.setBounds(180,170,50,40);
bmul.setBounds(250,170,50,40);
b1.setBounds(40,240,50,40);
b2.setBounds(110,240,50,40);
b3.setBounds(180,240,50,40);
bsub.setBounds(250,240,50,40);
bdec.setBounds(40,310,50,40);
b0.setBounds(110,310,50,40);
beq.setBounds(180,310,50,40);
badd.setBounds(250,310,50,40);
bdel.setBounds(60,380,100,40);
bclr.setBounds(180,380,100,40);
f.add(t);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(bdiv);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(bmul);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(bsub);
f.add(bdec);
f.add(b0);
f.add(beq);
f.add(badd);
f.add(bdel);
f.add(bclr);
f.setLayout(null);
f.setVisible(true);
f.setSize(350,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bdiv.addActionListener(this);
bmul.addActionListener(this);
bsub.addActionListener(this);
bdec.addActionListener(this);
beq.addActionListener(this);
bdel.addActionListener(this);
bclr.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText(t.getText().concat("1"));
if(e.getSource()==b2)
t.setText(t.getText().concat("2"));
if(e.getSource()==b3)
t.setText(t.getText().concat("3"));
if(e.getSource()==b4)
t.setText(t.getText().concat("4"));
if(e.getSource()==b5)
t.setText(t.getText().concat("5"));
if(e.getSource()==b6)
t.setText(t.getText().concat("6"));
if(e.getSource()==b7)
t.setText(t.getText().concat("7"));
if(e.getSource()==b8)
t.setText(t.getText().concat("8"));
if(e.getSource()==b9)
t.setText(t.getText().concat("9"));
if(e.getSource()==b0)
t.setText(t.getText().concat("0"));
if(e.getSource()==bdec)
t.setText(t.getText().concat("."));
if(e.getSource()==badd)
{
a=Double.parseDouble(t.getText());
operator=1;
t.setText("");
}
if(e.getSource()==bsub)
{
a=Double.parseDouble(t.getText());
operator=2;
t.setText("");
}
if(e.getSource()==bmul)
{
a=Double.parseDouble(t.getText());
operator=3;
t.setText("");
}
if(e.getSource()==bdiv)
{
a=Double.parseDouble(t.getText());
operator=4;
t.setText("");
}
if(e.getSource()==beq)
{
b=Double.parseDouble(t.getText());
switch(operator)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
default: result=0;
}
t.setText(""+result);
}
if(e.getSource()==bclr)
t.setText("");
if(e.getSource()==bdel)
{
String s=t.getText();
t.setText("");
for(int i=0;i<s.length()-1;i++)
t.setText(t.getText()+s.charAt(i));
}
}
public static void main(String...s)
{
new Calc();
}
}
Source : http://www.thecrazyprogrammer.com/2014/06/program-to-create-calculator-using-java-swing.html
Use Case
Class Diagram
CRC Card
Class Calc
Responsibilities
|
Collabolator
|
Allow user to set operand
|
|
Allow user to add operand
|
|
Allow user to substract operand
|
|
Allow user to multiply operand
|
|
Allow user to divide operand
|
|
Allow user to add decimal point to operand
|
|
Allow user to delete operand
|
|
Allow user to clear the display
|
Sequence Diagram
Activity Diagram
Ahmad Bilal
5114100121
PPL C
Minggu, 02 Oktober 2016
CRC dan Class Diagram Aplikasi Address Book
1. Use Case
2. CRC Cards
Class AddressBook
Responsibilities
|
Collaborators
|
Menampung semua objek Person
|
|
Memiliki data jumlah semua objek Person
|
|
Update perubahan pada AddressBook
|
|
Update Person
|
Person
|
Remove Person
|
Person
|
Urutkan berdasarkan nama
|
|
Urutkan berdasarkan ZIP
|
|
Cetak / Print AddressBook
|
Class AddressBookController
Sebagai controller dari aplikasi (melayani fitur-fitur yang ada)
Responsibilities
|
Collaborators
|
User dapat melakukan Add Person Use Case
|
|
User dapat melakukan Edit Person Use Case
|
|
User dapat melakukan Delete a Person Use Case
|
|
User dapat melakukan Sort Entries by Name Use Case
|
|
User dapat melakukan Sort Entries by ZIP Use Case
|
|
User dapat melakukan Create New Address Book Use Case
|
|
User dapat melakukan Open Existing Address Book Use Case
|
|
User dapat melakukan Save Address Book Use Case
|
|
User dapat melakukan Save Address Book As ... Use Case
|
|
User dapat melakukan Print Entries Use Case
|
Class AddressBookGUI
Sebagai
view dari aplikasi Address Book yang membolehkan user untuk berinteraksi dengan
aplikasi.
Responsibilities
|
Collaborators
|
Menjaga objek address book yang sedang ditampilkan
| |
Menampilkan nama-nama orang pada address book
| |
Menampilkan judul dari address book
| |
Menjaga state dari menu “Save” – dalam artian menu tersebut hanya bisa
digunakan ketika ada perubahan pada address book sejak address book dibuka
| |
Membolehkan user untuk menggunakan fungsi dari sebuah use case
|
Class FileSystem
Berfungsi
untuk mengatur interaksi antara aplikasi dengan filesystem dimana aplikasi
berjalan.
Collaborators
|
|
Membaca (Read)
address book yang tersimpan dari sebuah file
|
|
Menyimpan
sebuah address book ke suatu file
|
Class Person
Berfungsi
menjaga informasi tentang suatu individu
Responsibilities
|
Collaborators
|
Membuat sebuah objek, diketahui nama seseorang, alamat, kota, state, ZIP/kode
pos, dan phone (nomor telepon)
|
|
Edit nama awal
|
|
Edit nama akhir
|
|
Edit alamat
|
|
Edit kota
|
|
Edit state
|
|
Edit ZIP / kode pos
|
|
Edit nomor telepon
|
|
Update informasi seseorang dalam address book
|
3. Class
5. Check Realization
USE
CASE
|
CLASS
|
Add a Person
|
AddressBookController, Person
|
Edit a Person
|
AddressBookController, Person
|
Delete a Person
|
AddressBookController, Person
|
Sort Entries by Name
|
AddressBook, AddressBookController
|
Sort Entries by ZIP
|
AddressBook, AddressBookController
|
Print Entries
|
AddressBook, AddressBookController
|
Create New Address Book
|
AddressBookController, FileSystem
|
Open Existing Address Book
|
AddressBookController, FileSystem
|
Save Address Book
|
AddressBook , FileSystem
|
Save Address Book As
|
AddressBook
|
Quit Program
|
AddressBookApplication
|
Offer to Save Changes
|
AddressBook, AddressBookGUI
|
Ahmad Bilal
5114100121
APSI C
Langganan:
Postingan (Atom)