310 likes | 440 Views
Structured Problem Solving. Object Oriented Concepts 2 Stewart Blakeway FML 213 blakews@hope.ac.uk. 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 )
E N D
Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213 blakews@hope.ac.uk
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)
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)
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
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
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
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
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) • Creating objects, sending messages and Object states in BlueJ environment • Chapter 9 up to 9.7 in Data Structures to Java booklet
Geeting the BlueJ Projects • You will find the BlueJ projects on the course website… • … seminar • Download and unzip it
BlueJ Environment Start BlueJ, Open Project named hopeshapes
BlueJ Environment Each brown box represents a class Start BlueJ, Open Project named hopeshapes
BlueJ Environment Start BlueJ, Open Project named hopeshapes Make an object of a class by right clicking on its brown box and selecting menu item new <Classname>() e.g. new Triangle()
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 Triangleobject by right-clicking on the Triangle rectangle and select: • new Triangle()
Creating Objects using the new message • The new message helps to construct a Triangle software object triangle1
Creating a Triangle object • A red Triangle software object triangle1 appears at the bottom left corner
Creating many objects • More Red objects appear. • How many objects of each class? 17
Creating many objects • Send them messages to see their behaviour
Simple messages to object triangle1 • Send a message makeVisible() to triangle1 to see it BY RIGHT – CLICKING on the triangle1 object
Display Window • The triangle1 object appears with default state values set in the constructor message Triangle() • height = 30; • width = 40; • xPosition = 50; • yPosition = 15; • color = "green"; • isVisible = true;
Message signature Sending Messages with parameters • To move the greentriangle1 object 50 pixels to the right , we use moveHorizontal (50)
Sending Messages with parameters • To move the greentriangle1 object 50 pixels to the right , we use moveHorizontal (50) Integer Value 50 Parameter, integer Void return Method name
Sending message = Method call • To change colour of the triangle1 object to blue, we call changeColor (“blue”) • Notice the quotes “” for a String parameter
The Inspect menu item Displaying the state of an object • Pick an object in the Menu of objects, Right-click and choose Inspect
Is instance of int diameter int xPosition int yPosition String color Boolean isVisible Different states of the same object Is instance of
Drawing Pictures By creating many objects, we can draw many shapes - A house with chimney !
Editing the source code Open the hopepicture project
Opening the editor with Picture class selected to see the class source code Editing source code
Things to remember • Go to BlueJ web site: • 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