Download presentation
Presentation is loading. Please wait.
1
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Object Oriented Programming Pie Eater Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113
2
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done so far Created PieEaters world – grid of 8x6 – cells of 20px by 20px Created PieEater – walks (in direction facing) – records his direction – turns left – turns right
3
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Aims of the Presentation To apply advanced features for PieEater – World Boundaries – Creating Pies – Placing Pies – Random Pies
4
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we know - Recap public class Picture { private Circle pieEater; } public class Picture { private Circle pieEater; } public void walk() { } public void walk() { }
5
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Create World Boundaries How can we achieve this? – How many steps can pieEater take before he leaves his world if facing East? – How many steps can pieEater take before he leaves his world if facing South? – How many steps can pieEater take before he leaves his world if facing West? – How many steps can pieEater take before he leaves his world if facing North?
6
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sliding Scales Walking East and West 1 2 3 4 5 6 7 8 int stepsx = 1; //PieEater starts in square 1 facing E We know his direction because each time we turn left or right, it is set. Each time we walk we add 1 to stepsx If stepsx < 8 and direction = ‘E’ then walk else do nothing!
7
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Pie Eater and Boundaries public class Picture { private Circle pieEater; int direction = “E”; int stepsx = 1; } public class Picture { private Circle pieEater; int direction = “E”; int stepsx = 1; } public void walk() { if ((direction == "E") && (stepsx < 8)) { pieEater.moveHorizontal(40); tail.moveHorizontal(40); stepsx++; } public void walk() { if ((direction == "E") && (stepsx < 8)) { pieEater.moveHorizontal(40); tail.moveHorizontal(40); stepsx++; } Class Exercises – Write the code to solve the problem with the North and South boundary issue
8
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE My Solution public class Picture { private Circle pieEater; int direction = “E”; int stepsy = 1; } public class Picture { private Circle pieEater; int direction = “E”; int stepsy = 1; } public void walk() { if ((direction == "N") && (stepsy > 1)) { pieEater.moveVertical(-40); tail.moveVertical(-40); stepsy--; } if ((direction == "S") && (stepsy < 6)) { pieEater.moveVertical(40); tail.moveVertical(40); stepsy++; } public void walk() { if ((direction == "N") && (stepsy > 1)) { pieEater.moveVertical(-40); tail.moveVertical(-40); stepsy--; } if ((direction == "S") && (stepsy < 6)) { pieEater.moveVertical(40); tail.moveVertical(40); stepsy++; }
9
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Let’s Create Some Pies 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 This is a complex problem. What do we do with complicated problems?
10
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE One Pie public class Picture { private Circle pie; } public class Picture { private Circle pie; } public void drawPie() { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(-30); pie.moveVertical(86); pie.changeSize(30); pie.makeVisible(); } public void drawPie() { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(-30); pie.moveVertical(86); pie.changeSize(30); pie.makeVisible(); } First we create one pie
11
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating Pies Considerations We need to ask the user how many pies they want on the grid We need to place the pies on the grid at random locations Dealing with co-ordinates x and y can be confusing and difficult to work with – easier to work with rows and columns
12
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE public class Picture { int[] gridColumn = new int[8]; public class Picture { int[] gridColumn = new int[8]; Creating Pies initial solution 1.Create a data structure that stores the positions of the 8 columns public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } What data structure would you use for this?
13
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE public class Picture { int[] gridColumn = new int[8]; public class Picture { int[] gridColumn = new int[8]; Creating Pies initial solution 1.Create a data structure that stores the positions of the 6 rows – the first row position is -30 public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } What data structure would you use for this?
14
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Creating Pies Initial Solution 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 createPie() { int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } public void createPie() { int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } My solution
15
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE So far we have created a method called – drawPie () draws a pie and places it in the first square – createPie () creates two data structures which contains arrays called gridRow and gridColumn How can we draw a pie using a gridRow and gridColumn value?
16
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Two Messages public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } drawPie (gridColumn[4],gridRow[3]); } public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } drawPie (gridColumn[4],gridRow[3]); } public void drawPie(int col, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); } public void drawPie(int col, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); }
17
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Placing Pies Randomly Java comes with an inbuilt message that will generate and return a random value – We could use this to return a random gridColumn – and then again for a random gridRow row = (int)(Math.random() * 6); column = (int)(Math.random() * 8);
18
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Two Messages public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } int row = (int)(Math.random() * 6); int column = (int)(Math.random() * 8); drawPie (gridColumn[column],gridRow[row]); } public void createPie() { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } int row = (int)(Math.random() * 6); int column = (int)(Math.random() * 8); drawPie (gridColumn[column],gridRow[row]); } public void drawPie(int col, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); } public void drawPie(int col, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); }
19
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Nearly There! we have created a method called – drawPie () draws a pie and places it in a square specified – createPie () creates two data structures which are arrays called gridRow and gridColumn populates the array with y and x coordinates creates random values for row and column passes the gridRow and gridColum array elements using the random values What if we want more pies?
20
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Two Messages public void createPie(int numOfPies) { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } for (int x=1; x < numOfPies ; x++) { int row = (int)(Math.random() * 6); int column = (int)(Math.random() * 8); drawPie (gridColumn[column],gridRow[row]); } public void createPie(int numOfPies) { int posx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } for (int x=1; x < numOfPies ; x++) { int row = (int)(Math.random() * 6); int column = (int)(Math.random() * 8); drawPie (gridColumn[column],gridRow[row]); } public void drawPie(int col, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); } public void drawPie(int col, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); }
21
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE So far Pie Eater – Created Pie Eater (with a tail) – Walked – Turned Left – Turned Right – Checked Direction – Updated Direction We have discussed boundaries – Stopped Pie Eater leaving his world The pies – Placing them on the grid – Random location when placing them – Allowing a specific number of pies
22
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Next 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 finish 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? Next week is a drop in support session – location to be arranged.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.