DOC PREVIEW
UW-Madison CS 302 - Chapter 3 - Implementing Classes

This preview shows page 1-2-3-4-29-30-31-32-59-60-61-62 out of 62 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 3 – Implementing ClassesChapter GoalsBlack BoxLevels of abstraction: Software DesignSlide 53.2 Designing the Public Interface to a ClassDesigning #1: MethodsDesigning: Method DefinitionsSlide 9Syntax 3.1: Method DefinitionDesigning #2: ConstructorConstructorsConstructor vs. MethodSyntax 3.2 : Constructor DefinitionBankAccount Public InterfaceSlide 16Remember3.3 Commenting the Public InterfaceJavadoc CommentsJavadoc commentsSlide 21Class Comment3.4 Instance FieldsSlide 24Slide 25Access Specifiers Syntax 3.4 : Instance Field DeclarationAccessing Instance FieldsSlide 29Slide 303.5 Implementing Constructors & MethodsConstructor Call ExampleWhy put instructions in a method?Implementing MethodsMethod Call ExampleSyntax 3.5: The return StatementSlide 37Slide 38Slide 39Wu’s TemplateSlide 413.6 Testing Classes3.6 Testing A ClassSlide 44Slide 453.7 Categories of VariablesInstance FieldLocal VariablesLifetime Of VariablesSlide 503.8 Implicit and Explicit Method ParametersSlide 52Slide 53Implicit Parameters and thisSlide 55Example – without thisExample – with thisHOW TO 3.1 – Designing and Implementing a ClassHOW-TO 3.1 (cont)Slide 60Note on Style #1Note on Style #2Chapter 3 – Chapter 3 – Implementing ClassesImplementing ClassesChapter GoalsChapter GoalsTo become familiar with the process of To become familiar with the process of implementing classes implementing classes To be able to implement simple methods To be able to implement simple methods To understand the purpose and use of To understand the purpose and use of constructors constructors To understand how to access instance To understand how to access instance fields and local variables fields and local variables To appreciate the importance of To appreciate the importance of documentation comments documentation commentsBlack BoxBlack BoxA black box is A black box is something that something that performs a task, but performs a task, but hides how it does ithides how it does itThis is also called This is also called encapsulationencapsulationBlack boxes in a car: Black boxes in a car: transmission, electronic transmission, electronic control module, etc control module, etcLevels of Levels of abstraction: abstraction: Software Software DesignDesignOld times: computer programs only Old times: computer programs only manipulated primitive types such as manipulated primitive types such as numbers and characters numbers and characters Gradually programs became more Gradually programs became more complex, vastly increasing the amount of complex, vastly increasing the amount of detail a programmer had to remember and detail a programmer had to remember and maintainmaintain3.2 Designing the Public Interface 3.2 Designing the Public Interface to a Classto a ClassYour task: Develop a Your task: Develop a BankAccountBankAccount class classFirst step: Define essential featuresFirst step: Define essential featuresBehavior of bank account (abstraction):Behavior of bank account (abstraction):deposit money deposit money withdraw money withdraw money get balance get balanceDesigning #1: MethodsDesigning #1: MethodsMethods of BankAccount class:Methods of BankAccount class:deposit deposit withdraw withdraw getBalance getBalance Support method calls such as the following:Support method calls such as the following:harrysChecking.deposit(2000);harrysChecking.deposit(2000);harrysChecking.withdraw(500);harrysChecking.withdraw(500);System.out.println(harrysChecking.getBalance());System.out.println(harrysChecking.getBalance()); Which methods are accessors? Mutators?Which methods are accessors? Mutators?Designing: Method DefinitionsDesigning: Method DefinitionsCOMPONENTS:COMPONENTS:access specifieraccess specifier (Ex. public) (Ex. public) return typereturn type (Ex. String or void) (Ex. String or void) method name (Ex. deposit) method name (Ex. deposit) list of list of parametersparameters (Ex. amount for deposit) (Ex. amount for deposit) method method bodybody in braces {…} in braces {…}Examples: Examples: public void deposit(double amount) {...} public void deposit(double amount) {...} public void withdraw(double amount) {...} public void withdraw(double amount) {...} public double getBalance() {...} public double getBalance() {...}Syntax 3.1: Method DefinitionSyntax 3.1: Method DefinitionaccessSpecifier returnType accessSpecifier returnType methodNamemethodName((parameterType parameterNameparameterType parameterName,...),...){{method bodymethod body } } Example:Example: public void deposit(double amount){public void deposit(double amount){. . .. . .}}Purpose:Purpose:To define the behavior of a method To define the behavior of a methodDesigning #2: ConstructorDesigning #2: ConstructorA constructor initializes the instance A constructor initializes the instance variables variables Constructor name = class nameConstructor name = class namepublic BankAccount()public BankAccount(){{// body--filled in later// body--filled in later } }ConstructorsConstructorsConstructor body is executed when new Constructor body is executed when new object is created object is created Statements in constructor body will set the Statements in constructor body will set the internal data of the object that is being internal data of the object that is being constructedconstructed How does the compile know which How does the compile know which constructor to call?constructor to call?Constructor vs. MethodConstructor vs. MethodConstructors are a specialization of Constructors are a specialization of methodsmethodsGoal: to set the internal data of the objectGoal: to set the internal data of the object2 differences2 differencesAll constructors are named after the classAll constructors are named after the classTherefore all constructors of a class have the Therefore all constructors of a class have the same namesame nameNo return type listed EVER!No return type listed EVER!Syntax 3.2 : Constructor Syntax 3.2 : Constructor DefinitionDefinitionaccessSpecifier ClassNameaccessSpecifier ClassName((parameterType parameterType parameterNameparameterName, . . .), . . .){{constructor bodyconstructor body }}Example:Example:public BankAccount(double initialBalance)public BankAccount(double initialBalance){ { . . . . . . }}Purpose:Purpose:To define the behavior of a constructor To define the behavior of a constructorBankAccountBankAccount Public Interface Public InterfaceThe public constructors and methods of a The public constructors


View Full Document

UW-Madison CS 302 - Chapter 3 - Implementing Classes

Download Chapter 3 - Implementing 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 3 - Implementing 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 3 - Implementing 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?