1 / 31

Advanced Objects (Dependencies and Documentation)

Advanced Objects (Dependencies and Documentation). Java Class Concept . Encapsulation Blackbox concept. methods called. Data and method(s) Hidden details. Interface. Effect(s). class. UML Relationship Symbols. Aggregation is a stronger form of dependency-see page 467.

saxon
Download Presentation

Advanced Objects (Dependencies and Documentation)

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. Advanced Objects(Dependencies and Documentation)

  2. Java Class Concept Encapsulation • Blackbox concept methods called Data and method(s) Hidden details Interface Effect(s) class

  3. UML Relationship Symbols • Aggregation is a stronger form of dependency-see page 467

  4. Multiplicity (Cardinality) • Place multiplicity notations near the ends of an association/ • dependency.

  5. GJY 1..*

  6. has-a relationship • Establishes has-a relationship/association between classes • BankAccount • One can navigate from one class to another • instantiation in Java – (note NOTinheritance)

  7. package bankaccount; • public class Bank { • public static void main(String[] args) • { • BankAccountharrysChecking = new BankAccount(); • harrysChecking.deposit(2000.00); • harrysChecking.withdraw(500.00); • System.out.println(harrysChecking.getBalance()); • } • } • package bankaccount; • public class BankAccount • { • private double balance; • public BankAccount () { • balance=0; } • public void deposit (double amount) { • balance=balance+amount; } • public void withdraw (double amount){ • balance=balance - amount; } • public double getBalance () { • return balance; • } • } Review BankAccount

  8. Lab: Part 1 Add the interestmethod to your BankAccount Class 2. Add an accountwithyour name 3. Call interest for Harry and pass 5% or .05 4. Call the interestmethod for your account and pass the parameter as .08 5. Revise VISIO drawingaccordingly

  9. Chapter 8, pp 304-307 • pre & post conditions • Section 8.5

  10. Documentation Comments • Documentation included in form of comments • Prior to a class • Prior to a method • Begins with “/**” and ends with “*/” • Internal Method comments discouraged

  11. Documentation • Objective: • Tells other programmers how to use the method! • precondition documentation • postcondition documentation

  12. Documentation • Precondition: describes the state of relevant identifiers • priorto the function's execution. • Includes a reference to what variables are • passed to the function. • Postcondition: describes the state after the function is • executed: • the state of attributes after execution • the return value if any.

  13. Documentation • Do NOT: • Repeat code description! • (code is hidden and user/programmer on needs to know what to expect and what must be supplies) • Describe how code works • Do not use attribute names unless meaningful

  14. Preconditions • Precondition • Publish preconditions so the caller won't call methods with bad parameters

  15. Method should let user know if condition is violated. • Caution: Text might imply otherwise

  16. Postconditions • Condition that is true after a method has completed • The return value is computed correctly • The object is in a certain state after the method call is completed • Don't document trivial postconditions that repeat the @return clause

  17. /** • * precondition: Balance is available for an account • * @param amount >=0 • * postcondition:The account balance is adjusted to reflect • * the deposit. • */ • public void deposit (double amount) • { • System.out.println(amount); • balance = balance + amount; • }

  18. Lab: Part 2 • Enhance the BankAccount Class • Add preconditions for all methods • Add parameters to documentation as • necessary • Add postconditions where applicable • Post Complete labs to DropBoxes • located in September 19th ANGEL • Lesson Tab

  19. Inheritance • Chapter 10

  20. Inheritance • Inheritance : derive a new class from an existing one • Parent class,or superclass, or base class. • Thechildclass or subclass. • Component hierarchy:

  21. Inheritance (generalization) • is-a relationship between superclassand • a subclass or base class

  22. Inheritance Deposit child Is-a component of parent BankForm

  23. Business ServiceBusiness RetailBusiness KMart Macys Kinkos is-a

  24. Deriving Subclasses JAVA Example 1: class RetailBusinessextends Business Example 2: class CheckingAccount extends BankAccount { <class contents> }

  25. Layout of a Subclass Object SavingsAccount object inherits the balance instance field from BankAccount, and gains one additional instance field: interestRate:

  26. public class BankAccount { private double balance; /** * PostCondition: A bank account with a zero balance. */ public BankAccount() { balance = 0; } /** @paraminitialBalance the initial balance PostCondition: A bank account with a given balance is constructed. */ public BankAccount(double initialBalance) { balance = initialBalance; } Continued Next Page

  27. /** * PreCondition: a balance exists @param amount the amount to deposit PostCondition: Money is deposited into the bank account. */ public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } /** * Precondition: A balance exists @param amount the amount to withdraw PostCondition: Money is withdrawn from the bank account. */ public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; } /** PostCondition: Current balance of the bank account exists. @return the current balance */ public double getBalance() { return balance; }} BankAccount Class Continued

  28. Savings Account package infsy535bankacctwithinheritance; public class SavingsAccount extends BankAccount{ private double interestRate; /** @param rate the interest rate PostCondition: A bank account with a given interest rate. */ public SavingsAccount(double rate) { interestRate = rate; } /** PostCondition: Added the earned interest to the account balance. */ public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } }

More Related