Advanced Objects (Dependencies and Documentation)
description
Transcript of Advanced Objects (Dependencies and Documentation)
![Page 1: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/1.jpg)
Advanced Objects(Dependencies and Documentation)
![Page 2: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/2.jpg)
Java Class Concept
Encapsulation◦ Blackbox concept
Data and method(s)Hidden details
Interface Effect(s)
methods called
class
![Page 3: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/3.jpg)
Relationship Symbol Line Style
Arrow Tip
Inheritance-is-a
Solid Triangle
Interface Implementation
Dotted Triangle
Aggregation-has-a
Solid Diamond
Dependency
Dotted Open
UML Relationship Symbols
Aggregation is a stronger form of dependency-see page 467
![Page 4: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/4.jpg)
![Page 5: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/5.jpg)
• Place multiplicity notations near the ends of an association/• dependency.
Multiplicity (Cardinality)
![Page 6: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/6.jpg)
GJY
1..*
![Page 7: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/7.jpg)
has-a relationship
Establishes has-a relationship/association between classes BankAccount One can navigate from one class to another instantiation in Java – (note NOT inheritance)
![Page 8: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/8.jpg)
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;}
}
package bankaccount;public class Bank {
public static void main(String[] args) { BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000.00); harrysChecking.withdraw(500.00); System.out.println(harrysChecking.getBalance()); }}
Review BankAccount
![Page 9: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/9.jpg)
Lab: Part 1
1. Add the interest method to your BankAccount Class2. Add an account with your name3. Call interest for Harry and pass 5% or .054. Call the interest method for your account and pass the parameter as .085. Revise VISIO drawing accordingly
![Page 10: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/10.jpg)
Chapter 8, pp 304-307 pre & post conditions Section 8.5
![Page 11: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/11.jpg)
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
![Page 12: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/12.jpg)
• Objective:
Tells other programmers how to use the method!
• precondition documentation• postcondition documentation
![Page 13: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/13.jpg)
Precondition: describes the state of relevant identifiers prior to 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.
![Page 14: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/14.jpg)
Do NOT:1) Repeat code description! (code is hidden and user/programmer on needs to
know what to expect and what must be supplies)
2) Describe how code works3) Do not use attribute names unless meaningful
![Page 15: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/15.jpg)
• Precondition Publish preconditions so the caller won't call methods with
bad parameters
Preconditions
![Page 16: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/16.jpg)
• Method should let user know if condition is violated.
• Caution: Text might imply otherwise
![Page 17: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/17.jpg)
• 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
Postconditions
![Page 18: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/18.jpg)
/** * 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;
}
![Page 19: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/19.jpg)
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
![Page 20: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/20.jpg)
Chapter 10
![Page 21: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/21.jpg)
Inheritance
Inheritance : derive a new class from an existing one
Parent class, or superclass, or base class.
Thechild class or subclass.
Component hierarchy:
![Page 22: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/22.jpg)
Inheritance (generalization)
is-a relationship between superclass and a subclass or base class
![Page 23: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/23.jpg)
Inheritance
Deposit
BankForm parent
Is-a component of
child
![Page 24: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/24.jpg)
Business
KMart Macys
ServiceBusiness
Kinkos
RetailBusiness
is-a
![Page 25: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/25.jpg)
Deriving SubclassesJAVA Example 1: class RetailBusiness extends Business
Example 2:class CheckingAccount extends BankAccount{
<class contents>}
![Page 26: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/26.jpg)
![Page 27: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/27.jpg)
SavingsAccount object inherits the balance instance field from BankAccount, and gains one additional instance field: interestRate:
Layout of a Subclass Object
![Page 28: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/28.jpg)
![Page 29: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/29.jpg)
public class BankAccount{ private double balance;/** * PostCondition: A bank account with a zero balance.*/ public BankAccount() { balance = 0; } /** @param initialBalance the initial balance PostCondition: A bank account with a given balance is constructed. */ public BankAccount(double initialBalance) { balance = initialBalance; }
Continued Next Page
![Page 30: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/30.jpg)
/** * 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
![Page 31: Advanced Objects (Dependencies and Documentation)](https://reader036.fdocuments.in/reader036/viewer/2022062323/56816777550346895ddc7276/html5/thumbnails/31.jpg)
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); } }
Savings Account