Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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

2 Fundamentals of Software Development 1Slide 2 What can you do with names? 1. 1.Declare the type of the name JButton buttonUserPressed; char middleInitial; 2. 2.Give (assign) a value to the thing to which the name refers buttonUserPressed = new JButton(“press me”); yourMiddleInitial = ‘C’; 3. 3.Refer to the thing to which the name refers, in an expression buttonUserPressed.isEnabled() numberOfTimesVisitedAlaska < 5 System.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

3 Fundamentals of Software Development 1Slide 3 Giving a name a type 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?

4 Fundamentals of Software Development 1Slide 4 Given:Given: JButton buttonUserPressed; int weight; char middleInitial; double temperature; boolean isPressed; Assign values with the = operator:Assign values with the = operator: buttonUserPressed = new JButton(“press me”); middleInitial = josephinesMiddleInitial; weight = 560; temperature = 98.627; middleInitial = ‘W’; isPressed = true; Assigning values to names Read the = operator as “gets” or “gets the value”. It is NOT a test for equality; we use == for that.

5 Fundamentals of Software Development 1Slide 5 Definition = Declaration + Assignment Declare names by type-of-thing name-of-thing:Declare names by type-of-thing name-of-thing: – ; –char firstLetterOfMyName; – ; –Dog myPet; Assign values with = [read it as "gets"]:Assign values with = [read it as "gets"]: – = 'C'; –firstLetterOfMyName = 'C'; –myPet = new ( " Labrador ", " Larken ", 11); –myPet = new Dog( " Labrador ", " Larken ", 11); Define names by combining these:Define names by combining these: – = 'C'; –char firstLetterOfMyName = 'C'; – = new ( " Labrador ", " Larken ", 11); –Dog myPet = new Dog( " Labrador ", " Larken ", 11); Questions so far?

6 Fundamentals of Software Development 1Slide 6 Refer to things in expressions inside statements if (buttonUserPressed.isEnabled()) { userCanStartGame = true; } numberOfTimesVisitedAlaska = numberOfTimesVisitedAlaska + 1; Much more on expressions and statements in the next several sessions

7 Fundamentals of Software Development 1Slide 7 Object-type Names An object-type name (aka reference, label) is just a label that can be stuck onto (an appropriately- typed) objectAn object-type name (aka reference, label) is just a label that can be stuck onto (an appropriately- typed) object Objects are always given object-type names.Objects are always given object-type names. –myPet = new Dog( " Labrador ", " Larken ", 11); –familyDog = myPet; –uglyDog = null; myPet familyDog uglyDog?

8 Fundamentals of Software Development 1Slide 8 Object-type versus primitive-type An object-type name is just a label stuck onto an objectAn object-type name is just a label stuck onto an object –Objects are always given label names Dog familyDog = myPet; A primitive-type name is like a dial: it has exactly one value.A primitive-type name is like a dial: it has exactly one value. –Primitive types (int, double, char, boolean and a few others) always have primitive-type names associated with them int count = 9; char grade; boolean amISleepy = true; myPet familyDog Questions on Things, Types and Names?

9 Fundamentals of Software Development 1Slide 9 Things, Types and Names: Exercise Find a partner who is sitting near youFind a partner who is sitting near you Each of you: do the Angel quizzes (per next items on the schedule)Each of you: do the Angel quizzes (per next items on the schedule) –Quiz – Things, types and names –Quiz – Assignment Work as partners:Work as partners: –Both partners: work together, problem by problem If you disagree on an answer or are unsure, ask an assistantIf you disagree on an answer or are unsure, ask an assistant –Both partners: write your own answers You’ll finish the exercise as homework and turn in your own set of answers individuallyYou’ll finish the exercise as homework and turn in your own set of answers individually –Don’t hesitate to ask questions!


Download ppt "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."

Similar presentations


Ads by Google