View
216
Download
2
Embed Size (px)
Citation preview
Monday Monday AnnouncementsAnnouncements Software installation fest Tuesday Software installation fest Tuesday
& Wednesday 4-7 in Baldy 21.& Wednesday 4-7 in Baldy 21. Pick up Exam 1’s in lab this week.Pick up Exam 1’s in lab this week. Lab 2 due this week. Office hour Lab 2 due this week. Office hour
visits for Lab 1 must be visits for Lab 1 must be completed this week.completed this week.
MondayMonday
Method definitionsMethod definitions– Method headerMethod header
visibility returnType methodName (parameterList)visibility returnType methodName (parameterList)
– Method bodyMethod body Enclosed between { }Enclosed between { } Code inside gives the steps the method Code inside gives the steps the method
should perform.should perform.
MondayMonday
Method headerMethod header– No two methods can have the same No two methods can have the same
method headermethod header– visibilty: usually publicvisibilty: usually public– returnType: the type of thing the returnType: the type of thing the
method returns, if it does not return method returns, if it does not return anything, the return type is listed as anything, the return type is listed as the keyword voidthe keyword void
MondayMonday
Method headerMethod header– methodName: identifier that methodName: identifier that
programmer picks, as style first letter programmer picks, as style first letter is lower case, letters in subsequent is lower case, letters in subsequent words upper casewords upper case
– parameterList: additional information parameterList: additional information that is needed to complete the method that is needed to complete the method – can be empty– can be empty
MondayMonday
Parameter ListParameter List– If the parameter list is not empty, it is If the parameter list is not empty, it is
a comma-separate list of a comma-separate list of typetype identifieridentifier pairs, where pairs, where type is the type is the type of the parametertype of the parameter and and identifier is identifier is the name of the parameter to be used the name of the parameter to be used inside the method bodyinside the method body
Wednesday Wednesday AnnouncementsAnnouncements Software Installation Fest today 4-7 in Software Installation Fest today 4-7 in
Baldy 21.Baldy 21. Exam 3 on Monday 2/11Exam 3 on Monday 2/11 Exam Review Sessions start next week Exam Review Sessions start next week
Tuesday 2/12 and Thursday 2/14 – see Tuesday 2/12 and Thursday 2/14 – see Schedule page for more details.Schedule page for more details.
Lab 2 due this week. Office hour visits Lab 2 due this week. Office hour visits for Lab 1 must be completed this for Lab 1 must be completed this week.week.
WednesdayWednesday
Constructors are methods, but Constructors are methods, but the syntax for calling them and the syntax for calling them and writing them is just different writing them is just different because of their specialized because of their specialized purpose.purpose.
WednesdayWednesday
Note that no two methods can Note that no two methods can have the exact same header. have the exact same header. However, two methods can have However, two methods can have the same name if they differ in the same name if they differ in number and/or type of their number and/or type of their parameters. This is called parameters. This is called method overloading.method overloading.
WednesdayWednesday
Local variables only exist inside a Local variables only exist inside a method. That is to say that their method. That is to say that their scope is only within the method scope is only within the method body. It turns out that their body. It turns out that their lifetime is also only when the lifetime is also only when the method is being executed.method is being executed.
What if we wanted to use the same What if we wanted to use the same variable in multiple methods?variable in multiple methods?
WednesdayWednesday
Instance variablesInstance variables– Allow us to declare a variable to Allow us to declare a variable to
belong to the entire class. Its scope belong to the entire class. Its scope is within the class body and its is within the class body and its lifetime is linked to the lifetime of lifetime is linked to the lifetime of the classthe class
– Syntax for variable declaration:Syntax for variable declaration:
privateprivate typetype identifieridentifier;;
WednesdayWednesday
private is a keyword meaning that private is a keyword meaning that the variable is only accessible the variable is only accessible within the classwithin the class
type is the type of the instance type is the type of the instance variablevariable
WednesdayWednesday
identifier is the name given the identifier is the name given the variable by the programmer, our variable by the programmer, our instance variables will start with instance variables will start with an underscore and then follow the an underscore and then follow the same rules for local variablessame rules for local variables
WednesdayWednesday
So, the variable can be declared, So, the variable can be declared, but how can we set its value – but how can we set its value – remember it starts out null.remember it starts out null.
We have two options:We have two options:– Create an instance for it in the Create an instance for it in the
constructorconstructor– Pass in a value for it to the Pass in a value for it to the
constructorconstructor
Friday AnnouncementsFriday Announcements
Exam 3 on Monday 2/11Exam 3 on Monday 2/11 Exam Review Sessions start next Exam Review Sessions start next
week Tuesday 2/12 and Thursday week Tuesday 2/12 and Thursday 2/14 – see Schedule page for 2/14 – see Schedule page for more details. Will be reviewing more details. Will be reviewing Exam 1 & 2.Exam 1 & 2.
Lab 2 due this week. Office hour Lab 2 due this week. Office hour visits for Lab 1 must be visits for Lab 1 must be completed this week.completed this week.
FridayFriday
If we are trying to assign the If we are trying to assign the value to an instance variable value to an instance variable declaration, we saw that we had declaration, we saw that we had two options:two options:– Create an instance for it in the Create an instance for it in the
constructorconstructor– Pass in a value for it in the Pass in a value for it in the
parameters of the constructorparameters of the constructor
FridayFriday
If we create an instance for it in If we create an instance for it in the constructor then we are the constructor then we are constructing a relationship constructing a relationship whereby the object is responsible whereby the object is responsible for creating the “subpart”. This is for creating the “subpart”. This is the relationship of composition.the relationship of composition.
FridayFriday
In the composition relationship, In the composition relationship, the lifetime of the subpart is the lifetime of the subpart is connected to the lifetime of the connected to the lifetime of the whole. The part does not exist whole. The part does not exist before the whole, and will not before the whole, and will not exist after the whole is destroyed.exist after the whole is destroyed.
FridayFriday
If we pass in a value to the If we pass in a value to the parameter of the constructor, we parameter of the constructor, we are creating an association are creating an association relationship. This relationship’s relationship. This relationship’s informal name is “knows a”.informal name is “knows a”.
FridayFriday
In the association relationship, In the association relationship, the lifetime of the value referred the lifetime of the value referred to by the instance variable is not to by the instance variable is not related to the lifetime of the related to the lifetime of the object that holds the reference. object that holds the reference. The two objects merely need to The two objects merely need to communicate with one another in communicate with one another in order to get some work done.order to get some work done.