Object Oriented Programming 4

carenextSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

79 views

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

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

www.
hope
.ac.uk




Faculty
of Sciences and Social Sciences



HOPE

What we know so far

public class Picture



{




private Circle
pieEater
;










}

public void walk()


{




}

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?

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


www.
hope
.ac.uk




Faculty
of Sciences and Social Sciences



HOPE

Pie Eater

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


}

We do something
similar for the tail

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


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

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


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

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 void walk()



{



pieEater.moveHorizontal
(40);


tail.moveHorizontal
(40);




}

My solution

www.
hope
.ac.uk




Faculty
of Sciences and Social Sciences



HOPE

Let’s Make Pie Turn Left


How can we achieve this?




Your suggestions???


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 void
turnLeft
()



{



tail.moveHorizontal
(18);


tail.changeSize
(10,5);


tail.moveVertical
(16);



}

My solution

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 ?

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 ?

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?

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

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 void
setDirection
(String turn)


{


if (direction == "E")


{


if (turn == "L")


{ direction = "N"; }


else


{ direction = "S"; }



}


}

My solution

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

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 void
turnLeft
()



{



tail.moveHorizontal
(18);


tail.changeSize
(10,5);


tail.moveVertical
(16);



setDirection

(“L”);



}

My solution

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

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?

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)

www.
hope
.ac.uk




Faculty
of Sciences and Social Sciences



HOPE

Any Questions?