Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.

Similar presentations


Presentation on theme: "Object Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW."— Presentation transcript:

1 Object Interaction and Source Code Week 2

2 OBJECT ORIENTATION BASICS REVIEW

3 Object – Represents an actual thing! Class – ‘Blueprint’ for making an object Method – Things an object can do (behaviour) Parameter – input required by an object method Data type – what type of data is allowed Object Model Basics FUNDAMENTAL CONCEPTS

4 Many objects (instances) can be created from a single class. An object has attributes (fields), and those attributes have values assigned to them. The class defines what fields an object has, but each object stores its own set of values (state). Each class has source code (Java) associated with it that defines its fields and methods. Object Model Basics OTHER OBSERVATIONS

5 OBJECT ORIENTATION OBJECT INTERACTION

6 Create the outside wall: 1.wall = new Square(); 2.wall.moveHorizontal(-140); 3.wall.moveVertical(20); 4.wall.changeSize(120); 5.wall.makeVisible(); How To Draw A House A SEQUENCE OF METHOD CALLS

7 Create the window: 1.window = new Square(); 2.window.changeColor(“black”); 3.window.moveHorizontal(-120); 4.window.moveVertical(40); 5.window.changeSize(40); 6.window.makeVisible(); How To Draw A House A SEQUENCE OF METHOD CALLS

8 Create the roof: 1.roof = new Triangle(); 2.roof.changeSize(60, 180); 3.roof.moveHorizontal(20); 4.roof.moveVertical(-60); 5.roof.makeVisible(); How To Draw A House A SEQUENCE OF METHOD CALLS

9 Create the sun: 1.sun = new Circle(); 2.sun.changeColor(“yellow”); 3.sun.moveHorizontal(100); 4.sun.moveVertical(-40); 5.sun.changeSize(80); 6.sun.makeVisible(); How To Draw A House A SEQUENCE OF METHOD CALLS

10 How To Draw A House A SEQUENCE OF METHOD CALLS Final result after a sequence of 22 method calls

11 A house picture is composed of four different shape objects – representing a wall, window, roof and sun. The sequence of 22 method calls used to create and manipulate these objects will always be the same. It would be convenient to record the 22 method calls and replay them whenever we wished to draw a picture of the house. Object Interaction OBSERVATIONS ON DRAWING THE HOUSE

12 BlueJ Demonstration A First Look at the Picture class

13 /** * This class represents a simple picture. * You can draw the picture using the draw method… */ public class Picture { private Square wall; private Square window; private Triangle roof; private Circle sun; Java Source Code Picture.java

14 /** * Constructor for objects of class Picture */ public Picture() { // nothing to do... instance variables are automatically set to null } Java Source Code Picture.java A constructor is a ‘special method’ that is called when the object is created and is used for object initialisation. Constructors are defined like other methods but use the name of the class.

15 Creating a new object wall = new Square(); variable of type Square assignment creates an instance of the class calls the constructor

16 Sending messages to objects by invoking their methods wall.moveHorizontal(-140);

17 public void draw() { wall = new Square(); wall.moveHorizontal(-140); wall.moveVertical(20); wall.changeSize(120); wall.makeVisible(); window = new Square(); window.changeColor("black"); window.moveHorizontal(-120); window.moveVertical(40); window.changeSize(40); window.makeVisible(); Java Source Code Picture.java

18 Java Source Code Picture.java roof = new Triangle(); roof.changeSize(60, 180); roof.moveHorizontal(20); roof.moveVertical(-60); roof.makeVisible(); sun = new Circle(); sun.changeColor("yellow"); sun.moveHorizontal(100); sun.moveVertical(-40); sun.changeSize(80); sun.makeVisible(); }

19 A method of one class can create objects of other classes, and can invoke those object’s methods. An object’s method can only be called with its correct method signature, i.e. with the correct number, data types and order of its parameters. Always put comments in your source code to ensure others can understand your program. Object Interaction & Source Code IMPORTANT POINTS TO REMEMBER

20 Comments Comments are statements that are put into a program to help others understand what it does Comments are marked by: // this is a single line comment /* this is a multiline comment */ /** so is this */

21 Java syntax - blocks and semicolons Curly brackets { } mark the beginning and end of blocks of code including classes and methods Semicolons ; mark the end of statements

22 COMPILING AND RUNNING JAVA PROGRAMS Java compiler JVM Running program Edit & debug Libraries / Java API Source code Java byte code Each Java class definition is held in a separate.java file, and the byte code is held in a.class file. Java Source Code Picture.java holds our source code, and Picture.class holds the detail the computer needs to create Picture objects.

23 Required Reading Objects First With Java – A Practical Introduction using BlueJ Related reading for this lecture Chapter 1 pp 3-17 (Objects and Classes)


Download ppt "Object Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW."

Similar presentations


Ads by Google