Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Praktikum Inheritance

Presentasi serupa


Presentasi berjudul: "Praktikum Inheritance"— Transcript presentasi:

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

2 Lat 1. Invoking parent class constructor
Employee1.java import java.util.Date; public class Employee1 { private static final double BASE_SALARY = ; 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);

3 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;

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

5 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 :

6 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

7 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;

8 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.

9 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

10 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.

11 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;

12 Class TestBanking Creating the customer Jane Smith. Creating her Savings Account with a balance and 3% interest. Creating the customer Owen Bryant. Creating his Checking Account with a balance and no overdraft protection. Creating the customer Tim Soley. Creating his Checking Account with a balance and 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 : true Deposit 22.50: true Withdraw 47.62: true Withdraw : false Customer [Simms, Jane] has a balance of Retrieving the customer Owen Bryant with his checking account with no overdraft protection. Customer [Bryant, Owen] has a balance of

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

14 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 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 balance and no overdraft protection."); customer.setAccount(new CheckingAccount(500.00));

15 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 balance and in overdraft protection."); customer.setAccount(new CheckingAccount(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();

16 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 : " + 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 : " + 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);

17 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 : " + 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 : " + 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 : " + account.deposit(150.00)); System.out.println("Withdraw : " + account.withdraw(750.00)); }

18 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.

19 UML diagram


Download ppt "Praktikum Inheritance"

Presentasi serupa


Iklan oleh Google