Download presentation
Presentation is loading. Please wait.
1
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 3 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 2 Introduction to BlueJ Creating objects in BlueJ 2
3
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 2 Sending messages to those objects – square1.makeVisible() Messages with integer and String parameters – square1.moveHorizontal(50) – square1.changeColor(“blue”) 3
4
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 2 Modifying the code for a message to draw a picture of a house 4
5
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today Reminder of Java programming constructs Adding animation to picture drawing Adding new messages to the protocol of a Java class Visualising messages passing between objects 5
6
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java Programming Constructs Sequence Selection Repetition 6
7
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence pie1.turnLeft(); pie1.walk(); 7
8
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Selection Design if row is less than 4 then begin walk end Java if (row < 4 ) { pie1.walk(); } 8 ‘Decisions, decisions!'
9
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Selection Design if row is less than 4 then begin walk end else begin turn left end Java if (row < 4 ) { pie1.walk(); } else { pie1.turnLeft(); } 9 ‘Decisions, decisions!'
10
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Repetition - while Design i := 0 while i is less than 5 begin walk turn left i := i+1 end Java i = 0; while (i < 5 ) { pie1.walk(); pie1.turnLeft(); i++; } 10
11
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Repetition - for Design for 100 times begin turn left end Java for (int i=0; i < 100; i++) { pie1.turnLeft(); } 11 Declare integer i, set to 0 OK in full Java like BlueJ, but not in Java Trainer As long as i is less than 100 Increment i by 1 for each pass
12
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Alternative Java layouts if (row < 4 ) { pie1.walk(); } if (row < 4 ){ pie1.walk(); } 12
13
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Alternative Java layouts if (row < 4 ) { pie1.walk(); } else { pie1.turnLeft(); } if (row < 4 ){ pie1.walk(); } else { pie1.turnLeft(); } 13
14
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Alternative Java layouts i = 0; while (i < 5 ) { pie1.walk(); pie1.turnLeft(); i++; } i = 0; while (i < 5 ) { pie1.walk(); pie1.turnLeft(); i++; } 14
15
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Alternative Java layouts for (int i=0; i < 100; i++ ) { pie1.turnLeft(); } for (int i=0; i < 100; i++ ) { pie1.turnLeft(); } 15
16
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Conditions in Java Pseudo CodeJavaDescription A < B A is less than B A > B A is greater than B A = B A == B A equals B A <> B A != B A does not equal B A >= B A is greater than or equal to B A <= B A is less than or equal to B 16
17
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE BlueJ Project hopeball: Adding animation 17 public class Picture { private Circle ball; public void draw() { int i; ball = new Circle(); ball.changeColor("blue"); ball.moveVertical(50); ball.moveHorizontal(70); ball.changeSize(50); ball.makeVisible(); for (i=1; i<100; i++) { ball.moveHorizontal(1); }
18
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Slowing it down a little for (int i=0; i < 1000; i++ ) { // Do something useless that doesn’t // get optimised out by the compiler } 18
19
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Slowing it down a little for (int i=0; i < 1000; i++ ) { System.out.print(“Hello”); } 19
20
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE BlueJ Project hopeball: Adding animation 20 public class Picture { private Circle ball; public void draw() { int i; ball = new Circle(); ball.changeColor("blue"); ball.moveVertical(50); ball.moveHorizontal(70); ball.changeSize(50); ball.makeVisible(); } public void moveLeft() { ball.moveHorizontal(1); } public void moveLeft(int dist) { ball.moveHorizontal(dist); } Creating a new message
21
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater revisited Programming PieEater 21
22
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 22 Project named javapieeater Pie Eater revisited
23
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 23 Creating a PieEater object using a constructor Create a PieEater object with the constructor PieEater(String title, String colourName)
24
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Constructor parameters Remember – we need double quotes around the String values 24
25
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie1 Object 25 PieEater attributes
26
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Objects can have other objects as attributes Just like our Picture objects included Circle, Triangle etc 26 Picture attributes
27
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Objects can have other objects as attributes PieBoss class includes PieEater object as an attribute 27 PieBoss attribute
28
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The full PieBoss class 28 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); pie1.walk(); } Naming the classDeclaring the attribute Defining a method
29
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The full PieBoss class 29 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); pie1.walk(); } Creating a new PieEater object called pie1
30
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Adding a method to walk a number of steps 30 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); this.steps(5); } public void steps(int n) { int i; for( i=0; i<n; i++) { pie1.walk(); }
31
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Adding a method to walk a number of steps 31 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); this.steps(5); } public void steps(int n) { int i; for( i=0; i<n; i++) { pie1.walk(); } Using this to send a message to itself
32
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence diagram Visualising the flow of messages between objects 32 time
33
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence diagram Vertical lines represent objects User treated as an object sending messages 33 time
34
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence diagram User sends PieBoss message demo1() 34 time
35
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence diagram PieBoss responds by creating a new PieEater object 35 time
36
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence diagram... then sending itself the message steps(5)... 36 time
37
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sequence diagram... which in turn sends the PieEater the message walk(). 37 time
38
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have covered Reminder of Java programming constructs Adding animation to picture drawing Adding new messages to the protocol of a Java class Visualising messages passing between objects
39
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Any Questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.