www.hope.ac.uk faculty of sciences and social sciences hope object oriented programming pie eater...
Post on 19-Dec-2015
214 views
TRANSCRIPT
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Object Oriented ProgrammingPie Eater
Stewart Blakeway
FML 213
0151 291 3113
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Aims of the Presentation
• To apply advanced features for PieEater– World Boundaries– Creating Pies– Placing Pies– Random Pies
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we know - Recap
public class Picture {
private Circle pieEater;
}
public void walk() {
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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 stepsxIf stepsx < 8 and direction = ‘E’ then walkelse do nothing!
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Pie Eater and Boundariespublic 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++; } }
Class Exercises – Write the code to
solve the problem with the North and South
boundary issue
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
My Solutionpublic 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++; } }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
One Piepublic 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(); }
First we create one pie
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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; } }
What data structure would you use for this?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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; } }
What data structure would you use for this?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Creating Pies Initial Solutionpublic 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; } }
My solution
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Two Messagespublic 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(); }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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);
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Two Messagespublic 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(); }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Two Messagespublic 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(); }
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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?
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any Questions?
• Next week is a drop in support session – location to be arranged.