1 / 31

Structured Problem Solving

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 )

fay
Download Presentation

Structured Problem Solving

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213 blakews@hope.ac.uk

  2. 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)

  3. 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)

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

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

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

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

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

  9. BlueJ Screen

  10. Geeting the BlueJ Projects • You will find the BlueJ projects on the course website… • … seminar • Download and unzip it

  11. BlueJ Environment Start BlueJ, Open Project named hopeshapes

  12. BlueJ Environment Each brown box represents a class Start BlueJ, Open Project named hopeshapes

  13. 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()

  14. 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()

  15. Creating Objects using the new message • The new message helps to construct a Triangle software object triangle1

  16. Creating a Triangle object • A red Triangle software object triangle1 appears at the bottom left corner

  17. Creating many objects • More Red objects appear. • How many objects of each class? 17

  18. Creating many objects • Send them messages to see their behaviour

  19. Simple messages to object triangle1 • Send a message makeVisible() to triangle1 to see it BY RIGHT – CLICKING on the triangle1 object

  20. 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;

  21. Message signature Sending Messages with parameters • To move the greentriangle1 object 50 pixels to the right , we use moveHorizontal (50)

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

  23. Sending message = Method call • To change colour of the triangle1 object to blue, we call changeColor (“blue”) • Notice the quotes “” for a String parameter

  24. The Inspect menu item Displaying the state of an object • Pick an object in the Menu of objects, Right-click and choose Inspect

  25. Current state of triangle1 object

  26. Is instance of int diameter int xPosition int yPosition String color Boolean isVisible Different states of the same object Is instance of

  27. Drawing Pictures By creating many objects, we can draw many shapes - A house with chimney !

  28. Editing the source code Open the hopepicture project

  29. Opening the editor with Picture class selected to see the class source code Editing source code

  30. Source Code – explore the code!

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

More Related