cse 115 week 3 january 28 – february 1, 2008. monday announcements software installation fest: 2/5...
Post on 21-Dec-2015
216 views
TRANSCRIPT
Monday Monday AnnouncementsAnnouncements Software Installation Fest: 2/5 and Software Installation Fest: 2/5 and
2/6 4pm – 7pm in Baldy 212/6 4pm – 7pm in Baldy 21– Bring your laptop or desktop to have Bring your laptop or desktop to have
appropriate software installed!appropriate software installed!
Monday Game PlanMonday Game Plan
Exam 1Exam 1 Class Definitions continuedClass Definitions continued
MondayMonday
Inside the class body where we Inside the class body where we would define the capabilities, we would define the capabilities, we have the definition of a special have the definition of a special type of capability, the constructortype of capability, the constructor
Constructor helps us set up the Constructor helps us set up the initial state of the object.initial state of the object.
MondayMonday
Constructor is made up of:Constructor is made up of:– Constructor headerConstructor header– Constructor bodyConstructor body
MondayMonday
Constructor header:Constructor header: publicpublic identifieridentifier ()()
– public means the same here as public means the same here as beforebefore
– identifier is the name of the identifier is the name of the constructor, which must be the constructor, which must be the same as the name of the classsame as the name of the class
– () called parameter list() called parameter list
MondayMonday
Constructor body is enclosed by { Constructor body is enclosed by { and }and }
Contains code that would help us Contains code that would help us set up the initial state of the set up the initial state of the object.object.
MondayMonday
Note that the () in the constructor Note that the () in the constructor definition are a parameter list.definition are a parameter list.
We see these when we were We see these when we were creating an object as wellcreating an object as well
That’s because what we are That’s because what we are actually doing when we create an actually doing when we create an object is allocating space for the object is allocating space for the object object (new)(new) and calling the and calling the constructorconstructor
MondayMonday
The () in the call to the The () in the call to the constructor are called the constructor are called the argument list. The contents of argument list. The contents of the two sets of () will have to the two sets of () will have to match. We’ll talk more about this match. We’ll talk more about this later.later.
MondayMonday
We will begin to build We will begin to build relationships between our relationships between our classes.classes.
When we do, we will see that When we do, we will see that relationships have formal names, relationships have formal names, informal names, ways to be informal names, ways to be expressed in UML and ways to be expressed in UML and ways to be expressed in (Java) codeexpressed in (Java) code
MondayMonday
Our first relationship will be the Our first relationship will be the instantiation dependency instantiation dependency relationship, or “uses a” relationship, or “uses a” informally.informally.
UML indicator – see GreenUML indicator – see Green
Wednesday Wednesday AnnouncementsAnnouncements Software Installation Fest: 2/5 and Software Installation Fest: 2/5 and
2/6 4pm – 7pm in Baldy 212/6 4pm – 7pm in Baldy 21– Bring your laptop or desktop to have Bring your laptop or desktop to have
appropriate software installed!appropriate software installed!
Wednesday Game PlanWednesday Game Plan
Instantiation Dependency Instantiation Dependency ContinuedContinued
WednesdayWednesday
If we need to refer to an object again If we need to refer to an object again once we’ve created it, we need to once we’ve created it, we need to give it a name.give it a name.
Variables in programming allow us to Variables in programming allow us to name objects and refer to them later.name objects and refer to them later.
For example, this will allow us to add For example, this will allow us to add multiple ants to the same terrarium.multiple ants to the same terrarium.
WednesdayWednesday
To use a variable, we must declare it.To use a variable, we must declare it. To declare a variable we use the To declare a variable we use the
following syntaxfollowing syntaxtypetype identifieridentifier;;
type is the type of thing the variable type is the type of thing the variable refers to.refers to.
identifier is the name the identifier is the name the programmer is giving to the variable.programmer is giving to the variable.
WednesdayWednesday
Where does the declaration go?Where does the declaration go? Where it goes makes a difference Where it goes makes a difference
in how it can be used in our in how it can be used in our program.program.
For now, we will put the variable For now, we will put the variable declaration inside the constructor declaration inside the constructor body.body.
WednesdayWednesday
This makes the variable a local This makes the variable a local variable, meaning that the variable, meaning that the variable is local to the constructor variable is local to the constructor and only exists within the scope and only exists within the scope of the constructor’s body.of the constructor’s body.
There are other places to put There are other places to put variables in our code and we will variables in our code and we will learn about those shortly.learn about those shortly.
WednesdayWednesday
When a variable is declared, it is When a variable is declared, it is a null reference. We need to give a null reference. We need to give it a value in order for it to be it a value in order for it to be useful.useful.
To assign a value to a variable, To assign a value to a variable, we use the assignment operator, we use the assignment operator, which is the equals sign (=).which is the equals sign (=).
WednesdayWednesday
Assignment syntax:Assignment syntax:
identifieridentifier = = expressionexpression;; identifier is the name that the identifier is the name that the
programmer has given the variableprogrammer has given the variable expression is an expression whose expression is an expression whose
value is assigned to the variable named value is assigned to the variable named by identifierby identifier
Note that assignment is from the right Note that assignment is from the right to the left.to the left.
WednesdayWednesday
Knowing about local variables Knowing about local variables allows us to create a variant of allows us to create a variant of the dependency relationship the dependency relationship called Local Variable called Local Variable Dependency. This can still be Dependency. This can still be informally called the “uses a” informally called the “uses a” relationship, but the code and the relationship, but the code and the UML are slightly different.UML are slightly different.
Friday AnnouncementsFriday Announcements
Software Installation Fest: 2/5 and Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 212/6 4pm – 7pm in Baldy 21– Bring your laptop or desktop to have Bring your laptop or desktop to have
appropriate software installed!appropriate software installed!
Friday Game PlanFriday Game Plan
What’s going on inside the What’s going on inside the machine when we create machine when we create variables.variables.
FridayFriday
Today we looked at how the Today we looked at how the computer allocates space for the computer allocates space for the variables we create and how the variables we create and how the use of the assignment operator use of the assignment operator can actually cause us to lose a can actually cause us to lose a reference to an object we reference to an object we previously created.previously created.