24
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Object Oriented Programming Pie Eater Stewart Blakeway FML 213 [email protected] 0151 291 3113

Object Oriented Programming Pie Eater

  • Upload
    raleigh

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

Object Oriented Programming Pie Eater. Stewart Blakeway FML 213 [email protected] 0151 291 3113. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEObject Oriented ProgrammingPie Eater

Stewart BlakewayFML [email protected] 291 3113

Page 2: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat 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

Page 3: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEAims of the Presentation

• To apply advanced features for PieEater– World Boundaries– Creating Pies– Placing Pies– Random Pies

Page 4: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat we know - Recap

public class Picture {

private Circle pieEater;

}

public void walk() {

}

Page 5: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PELet’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?

Page 6: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PESliding 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!

Page 7: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPie Eater and Boundaries

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++; } }

Class Exercises – Write the code to

solve the problem with the North and South

boundary issue

Page 8: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEMy Solution

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++; } }

Page 9: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PELet’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?

Page 10: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEOne 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(); }

First we create one pie

Page 11: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PECreating 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

Page 12: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

public class Picture { int[] gridColumn = new int[8];

Creating Pies initial solution1. 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?

Page 13: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

public class Picture { int[] gridColumn = new int[8];

Creating Pies initial solution1. 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?

Page 14: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PECreating Pies Initial Solution

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; } }

My solution

Page 15: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PESo 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?

Page 16: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETwo 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(); }

Page 17: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPlacing 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);

Page 18: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETwo 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(); }

Page 19: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PENearly 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?

Page 20: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETwo 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(); }

Page 21: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PESo 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

Page 22: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PENext

• 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?

Page 23: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEConclusion

• 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)

Page 24: Object Oriented Programming Pie Eater

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEAny Questions?

• Next week is a drop in support session – location to be arranged.