Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 115 Week 4 February 4 - 8, 2008. Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.

Similar presentations


Presentation on theme: "CSE 115 Week 4 February 4 - 8, 2008. Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday."— Presentation transcript:

1 CSE 115 Week 4 February 4 - 8, 2008

2 Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday & 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 visits for Lab 1 must be completed this week. Lab 2 due this week. Office hour visits for Lab 1 must be completed this week.

3 Monday Method definitions Method definitions –Method header visibility returnType methodName (parameterList) visibility returnType methodName (parameterList) –Method body Enclosed between { } Enclosed between { } Code inside gives the steps the method should perform. Code inside gives the steps the method should perform.

4 Monday Method header Method header –No two methods can have the same method header –visibilty: usually public –returnType: the type of thing the method returns, if it does not return anything, the return type is listed as the keyword void

5 Monday Method header Method header –methodName: identifier that programmer picks, as style first letter is lower case, letters in subsequent words upper case –parameterList: additional information that is needed to complete the method – can be empty

6 Monday Parameter List Parameter List –If the parameter list is not empty, it is a comma-separate list of type identifier pairs, where type is the type of the parameter and identifier is the name of the parameter to be used inside the method body

7 Wednesday Announcements Software Installation Fest today 4-7 in Baldy 21. Software Installation Fest today 4-7 in Baldy 21. Exam 3 on Monday 2/11 Exam 3 on Monday 2/11 Exam Review Sessions start next week Tuesday 2/12 and Thursday 2/14 – see Schedule page for more details. Exam Review Sessions start next week Tuesday 2/12 and Thursday 2/14 – see Schedule page for more details. Lab 2 due this week. Office hour visits for Lab 1 must be completed this week. Lab 2 due this week. Office hour visits for Lab 1 must be completed this week.

8 Wednesday Constructors are methods, but the syntax for calling them and writing them is just different because of their specialized purpose. Constructors are methods, but the syntax for calling them and writing them is just different because of their specialized purpose.

9 Wednesday Note that no two methods can have the exact same header. However, two methods can have the same name if they differ in number and/or type of their parameters. This is called method overloading. Note that no two methods can have the exact same header. However, two methods can have the same name if they differ in number and/or type of their parameters. This is called method overloading.

10 Wednesday Local variables only exist inside a method. That is to say that their scope is only within the method body. It turns out that their lifetime is also only when the method is being executed. Local variables only exist inside a method. That is to say that their scope is only within the method body. It turns out that their lifetime is also only when the method is being executed. What if we wanted to use the same variable in multiple methods? What if we wanted to use the same variable in multiple methods?

11 Wednesday Instance variables Instance variables –Allow us to declare a variable to belong to the entire class. Its scope is within the class body and its lifetime is linked to the lifetime of the class –Syntax for variable declaration: private type identifier;

12 Wednesday private is a keyword meaning that the variable is only accessible within the class private is a keyword meaning that the variable is only accessible within the class type is the type of the instance variable type is the type of the instance variable

13 Wednesday identifier is the name given the variable by the programmer, our instance variables will start with an underscore and then follow the same rules for local variables identifier is the name given the variable by the programmer, our instance variables will start with an underscore and then follow the same rules for local variables

14 Wednesday So, the variable can be declared, but how can we set its value – remember it starts out null. So, the variable can be declared, but how can we set its value – remember it starts out null. We have two options: We have two options: –Create an instance for it in the constructor –Pass in a value for it to the constructor

15 Friday Announcements Exam 3 on Monday 2/11 Exam 3 on Monday 2/11 Exam Review Sessions start next week Tuesday 2/12 and Thursday 2/14 – see Schedule page for more details. Will be reviewing Exam 1 & 2. Exam Review Sessions start next week Tuesday 2/12 and Thursday 2/14 – see Schedule page for more details. Will be reviewing Exam 1 & 2. Lab 2 due this week. Office hour visits for Lab 1 must be completed this week. Lab 2 due this week. Office hour visits for Lab 1 must be completed this week.

16 Friday If we are trying to assign the value to an instance variable declaration, we saw that we had two options: If we are trying to assign the value to an instance variable declaration, we saw that we had two options: –Create an instance for it in the constructor –Pass in a value for it in the parameters of the constructor

17 Friday If we create an instance for it in the constructor then we are constructing a relationship whereby the object is responsible for creating the “subpart”. This is the relationship of composition. If we create an instance for it in the constructor then we are constructing a relationship whereby the object is responsible for creating the “subpart”. This is the relationship of composition.

18 Friday In the composition relationship, the lifetime of the subpart is connected to the lifetime of the whole. The part does not exist before the whole, and will not exist after the whole is destroyed. In the composition relationship, the lifetime of the subpart is connected to the lifetime of the whole. The part does not exist before the whole, and will not exist after the whole is destroyed.

19 Friday If we pass in a value to the parameter of the constructor, we are creating an association relationship. This relationship’s informal name is “knows a”. If we pass in a value to the parameter of the constructor, we are creating an association relationship. This relationship’s informal name is “knows a”.

20 Friday In the association relationship, the lifetime of the value referred to by the instance variable is not related to the lifetime of the object that holds the reference. The two objects merely need to communicate with one another in order to get some work done. In the association relationship, the lifetime of the value referred to by the instance variable is not related to the lifetime of the object that holds the reference. The two objects merely need to communicate with one another in order to get some work done.

21 Friday There are two ways to code the association relationship. See book for both alternatives. There are two ways to code the association relationship. See book for both alternatives.


Download ppt "CSE 115 Week 4 February 4 - 8, 2008. Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday."

Similar presentations


Ads by Google