Download presentation
Presentation is loading. Please wait.
1
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Object Oriented Programming Pie Eater Revisited Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113
2
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Aims of the Presentation To create our friend from last year – Pie Eater To create Pie Eaters world To give Pie Eater some mobility – walk – turn left – turn right
3
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we know so far public class Picture { private Circle pieEater; } public class Picture { private Circle pieEater; } public void walk() { } public void walk() { }
4
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater Pie Eater is very complex – He can walk – He can turn left – He can turn right – He can eat pies His world is complex – There are boundaries – It can contain pies What do we do with any complex problem that requires solving?
5
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Create Pie Eater How can we achieve this? – We will do this in a method (message) – Remember we have use of the other classes Triangle Square Circle Rectangle
6
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater public class Picture { private Circle pieEater; } public class Picture { private Circle pieEater; } public void createPieEater() { pieEater = new Circle(); pieEater.changeColor("blue"); pieEater.moveHorizontal(87); pieEater.moveVertical(-29); pieEater.changeSize(30); pieEater.makeVisible(); } public void createPieEater() { pieEater = new Circle(); pieEater.changeColor("blue"); pieEater.moveHorizontal(87); pieEater.moveVertical(-29); pieEater.changeSize(30); pieEater.makeVisible(); } We do something similar for the tail
7
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Create Pie Eater’s World How can we achieve this? – We will do this in a method (message) – Remember we have use of the other classes Triangle Square Circle Rectangle – Think about the grid – it is a repetition of lines Horizontal lines Vertical lines
8
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater’s World public class Picture { private Circle pieEater; private Rectangle xLine; } public class Picture { private Circle pieEater; private Rectangle xLine; } public void drawGrid() { for (int y=10; y<400; y=y+40) { xLine = new Rectangle(); xLine.changeColor("black"); xLine.moveVertical(y); xLine.moveHorizontal(50); xLine.changeSize(3,320); xLine.makeVisible(); } public void drawGrid() { for (int y=10; y<400; y=y+40) { xLine = new Rectangle(); xLine.changeColor("black"); xLine.moveVertical(y); xLine.moveHorizontal(50); xLine.changeSize(3,320); xLine.makeVisible(); } We do something similar for the vertical line
9
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Make Pie Eater Walk How can we achieve this? – We will do this in a method (message) – Remember we have use of the other classes Triangle Square Circle Rectangle – There are a couple of ways to do this We can make him jump for one square to the next Or, we can create more complex code that allows for smooth transition
10
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater walking public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public void walk() { } public void walk() { } Class exercise Complete the code for the method. Assume we have objects called: pieEater and tail. The squares in the grid are 40x40px Class exercise Complete the code for the method. Assume we have objects called: pieEater and tail. The squares in the grid are 40x40px
11
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater walking public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public void walk() { pieEater.moveHorizontal(40); tail.moveHorizontal(40); } public void walk() { pieEater.moveHorizontal(40); tail.moveHorizontal(40); } My solution
12
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Make Pie Turn Left How can we achieve this? – Your suggestions???
13
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater Turn Left public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public void turnLeft() { tail.moveHorizontal(18); tail.changeSize(10,5); tail.moveVertical(16); } public void turnLeft() { tail.moveHorizontal(18); tail.changeSize(10,5); tail.moveVertical(16); } My solution
14
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Considerations? Walking pieEater.moveHorizontal(40); tail.moveHorizontal(40); What if we are facing Left, Up or Down ?
15
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Considerations? Turning tail.moveHorizontal(18); tail.changeSize(10,5); tail.moveVertical(16); What if we are facing Left, Up or Down ?
16
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Solution We need to know which direction Pie Eater is facing – If we walk, we need to know which direction to walk – If we turn right we need to know which direction we are facing When do we record the direction? When do we check the direction?
17
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Recording the Direction Pie Eater starts by default facing East – Each time Pie Eater turns right we must update his direction (after checking his direction) – Pie Eater needs and attribute called direction
18
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Recording the Direction Pie Eater starts by default facing East – Each time Pie Eater turns right we must update his direction (after checking his direction) – Pie Eater needs and attribute called direction public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; String direction = “E”; } public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; String direction = “E”; } public void setDirection(String turn) { if (direction == "E") { if (turn == "L") { direction = "N"; } else { direction = "S"; } } public void setDirection(String turn) { if (direction == "E") { if (turn == "L") { direction = "N"; } else { direction = "S"; } } My solution
19
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Recording the Direction When do we update the direction? I.e. when do we call the setDirection(String turn) method (message)?
20
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater Turn Left public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public void turnLeft() { tail.moveHorizontal(18); tail.changeSize(10,5); tail.moveVertical(16); setDirection (“L”); } public void turnLeft() { tail.moveHorizontal(18); tail.changeSize(10,5); tail.moveVertical(16); setDirection (“L”); } My solution
21
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE So far Created Pie Eater (with a tail) Walked Turned Left Turned Right Checked Direction Updated Direction
22
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next Not all directions have been accounted for yet – When walking – Turning We have not discussed boundaries – How do we stop Pie Eater leaving his world? Where are the pies? – How do we place them on the grid? – Where do we place them? – How many do we place? Pie Eater encounters a pie – How does pie eater know there is a pie? – How does pie eater eat the pie? Should Pie Eater have friends in his world?
23
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Conclusion Pie Eater is a complicated application Many real world factors have to be accounted for Object Orient Programming is an excellent approach for this type of problem You are to create your own PieEater Application – Your project will be compiled – You will demonstrate your project next week (5 min per person) – Your project will be placed on the website so that you can impress your friends and your tutors (better work hard)
24
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.