1 / 52

Implementing Classes

Implementing Classes. Learning objectives. By the end of this lecture you should be able to:. design classes using the notation of the Unified Modeling Language (UML) ; write the Java code for a specified class;

helene
Download Presentation

Implementing Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementing Classes Learning objectives By the end of this lecture you should be able to: • design classes using the notation of the Unified Modeling Language (UML); • write the Java code for a specified class; • explain the difference between public and private access to attributes and methods; • explain the use of the static keyword; • pass objects as parameters; • implement collection classes based on arrays.

  2. Implementing classes in Java A class consists of: a set of attributes(the data); a set of methods (double) setLength setHeight getLength getHeight calculateArea calculatePerimeter Oblong length height : double (double) : double () : double () : double () : double Oblong () : double (double, double)

  3. The notation of the Unified Modeling Language (UML) Oblong length : double height : double Oblong(double, double) setLength(double) setHeight(double) getLength() : double getHeight() : double calculateArea() : double calculatePerimeter() : double

  4. public class Oblong { double length; double height; private private Oblong(double lengthIn, double heightIn) { } // more methods here } public length = lengthIn; height = heightIn;

  5. getLength() { } public double return length; getHeight() { } public double return height;

  6. setLength( ) { } public void double lengthIn length = lengthIn; setHeight( ) { } public void double heightIn height = heightIn;

  7. calculateArea() { } public double return length * height; calculatePerimeter() { } public double return 2 * (length + height);

  8. The BankAccount class BankAccount accountNumber : String accountName : String balance : double BankAccount (String, String) getAccountNumber() : String getAccountName() : String getBalance() : double deposit(double) withdraw(double)

  9. public class BankAccount { private String accountNumber; private String accountName; private double balance; public BankAccount(String numberIn, String nameIn) { accountNumber = numberIn; accountName = nameIn; balance = 0; }

  10. public String getAccountName() { return accountName; } public String getAccountNumber() { return accountNumber; } public double getBalance() { return balance; }

  11. public void deposit(double amountIn) { balance = balance + amountIn; } public void withdraw(double amountIn) { balance = balance – amountIn; } }

  12. public class BankAccountTester { public static void main(String[ ] args) { BankAccount account1 = new BankAccount("99786754","Susan Richards"); account1.deposit(1000); System.out.println("Account number: " + account1.getAccountNumber()); System.out.println("Account name: " + account1.getAccountName()); System.out.println("Current balance: " + account1.getBalance()); } }

  13. acc1 acc2 acc3 Amending the BankAccount class acc1.getBalance(); acc1.getInterestRate(); “0012765” “Funmi Odulopo” £1200.49 1.25% acc2.addInterest (); acc2.getInterestRate(); “09887254” “Mary Stephenson” £975.12 acc3.deposit( 500 ); acc3.setInterestRate(1.5); 1.25% Bank.setInterestRate(1.4); Bank.getInterestRate(); “07721009” “Dilraj Mann” £3975.75 1.25%

  14. The static keyword private static double interestRate; public static void setInterestRate(double rateIn) { interestRate = rateIn; } public static double getInterestRate() { return interestRate; }

  15. The addInterest method public void addInterest() { balance = balance + (balance * interestRate)/100; }

  16. public class BankAccountTester2 { public static void main(String[] args) { BankAccount2 account1 = new BankAccount2("99786754","Varinder Singh"); BankAccount2 account2 = new BankAccount2("99887776","Lenny Roberts"); account1.deposit(1000); account2.deposit(2000); BankAccount2.setInterestRate(10); account1.addInterest();

  17. System.out.println("Account number: " + account1.getAccountNumber()); System.out.println("Account name: " + account1.getAccountName()); System.out.println("Interest Rate " + BankAccount2.getInterestRate()); System.out.println("Current balance: " + account1.getBalance()); System.out.println(); System.out.println("Account number: " + account2.getAccountNumber()); System.out.println("Account name: " + account2.getAccountName()); System.out.println("Interest Rate " + BankAccount2.getInterestRate()); System.out.println("Current balance: " + account2.getBalance()); } }

  18. Account number: 99786754 Account name: Varinder Singh Interest rate: 10.0 Current balance: 1100.0 Account number: 99887776 Account name: Lenny Roberts Interest rate: 10.0 Current balance: 2000.0

  19. int Objects double char boolean Initializing attributes Java does not give an initial value to local variables but does initialize attributes; 0 false null private static double interestRate = 0;

  20. import java.util.*; public class EasyScanner { public static int nextInt() { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); return i; } // more methods here }

  21. public static double nextDouble() { Scanner sc = new Scanner(System.in); double d = sc.nextDouble(); return d; } public static String nextString() { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); return s; }

  22. public static char nextChar() { Scanner sc = new Scanner(System.in); char c = sc.next().charAt(0); return c; }

  23. public class ParameterTest { public static void main(String[] args) { BankAccount acc = new BankAccount("1", "Samsun Okoyo"); test(acc); System.out.println("Account Number: " + acc.getAccountNumber()); System.out.println("Account Name: " + acc.getAccountName()); System.out.println("Balance: " + acc.getBalance()); } private static void test(BankAccount accIn) { accIn.deposit(2500); } }

  24. Account Number: 1 Account Name: Samsun Okoyo Balance: 2500.0

  25. acc a BankAccount object accIn Effect on computer memory Computer Memory Java Instructions public static void main (String[] args) { BankAccount acc = new BankAccount(….) ; } test(acc); private static void test(BankAccount accIn) { } accIn.deposit(2500);

  26. Bank BankAccount * Collection classes When one object itself consists of other objects, this relationship is called aggregation; A collection class is an implementation of the aggregation relationship.

  27. Bank list: BankAccount [ ] total : int Bank(int) search(String) : int getTotal() : int isEmpty() : boolean isFull() : boolean add(BankAccount) : boolean getItem(String) : BankAccount depositMoney(String, double) : boolean withdrawMoney(String, double) : boolean remove(String) : boolean

  28. public class Bank { private BankAccount[] list; private int total; public Bank(int sizeIn) { list = new BankAccount[sizeIn]; total = 0; }

  29. private int search(String accountNumberIn) { for(int i = 0; i ; i++) { } return -999; } < total BankAccount tempAccount = list[i]; String tempNumber = tempAccount.getAccountNumber(); if(tempNumber.equals(accountNumberIn)) { return i; }

  30. public int getTotal() { return total; } public boolean isEmpty() { if (total == 0) { return true; } else { return false; } }

  31. public boolean isFull() { if (total == list.length) { return true; } else { return false; } }

  32. public boolean add(BankAccount accountIn) { if( ) { return true; } else { return false; } } (!isFull() list[total] = accountIn; total++;

  33. public BankAccount getItem(String accountNumberIn) { int index; index = search(accountNumberIn); if(index == -999) { return null; } else { return list[index]; } }

  34. public boolean depositMoney (String accountNumberIn, double amountIn) { int index = search(accountNumberIn); if(index == -999) { return false; } else { list[index].deposit(amountIn); return true; } }

  35. public boolean remove(String numberIn) { int index = search(numberIn); if(index == -999) { return false; } else { // remove item from list return true; } }

  36. Smith 5th item Smith Adams 4th item list[index] = list[index+1]; for(int i = index; i<= total-2; i++) { } Adams list[index+1] = list[index+2]; 3rd item Patel list[i] = list[i+1]; Item to delete list[index+2] = list[index+3]; Patel Okoya 2nd item total--; Ling 1st item list

  37. public class BankProgram { public static void main(String[] args) { char choice; int size; System.out.print("Maximum number of accounts? "); size = EasyScanner.nextInt(); Bank myBank = new Bank(size);

  38. do { System.out.println(); System.out.println("1. Create new account"); System.out.println("2. Remove an account"); System.out.println("3. Deposit money"); System.out.println("4. Withdraw money"); System.out.println("5. Check account details"); System.out.println("6. Quit"); System.out.println(); System.out.print("Enter choice [1-6]: "); choice = EasyScanner.nextChar(); System.out.println();

  39. switch (choice) { case '1': option1(myBank); break; case '2': option2(myBank); break; case '3': option3(myBank); break; case '4': option4(myBank); break; case '5': option5(myBank); break; case '6': break; default: System.out.println("Invalid entry"); } }while (choice != '6'); }

  40. // add account private static void option1(Bank bankIn) { System.out.print("Enter account number: "); String number = EasyScanner.nextString(); System.out.print("Enter account name: "); BankAccount account = new BankAccount(number, name); boolean ok = bankIn.add(account); if (!ok) { System.out.println("The list is full"); } else { System.out.println("Account created"); } }

  41. // remove account private static void option2(Bank bankIn) { System.out.print("Enter account number: "); String number = EasyScanner.nextString(); boolean ok = bankIn.remove(number); if (!ok) { System.out.println("No such account number"); } else { System.out.println("Account removed"); } }

  42. // deposit money private static void option3(Bank bankIn) { System.out.print("Enter account number: "); String number = EasyScanner.nextString(); System.out.print("Enter amount to deposit: "); double amount = EasyScanner.nextDouble(); boolean ok = bankIn.depositMoney(number, amount); if (!ok) { System.out.println("No such account number"); } else { System.out.println("Money deposited"); } }

  43. // withdraw money from an account private static void option4(Bank bankIn) { System.out.print("Enter account number: "); String number = EasyScanner.nextString(); System.out.print("Enter amount to withdraw: "); double amount = EasyScanner.nextDouble(); boolean ok = bankIn.withdrawMoney(number, amount); if (!ok) { System.out.println("No such account number"); } else { System.out.println("Money withdrawn"); } }

  44. // check account details private static void option5(Bank bankIn) { System.out.print("Enter account number "); String number = EasyScanner.nextString(); BankAccount account = bankIn.getItem(number); if (account == null) { System.out.println("No such account number"); } else { System.out.println("Account number: " + account.getAccountNumber()); System.out.println("Account name: " + account.getAccountName()); System.out.println("Balance: " + account.getBalance()); System.out.println(); } }

  45. Maximum number of accounts? 100 1. Create new account 2. Remove an account 3. Deposit money 4. Withdraw money 5. Check account details 6. Quit Enter choice [1-6]: 1

  46. Enter account number: 63488965 Enter account name: Paula Wilkins Account created 1. Create new account 2. Remove an account 3. Deposit money 4. Withdraw money 5. Check account details 6. Quit Enter choice [1-6]: 1

  47. Enter account number: 14322508 Enter account name: Sydney Isaacs Account created 1. Create new account 2. Remove an account 3. Deposit money 4. Withdraw money 5. Check account details 6. Quit Enter choice [1-6]: 1

  48. Enter account number: 90871435 Enter account name: Delroy Joseph Account created 1. Create new account 2. Remove an account 3. Deposit money 4. Withdraw money 5. Check account details 6. Quit Enter choice [1-6]: 3

  49. Enter account number: 90871435 Enter amount to deposit: 1500 Money deposited 1. Create new account 2. Remove an account 3. Deposit money 4. Withdraw money 5. Check account details 6. Quit Enter choice [1-6]: 2

  50. Enter account number: 14322508 Account removed 1. Create new account 2. Remove an account 3. Deposit money 4. Withdraw money 5. Check account details 6. Quit Enter choice [1-6]: 5

More Related