fundamentals of software development 1slide 1 objects and names there are two types of things in...

9
Fundamentals of Softw are Development 1 Slide 1 Objects and Names Objects and Names There are two types of things in Java: Objects the button that the user just pressed the URL of your home page Primitive-type things The integer that represents the number of times you have visited Alaska The character that represents your middle initial We use names (also called variables) to refer to objects buttonUserPressed homeURL mobyDick and to primitive-type things: numberOfTimesVisitedAlaska middleInitial

Upload: laurel-richard

Post on 23-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 1

Objects and NamesObjects and Names

• There are two types of things in Java:– Objects

• the button that the user just pressed• the URL of your home page

– Primitive-type things• The integer that represents the number of times you have

visited Alaska• The character that represents your middle initial

• We use names (also called variables) to refer to objects

buttonUserPressed homeURL mobyDick

and to primitive-type things:numberOfTimesVisitedAlaskamiddleInitial

Page 2: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 2

What can you do with What can you do with names?names?

1. Declare the type of the nameJButton buttonUserPressed;char middleInitial;

2. Give (assign) a value to the thing to which the name refers

buttonUserPressed = new JButton(“press me”);yourMiddleInitial = ‘C’;

3. Refer to the thing to which the name refers, in an expression

buttonUserPressed.isEnabled()

numberOfTimesVisitedAlaska < 5System.out.println(yourMiddleInitial)

These dot notation references are where the power of an OOP language lies!

The next few slides contain details of each of these ideas

Page 3: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 3

Giving a name a Giving a name a typetype

• Java is a strongly typed language– This means that every name has a type

• Declare names as follows:JButton buttonUserPressed;Book mobyDick;

int weight;double temperature;char middleInitial;boolean isPressed;

The

type name

pattern appears often in Java

Can you guess WHY Java (and many other languages) choose to be strongly typed?

These are the most common of the 8 primitive types. The others are: long, short, float, and byte. What do you notice about the names of all 8 primitive types?

Page 4: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 4

• Given:Given:JButton buttonUserPressed;int weight;char middleInitial;double temperature;boolean isPressed;

• Assign valuesAssign values with the with the == operator: operator:buttonUserPressed = new JButton(“press me”);middleInitial = josephinesMiddleInitial;

weight = 560;temperature = 98.627;middleInitial = ‘W’;isPressed = true;

AssigningAssigning values to values to namesnames

Read the = operator as “gets” or “gets the value”. It is NOT a test for equality; we use == for that.

Page 5: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 5

Definition = Declaration + Definition = Declaration + AssignmentAssignment

• DeclareDeclare names by names by type-of-thingtype-of-thing name-name-of-thingof-thing::– char firstLetterOfMyName;;– Dog myPet;;

• AssignAssign values values with = [read it as " with = [read it as "getsgets"]:"]:– firstLetterOfMyName = = ''CC'';;– myPetmyPet = = newnew Dog((""LabradorLabrador"", , ""LarkenLarken"", 11);, 11);

• DefineDefine names by combining these: names by combining these:– char firstLetterOfMyName = = ''CC'';;– Dog myPet = = newnew Dog((""LabradorLabrador"", , ""LarkenLarken"", 11);, 11);

Questions so far?

Page 6: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 6

ReferRefer to things in to things in expressions expressions insideinside

statementsstatementsif (buttonUserPressed.isEnabled()) {

userCanStartGame = true;

}

numberOfTimesVisitedAlaska =

numberOfTimesVisitedAlaska + 1;

Much more on expressions and statements in the next several sessions

Page 7: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 7

Object-type NamesObject-type Names

• An An object-type nameobject-type name (aka (aka referencereference, , labellabel) is just a ) is just a labellabel that can be stuck that can be stuck onto (an appropriately-typed) objectonto (an appropriately-typed) object

• Objects are always given object-type names.Objects are always given object-type names.– myPetmyPet = = newnew DogDog((""LabradorLabrador"", , ""LarkenLarken"", 11);, 11);– familyDogfamilyDog = = myPetmyPet;;– uglyDoguglyDog = = nullnull;; myPet

familyDog

uglyDog ?

Page 8: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 8

Object-type versus Object-type versus primitive-typeprimitive-type

• An An object-type nameobject-type name is justis justa label stuck onto an objecta label stuck onto an object– Objects are always given label namesObjects are always given label names

DogDog familyDogfamilyDog = = myPetmyPet;;

• A A primitive-type nameprimitive-type name is like a dial: is like a dial: it has it has exactly one valueexactly one value..– Primitive typesPrimitive types ( (intint, , doubledouble, , charchar, , booleanboolean

and a few others) always have primitive-type and a few others) always have primitive-type names associated with themnames associated with themintint countcount = 9; = 9; charchar gradegrade;;booleanboolean amISleepyamISleepy = = truetrue;;

myPet

familyDog

Questions on Things, Types and Names?

Page 9: Fundamentals of Software Development 1Slide 1 Objects and Names There are two types of things in Java: – –Objects the button that the user just pressed

Fundamentals of Software Development 1

Slide 9

Things, Types and Names: Things, Types and Names: ExerciseExercise• Find a partner who is sitting near youFind a partner who is sitting near you

• Each of you: do the Angel quizzes (per Each of you: do the Angel quizzes (per next items on the schedule)next items on the schedule)– Quiz – Things, types and namesQuiz – Things, types and names– Quiz – AssignmentQuiz – Assignment

• Work as partners:Work as partners:– Both partners: work together, problem by Both partners: work together, problem by

problemproblem• If you disagree on an answer or are unsure, ask an If you disagree on an answer or are unsure, ask an

assistantassistant

– Both partners: write your own answersBoth partners: write your own answers• You’ll finish the exercise as homework and turn in You’ll finish the exercise as homework and turn in

your own set of answers individuallyyour own set of answers individually

– Don’t hesitate to ask questions!Don’t hesitate to ask questions!