Lecture3MoreDesigningClasses

- More Design Method Parameters In Java a method can never change parameters of primitive type Modifying a numeric parameter has no effect on

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
More Design
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Method Parameters In Java, a method can never change parameters of primitive type. Modifying a numeric parameter has no effect on caller’s argument
Background image of page 2
Example void trickyDeposit (double amount) { amount = 2 * amount; balance = balance + amount; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example double theAmount = 500; BankAccount michael = new BankAccount(0); michael.trickyDeposit (theAmount); System.out.println (theAmount);
Background image of page 4
Call by Value and Call by Reference In Java, method parameter values are copied into the parameter variables when a method starts This mechanism is called “call by value”
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Object Parameters Objects are not passed as parameters; object references are copied by value . A method can change the state of an object reference parameter, but it cannot replace the object reference with another.
Background image of page 6
Example public void transfer (double amount, BankAccount other) { balance = balance – amount; double newBalance = other.getBalance( ) + amount; other = new BankAccount ( newBalance); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example BankAccount suresh = new BankAccount (50); BankAccount alex = new BankAccount (100); suresh.transfer (25, alex); System.out.println ( alex.getBalance());
Background image of page 8
Guideline Do not change the contents of parameter variables
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Preconditions and Postconditions Precondition: A precondition is a requirement that the caller of a method must meet. Example: /** * Deposits money into account * @param amount the amount of money to * deposit * (Precondition: amount >= 0) */
Background image of page 10
Preconditions What should a method do when a condition is not met? 1. Check for violation and throw an exception. 2. Work under the assumption that preconditions are fulfilled.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example public void deposit (double amount) { if (amount < 0) throw new IllegalArgumentException( ); balance = balance + amount; }
Background image of page 12
A post condition is a promise that the method’s job has been done correctly. Two kinds
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/13/2008 for the course CSC 216 taught by Professor Miller during the Spring '08 term at N.C. State.

Page1 / 48

- More Design Method Parameters In Java a method can never change parameters of primitive type Modifying a numeric parameter has no effect on

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online