{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture3MoreDesigningClasses

Lecture3MoreDesigningClasses - More Design Method...

Info icon This preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
More Design
Image of page 1

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

View Full Document Right 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
Image of page 2
Example void trickyDeposit (double amount) { amount = 2 * amount; balance = balance + amount; }
Image of page 3

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

View Full Document Right Arrow Icon
Example double theAmount = 500; BankAccount michael = new BankAccount(0); michael.trickyDeposit (theAmount); System.out.println (theAmount);
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”
Image of page 5

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

View Full Document Right 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.
Image of page 6
Example public void transfer (double amount, BankAccount other) { balance = balance – amount; double newBalance = other.getBalance( ) + amount; other = new BankAccount ( newBalance); }
Image of page 7

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

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

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

View Full Document Right 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) */
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.
Image of page 11

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

View Full Document Right Arrow Icon
Example public void deposit (double amount) { if (amount < 0) throw new IllegalArgumentException( ); balance = balance + amount; }
Image of page 12
Post conditions A post condition is a promise that the method’s job has been done correctly. Two kinds 1. That the return value is computed correctly 2. That the object is in a certain state after the method call is completed
Image of page 13

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

View Full Document Right Arrow Icon
Example /** * Deposits money into this account.
Image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern