UW-Madison CS 302 - Chapter 9 – Designing Classes

Unformatted text preview:

Chapter 9 – Designing ClassesChapter Goals9.3 Immutable ClassesSlide 49.4 Side EffectsOther Side EffectsHow to minimize Side Effects?Pass By ValueWon’t work!Slide 10Neither will thisYou would need to do this9.5 PreconditionsPreconditionsHandling ViolationsAnother optionCommon errorPostconditionsSlide 19Slide 20ContractStatic Fields (Class Fields)Static Methods (Class Methods)Field Modifier choicesMethod Modifier choicesWhy make fields static?Why make methods static?Slide 28Slide 29Slide 30Slide 31Static FieldsSlide 33Scope Of Local VariablesSlide 35Slide 36Slide 37Slide 38Slide 39Slide 40PackagesSlide 42Default9.1 Choosing ClassesGood classesClass = conceptGood ClassesBad ClassesPractice Question class DieUML diagram for class DieFigure from Adv Topic 17.117.2 Discovering ClassesSimple ruleKey pointsBehaviorCRC CardSlide 579.2 CohesionCohesionSlide 60SolutionCouplingSlide 63Slide 64Slide 6517.3 Relationships Between ClassesDependencyAggregationSlide 6917.1 Software Life Cycle5 phasesSlide 72Slide 73Slide 74Slide 75Slide 76Perfect WorldWaterfall ModelProblems with Waterfall ModelSpiral ModelSpiralExtreme ProgrammingPracticesCont.Chapter 9 – Designing Chapter 9 – Designing ClassesClassesChapter GoalsChapter GoalsLearn to identify side effectsLearn to identify side effectsKnow when to use method preconditions Know when to use method preconditions and postconditions and postconditions Go in depth a bit more on static (aka class) Go in depth a bit more on static (aka class) methods and fieldsmethods and fieldsDesign programs the object-oriented wayDesign programs the object-oriented way9.3 Immutable Classes9.3 Immutable ClassesRecall that accessor methods only retrieve Recall that accessor methods only retrieve informationinformationThey do not change the state of the objectThey do not change the state of the objectCalling them multiple times in a row will yield Calling them multiple times in a row will yield same resultssame resultsImmutable Classes are classes that only Immutable Classes are classes that only have accessor methodshave accessor methodsExample: String ClassExample: String ClassAdvantagesAdvantagesDo not have to worry about the dangers of Do not have to worry about the dangers of handing off references in methods because handing off references in methods because there are no methods that can modify them.there are no methods that can modify them.DisadvantagesDisadvantagesYour objects are fixed once they are created.Your objects are fixed once they are created.9.4 Side Effects9.4 Side EffectsMutators are designed to change the internal data Mutators are designed to change the internal data of the implicit parameterof the implicit parameterSide effect of a method: modifying externally Side effect of a method: modifying externally observable data (explicit parameters or reference observable data (explicit parameters or reference types)types)public void transfer(double amount, BankAccount other)public void transfer(double amount, BankAccount other){{balance = balance - amount;balance = balance - amount;other.balance = other.balance + amount; other.balance = other.balance + amount; // Modifies explicit parameter// Modifies explicit parameter}} Best to avoid if possible (otherwise, document the Best to avoid if possible (otherwise, document the effect thoroughly)effect thoroughly)Other Side EffectsOther Side EffectsWhy don’t we add a printBalance() method to Why don’t we add a printBalance() method to BankAccount?BankAccount?public void printBalance()public void printBalance(){{ System.out.println("The balance is now $" System.out.println("The balance is now $" + balance); + balance);} } Assumes the printing should be done to the screenAssumes the printing should be done to the screenAssumes the printing should be in EnglishAssumes the printing should be in EnglishMakes this class dependent on the System class and Makes this class dependent on the System class and PrintStream classPrintStream classYou want to localize the input and output of your You want to localize the input and output of your program to as few places as possibleprogram to as few places as possibleHow to minimize Side Effects?How to minimize Side Effects?Never modify explicit parameters to a Never modify explicit parameters to a methodmethodTreat them as constantsTreat them as constantspublic void deposit(double amount)public void deposit(double amount){{amount = amount + balance;amount = amount + balance;}}Pass By ValuePass By ValueTwo ways to pass parametersTwo ways to pass parametersPass by reference – the memory location is Pass by reference – the memory location is sent, meaning that the data can be changedsent, meaning that the data can be changedPass by value – a copy of the memory Pass by value – a copy of the memory location is createdlocation is createdJava only uses pass by valueJava only uses pass by valueEven objects are passed by value, since the Even objects are passed by value, since the reference is copiedreference is copiedWon’t work!Won’t work!public class BankAccount {public class BankAccount {public void transferTo (BankAccount other, public void transferTo (BankAccount other, double amount)double amount){{balance = balance – amount;balance = balance – amount;double newBalance = other.balance + amount;double newBalance = other.balance + amount;other = new BankAccount(newBalance);other = new BankAccount(newBalance);}}}}Neither will thisNeither will thisclass Qclass Q::public void addResponse(String question) {public void addResponse(String question) {String response = “I accept”;String response = “I accept”;question = new String(question + response);question = new String(question + response);}}class Rclass R::public void invite() {public void invite() {Q friend = new Q();Q friend = new Q();String invite = “Want to go to the dance?”;String invite = “Want to go to the dance?”;int inviteLength = invite.length();int inviteLength = invite.length();do {do {friend.addResponse(invite);friend.addResponse(invite);} while(invite.length() == inviteLength);} while(invite.length() == inviteLength);}}You would need to do thisYou would need to do thisclass Qclass Q::public public StringString addResponse(String question) { addResponse(String question) {String response = “I accept”;String response = “I accept”;returnreturn


View Full Document

UW-Madison CS 302 - Chapter 9 – Designing Classes

Download Chapter 9 – Designing Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 9 – Designing Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 9 – Designing Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?