Praktikum Inheritance

Slides:



Advertisements
Presentasi serupa
EXCEPTION HANDLING.
Advertisements

- PERTEMUAN 3- INHERITANCE
Nur Hayatin, S.ST Jurusan Teknik Informatika Universitas Muhammadiyah Malang Sem Genap 2010.
POLIMORFISME Imam Fahrur Rozi 01.
Pemrograman JAVA (TIB09)
Workshop SCS: Java Game Programming
Pemrograman Berorientasi Objek
Overriding, Overloading, Polymorphism
Pemrograman Java Pewarisan / Inheritance Polimorfisme Pemrograman Java.
Inheritance Ery Setiyawan Jullev A.
Inheritance (Pewarisan)
Praktikum OOP Pengenalan Java Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2009.
Overriding, Overloading, Polymorphism
POLIMORFISME 2 Imam Fahrur Rozi 01.
Pemrograman Berbasis Obyek
Pertemuan 8 Pemrograman Berbasis Obyek Oleh Tita Karlita
Praktikum 8 Pemrograman Berbasis Obyek
Flow Control Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2008.
Encapsulation, Inheritance, polymorphism, dan interface
Variabel, Method & Constructor Oleh : Nur Hayatin, S.ST Teknik Informatika – UMM 2011.
Pemrograman Berorientasi Objek
INHERITANCE, POLYMORPHISM, DAN INTERFACE
Pewarisan, Polimorfisme, dan Interface
Inheritance (Pewarisan)
Pengenalan Pemrograman Berbasis Obyek
Pemrograman Berorientasi Obyek Oleh Tita Karlita
METHOD Object Oriented Programming with JAVA 2011/2012.
INHERITANCE Perbankan Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2009.
Object Oriented Struktur Data.
Pertemuan : Object Oriented Programming
OBJECT ORIENTED PROGRAMMING YANES HARDIANTO SUI Politeknik Manufaktur Astra 2011.
Inheritance Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2009.
07 Advanced Class Features
Object Oriented Programming with JAVA 2011/2012
Praktikum 6 Pemrograman Berbasis Obyek
Polymorphism (bag. 2).
Inheritance Konsep Inheritance Protected Member
OOP Java 06 Polymorphism.
Public class RelasiDemo { public static void main(String[] args) { //beberapa nilai int i = 37; int j = 42; int k = 42; System.out.println("Nilai variabel...");
Inheritance Matakuliah : T0984 / Algoritma dan Metode Object Oriented Programming II Pertemuan : 4 Tahun : 2008 Versi : 1/0.
Inheritance, polymorphism, dan interface
Advanced Class Features 2
Class and Object Matakuliah : XXXX / Algoritma dan Metode Object Oriented Programming II Pertemuan : 2 Tahun : 2008 Versi : 1/0.
INHERITANCE (Oleh : Nur Hayatin, S.ST)
Struktur kontrol.
Algoritma &Pemrograman 1
Pertemuan 6 PEWARISAN AND POLYMORPHISM
Praktikum Class Abstract dan Interface
Class & Object Disusun Oleh: Reza Budiawan Untuk:
Access Modifier.
Inheritance dan Kata Kunci static
POLYMORPHISM (KEBANYAKRUPAAN)
Pertemuan 2 SINTAKS BAHASA, TIPE DATA, DAN OPERATOR
Algoritma & Pemrograman 1
@kovazzevic | SMKN 24 Jakarta
Constructor dan Overloading
POLYMORPHISM (KEBANYAKRUPAAN)
Pertemuan 5 Tipe Data, Operator, Tata Bahasa
MODIFIER JAVA.
Pengorganisasian Class
Association, Composition dan Inheritance
MEMBUAT CLASS SENDIRI 2.
Tipe Data, Variabel, dan Operator
Tipe Data, Variabel, dan Operator
Inheritance.
Dasar Bahasa Java.
Pemrograman Berorientasi Object
STUDI KASUS.
Tipe Data, Variabel, dan Operator
Pertemuan 5 Inheritance
Transcript presentasi:

Praktikum Inheritance Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2009

Lat 1. Invoking parent class constructor Employee1.java import java.util.Date; public class Employee1 { private static final double BASE_SALARY = 15000.00; private String name; private double salary; private Date birthDate; public Employee1(String name, double salary, Date DoB) { this.name = name; this.salary = salary; this.birthDate = DoB; } public Employee1(String name, double salary) { this(name, salary, null);

public Employee1(String name, Date DoB) { this(name, BASE_SALARY, DoB); } public Employee1(String name) { this(name, BASE_SALARY); // more Employee code... class Manager extends Employee1 { private String department; public Manager(String name, double salary, String dept) { super(name, salary); department = dept;

public Manager(String n, String dept) { super(n); department = dept; } public Manager(String dept) { Coba kompile program diatas! Jika terjadi error,kenapa dan betulkan!

Lat.2 Membuat SubClass dari Account Anda akan membuat dua buah subclass dari class Account pada sebuah package Banking yaitu : class SavingAccount dan class CheckingAccount. Anda akan melakukan overriding pada method withdraw untuk cek Account dan Menggunakan super untuk mengakses konstruktor parentclass. Implementasikan subclass Account di bawah :

Class Account Modifikasi Class Account Copy kan class Account yang sudah di buat pada tugas minggu ke7 ke package banking Lakukan modifikasi pada class Account -> hak akses atribut balance menjadi protected package banking; public class Account { private double balance; public Account(double bal) { balance = bal; } public double getBalance() { return balance; public boolean deposit(double amount) { balance = balance + amount; return true; public boolean withdraw(double amount) { boolean result = true; if ( balance < amount ) { result = false; } else { balance = balance - amount; Class Account.java

Class Bank dan Class Customer package banking; public class Bank { private static int MAX_CUSTOMERS = 10; private static double SAVINGS_RATE = 3.5; private Customer[] customers; private int numberOfCustomers; public Bank() { customers = new Customer[MAX_CUSTOMERS]; numberOfCustomers = 0; } public void addCustomer(String f, String l) { int i = numberOfCustomers++; customers[i] = new Customer(f, l); public Customer getCustomer(int customer_index) { return customers[customer_index]; public int getNumOfCustomers() { return numberOfCustomers; package banking; public class Customer { // Data Attributes private Account account; private String firstName; private String lastName; public Customer(String f, String l) { firstName = f; lastName = l; } public String getFirstName() { return firstName; public String getLastName() { return lastName; public Account getAccount() { return account; public void setAccount(Account acct) { account = acct;

Modify the Account Class Langkah-langkah: Pada Banking package tambahkan subclass SavingsAccount dan CheckingAccount sebagaimana digambarkan pada UML diagram diatas Buat direktori Banking baru. Copy proyek banking yang sebelumnya ke direktori Banking baru ini. Modify the Account Class Perhatikan bahwa variabel balance bertipe protected (diindikasikan dengan tanda #, pada UML sebelumnya bertanda – yaitu private). Ubah akses mode balance menjadi protected.

The Savings Account Subclass Implementasikan class SavingsAccount sebagaimana UML diagram. Class SavingsAccount merupakan subclass dari class Account. Gunakan kata kunci extends. Terdapat satu buah variabel yaitu interestRate yang bertipe double. Terdapat satu buah public constructor dengan dua parameter: balance dan interest_rate. Constructor ini harus passing parameter balance ke parent constructor dengan menggunakan super(balance) dan mengeset nilai variabel interestRate dengan nilai interest_rate. Catatan: Interest rate = bunga tabungan

The Checking Account Subclass Implementasikan class CheckingAccount sesuai dengan UML diagram. Class CheckingAccount adalah subclass dari class Account. Pada class CheckingAccount harus terdapat variabel overdraftProtection yang bertipe double. Terdapat public constructor dengan dua parameter: balance and protect. Constructor ini harus passing parameter balance ke parent constructor dengan menggunakan super(balance) dan mengeset nilai variabel overdraftProtection dengan nilai protect. Terdapat satu buah public constructor dengan satu parameter yaitu balance. Constructor ini harus passing parameter balance ke lokal constructor dengan menggunakan this. Perhatikan bahwa constructor lain yang ada adalah constructor dengan dua parameter. Maka buat nilai protect default adalah -1.0 yang berarti bahwa pada account tidak terdapat overdraftProtection. Catatan: Saldo = balance + overdraftProtection overdraftProtection = Saldo minimal, yaitu saldo yang diharapkan tidak boleh diambil pada suatu rekening, kecuali bila konsumen ingin menutup rekening.

Class CheckingAccount harus mengoverride method withdraw Class CheckingAccount harus mengoverride method withdraw. Method withdraw harus melakukan cek terhadap saldo (balance) apakah jumlahnya cukup bila terjadi pengambilan sejumlah uang (amount). Cek yang dilakukan adalah sebagai berikut: Jika balance – amount => 0.0 maka proses pengambilan diperbolehkan dan mengembalikan nilai true. Dan selanjutnya set balance = balance – amount; Jika balance – amount < 0.0 maka lakukan cek sebagai berikut: Jika tidak ada overdraftProtection (nilai = -1.0) atau overdraftProtection < overdraftNeeded (amount-balance) maka gagalkan proses pengambilan uang dengan mengembalikan nilai false. Jika terdapat overdraftProtection atau overdraftProtection > overdraftNeeded (amount-balance) maka proses pengambilan uang berhasil dengan mengembalikan nilai true. Dan selanjutnya set balance = 0.0; overdraftProtection = overdraftProtection - overdraftNeeded;

Class TestBanking Creating the customer Jane Smith. Creating her Savings Account with a 500.00 balance and 3% interest. Creating the customer Owen Bryant. Creating his Checking Account with a 500.00 balance and no overdraft protection. Creating the customer Tim Soley. Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection. Creating the customer Maria Soley. Maria shares her Checking Account with her husband Tim. Retrieving the customer Jane Smith with her savings account. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: false Customer [Simms, Jane] has a balance of 324.88 Retrieving the customer Owen Bryant with his checking account with no overdraft protection. Customer [Bryant, Owen] has a balance of 324.88

Class TestBanking Retrieving the customer Tim Soley with his checking account that has overdraft protection. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: true Customer [Soley, Tim] has a balance of 0.0 Retrieving the customer Maria Soley with her joint checking account with husband Tim. Deposit 150.00: true Withdraw 750.00: false Customer [Soley, Maria] has a balance of 150.0

Class TestBanking import banking.*; public class TestBanking { public static void main(String[] args) { Bank bank = new Bank(); Customer customer; Account account; // // Create bank customers and their accounts System.out.println("Creating the customer Jane Smith."); bank.addCustomer("Jane", "Simms"); customer = bank.getCustomer(0); System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest."); customer.setAccount(new SavingsAccount(500.00, 0.03)); System.out.println("Creating the customer Owen Bryant."); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection."); customer.setAccount(new CheckingAccount(500.00));

Class TestBanking System.out.println("Creating the customer Tim Soley."); bank.addCustomer("Tim", "Soley"); customer = bank.getCustomer(2); System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection."); customer.setAccount(new CheckingAccount(500.00, 500.00)); System.out.println("Creating the customer Maria Soley."); bank.addCustomer("Maria", "Soley"); customer = bank.getCustomer(3); System.out.println("Maria shares her Checking Account with her husband Tim."); customer.setAccount(bank.getCustomer(2).getAccount()); System.out.println();

Class TestBanking // Demonstrate behavior of various account types // Test a standard Savings Account System.out.println("Retrieving the customer Jane Smith with her savings account."); customer = bank.getCustomer(0); account = customer.getAccount(); // Perform some account transactions System.out.println("Withdraw 150.00: " + account.withdraw(150.00)); System.out.println("Deposit 22.50: " + account.deposit(22.50)); System.out.println("Withdraw 47.62: " + account.withdraw(47.62)); System.out.println("Withdraw 400.00: " + account.withdraw(400.00)); // Print out the final account balance System.out.println("Customer [" + customer.getLastName() + ", " + customer.getFirstName() + "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account w/o overdraft protection System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection."); customer = bank.getCustomer(1);

Class TestBanking // Test a Checking Account with overdraft protection System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection."); customer = bank.getCustomer(2); account = customer.getAccount(); // Perform some account transactions System.out.println("Withdraw 150.00: " + account.withdraw(150.00)); System.out.println("Deposit 22.50: " + account.deposit(22.50)); System.out.println("Withdraw 47.62: " + account.withdraw(47.62)); System.out.println("Withdraw 400.00: " + account.withdraw(400.00)); // Print out the final account balance System.out.println("Customer [" + customer.getLastName() + ", " + customer.getFirstName() + "] has a balance of " + account.getBalance()); System.out.println(); System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim."); customer = bank.getCustomer(3); System.out.println("Deposit 150.00: " + account.deposit(150.00)); System.out.println("Withdraw 750.00: " + account.withdraw(750.00)); }

Lat 2. Membuat subclass dari Bank Accounts Membuat dua buah subclass dari class Accounts, yaitu SavingsAccount dan CheckingAccount. Kita akan mengoverride method withdraw untuk mengecek accounts dan menggunakan super untuk memanggil parent constructor.

UML diagram