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

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 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 11 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 11 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 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Chapter 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 comments Black 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, etc Levels 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 BankAccountBankAccountclassclassFirst 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 balance2Designing #1: MethodsDesigning #1: MethodsMethods of Methods of BankAccountBankAccountclass:class:deposit deposit withdraw withdraw getBalancegetBalanceSupport 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.getBalanceSystem.out.println(harrysChecking.getBalance());());Which methods are Which methods are accessorsaccessors? ? MutatorsMutators??Designing: Method DefinitionsDesigning: Method DefinitionsCOMPONENTS:COMPONENTS:access access specifierspecifier(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 bodybodyin braces {in braces {……} } Examples: Examples: public void public void deposit(doubledeposit(doubleamount) {...} amount) {...} public void public void withdraw(doublewithdraw(doubleamount) {...} amount) {...} public double public double getBalancegetBalance() {...} () {...} Syntax 3.1: Method DefinitionSyntax 3.1: Method DefinitionaccessSpecifieraccessSpecifierreturnTypereturnTypemethodNamemethodName((parameterTypeparameterTypeparameterNameparameterName,...),...){{method bodymethod body} } Example:Example:public void public void deposit(doubledeposit(doubleamount){amount){. . .. . .}}Purpose:Purpose:To define the behavior of a method To define the behavior of a method Designing #2: ConstructorDesigning #2: ConstructorA constructor initializes the instance A constructor initializes the instance variables variables Constructor name = class nameConstructor name = class namepublic public BankAccountBankAccount()(){{// body// body----filled in laterfilled 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 constructedconstructedHow does the compile know which How does the compile know which constructor to call?constructor to call?3Constructor 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 DefinitionDefinitionaccessSpecifieraccessSpecifierClassNameClassName((parameterTypeparameterTypeparameterNameparameterName, . . .), . . .){{constructor bodyconstructor body}}Example:Example:public public BankAccount(doubleBankAccount(doubleinitialBalanceinitialBalance)){ { . . . . . . }}Purpose:Purpose:To define the behavior of a constructor To define the behavior of a constructor BankAccountBankAccountPublic InterfacePublic InterfaceThe public constructors and methods of a The public constructors and methods of a class form the class form the public interfacepublic interfaceof the class.of the class.public class public class BankAccountBankAccount{{// private fields// private fields----filled in later filled in later // Constructors// Constructorspublic public BankAccountBankAccount()(){{// body// body----filled in later filled in later }}public public BankAccount(doubleBankAccount(doubleinitialBalanceinitialBalance) { ) { // body// body----filled in laterfilled in later} } // Methods // Methods public void public void deposit(doubledeposit(doubleamount) {amount) {// body// body----filled in laterfilled in later}}public void public void withdraw(doublewithdraw(doubleamount) { amount) { // body// body----filled in laterfilled in later} } public double public double getBalancegetBalance() { () { // body// body----filled in filled in later later }}} } RememberRememberPublic methods and constructors provide Public methods and constructors provide the the public interfacepublic interfaceto a classto a classThey are how you interact with the black boxThey are how you interact with the black boxOur class is simple, but we can do many Our class is simple, but we can do many


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?