Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213 blakews@hope.ac.uk

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 1 Defined objects and classes Objects are instances of classes Objects encapsulate (or contain) – State (data or attribute values) – A protocol (a set of functions, messages, methods etc ) (also known as an interface) 2

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 1 Objects send and receive messages in the form : (Object “DOT” message) – Object. message (with/without parameters) – textBlock1.setFont(“Courier New”); Using a message and its signature: – Return type (Answer type) – Message name – Parameters (List of data items sent with the message) 3

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 1 Identifying attributes (state of the Object) and protocols Representing objects using object diagrams. Sending messages to an object in Java 4

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Quizlet What is the only way we can change the state of an object? Sending it a message What is the collection of messages that an object can understand called? Protocol 5

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Quizlet int getAge() What is the message name? getAge What type of thing is returned by the message? int How many items in the parameter list? None 6

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Quizlet Triangle1.moveHorizontal(60) What is the name of the object receiving the message? Triangle1 What is the name of the message? moveHorizontal What parameter does the message have? 60 7

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today Introduce Java as an Object oriented language Installing and running BlueJ Ver 2.5.3 on a PC (http://www.bluej.org)http://www.bluej.org Creating objects, sending messages and Object states in BlueJ environment Chapter 9 up to 9.7 in Data Structures to Java booklet 8

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE BlueJ Screen 9

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Geeting the BlueJ Projects You will find the BlueJ projects on the course website… … seminar Download and unzip it 10

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE BlueJ Environment 11 Start BlueJ, Open Project named hopeshapes

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE BlueJ Environment 12 Start BlueJ, Open Project named hopeshapes Each brown box represents a class

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE BlueJ Environment 13 Start BlueJ, Open Project named hopeshapes Make an object of a class by right clicking on its brown box and selecting menu item new () e.g. new Triangle()

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating Objects Every brown rectangle symbol represents a class Compile by right-clicking on a class and then selecting pop-up menu item Compile Create a Triangle object by right-clicking on the Triangle rectangle and select: – new Triangle() 14

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating Objects using the new message 15 The new message helps to construct a Triangle software object triangle1

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating a Triangle object 16 A red Triangle software object triangle1 appears at the bottom left corner

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating many objects 17 More Red objects appear. How many objects of each class?

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating many objects 18 Send them messages to see their behaviour

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Simple messages to object triangle1 Send a message makeVisible() to triangle1 to see it BY RIGHT – CLICKING on the triangle1 object 19

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Display Window The triangle1 object appears with default state values set in the constructor message Triangle() 20 height = 30; width = 40; xPosition = 50; yPosition = 15; color = "green"; isVisible = true;

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sending Messages with parameters To move the green triangle1 object 50 pixels to the right, we use moveHorizontal (50) 21 Message signature

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sending Messages with parameters To move the green triangle1 object 50 pixels to the right, we use moveHorizontal (50) 22 Void return Parameter, integer Integer Value 50 Method name

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sending message = Method call To change colour of the triangle1 object to blue, we call changeColor (“blue”) Notice the quotes “” for a String parameter 23

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Displaying the state of an object Pick an object in the Menu of objects, Right- click and choose Inspect 24 The Inspect menu item

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Current state of triangle1 object 25

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Different states of the same object 26 Circle int diameter int xPosition int yPosition String color Boolean isVisible Is instance of

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Drawing Pictures 27 By creating many objects, we can draw many shapes - A house with chimney !

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Editing the source code 28 Open the hopepicture project

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Editing source code 29 Opening the editor with Picture class selected to see the class source code

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Source Code – explore the code! 30

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Things to remember Go to BlueJ web site: – http://www.bluej.org http://www.bluej.org – Click on download at left – Get and install the Java by clicking on JDK 6 on the right – Get and install BlueJ by clicking on bluejsetup-254.exe 31


Download ppt "Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213"

Similar presentations


Ads by Google