Assignment 5: Building More Complex Tasks: 1 Background

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment 5: Building more complex tasks

Object-Oriented Programming in Greenfoot

Background

In the previous assignment you have experimented with Greenfoot, and you learned the basic control structures of Java, such as conditional statements and repetitions (loops). These control structures enable you to specify capabilities (behaviours) of objects. In this assignment we will focus on properties (states) of objects. These properties inuence the objects behavior.

Learning objectives
edit, compile, run, debug and test your own code in Greenfoot; design and implement complex methods. extend objects with attributes/variables; dene more complex tasks

After doing this assignment you are able to do the following:

Instructions

In the previous assignments, weve already seen a case in which it was necessary to use an extra attribute that enabled Fryde to remember what she did in a previous step. In the current assignment, the intelligence of Fryde will be extended further.

3.1

Top-down scheme

Fryde must walk around a eld enclosed by a fence, as tightly as possible (so that at each moment she has a piece of fence beside her) and stop when she is back in her initial position; see Figure 1

Figure 1: Field enclosed by a fence

Building more complex tasks

Assignment 5

1. To get a fenced area in your world follow the following instructions. Open the chickenworld5 scenario, and compile (if necessary) this scenario. You should now have an chicken and a fenced area in the world. 2. Before you start programming, you must start with a design: rst draw a top down-scheme on paper for your program; next the function-headers (like e.g. void detectFence() ) and nally clarifying comment-lines for the procedures you are going to produce. Think about how you will accomplish this: How will Fryde know if there is a piece of fence right beside her? How will Fryde know when she has reached her initial position? Your code should work for just about any shape of area, not just the example given! You will not get any assistance to solve problems around your program-code as long as you cannot show such design on paper! Work efciently and make a design in advance. As we have seen in the previous assignments, the idea of your act method is that it supposed to do a single step of the corresponding actor, i.e. it should not result in the completion of a complex task. For instance, using while loops to lay eggs leads to unnatural behaviour of Fryde: all the eggs appear at the same time. One would expect Fryde to perform his task stepwise: eggs should be laid one after another by repeatedly invoking the act method. In this case the repetition should not be performed explicitly inside the act method (by using while loops) but implicitly by running the simulation, which calls act over and over again for all objects. In some cases it appears necessary that an actor should remember what it did in the previous step. Suppose you want to learn Fryde how to walk to the upper left corner of his world. Using renements, the task can be specied in four successive subtask: 1. Let Fryde face northwards; 2. Let Fryde walk to the upper wall 3. Make a turn left 4. Let Fryde walk to the left wall Implementing this directly with while-loops is relatively simple, but would result in immediate jump of Fryde to the left corner, which is unsatisfactory. To convince yourself, just press on the run button of your scenario which should bring Fryde straight to left corner. 1. Open the Barnfield class and examine the act method to see how this task was implemented. Note that the top-down scheme is closely followed by adding private methods for performing all subtasks. As you can see, all methods use while loops to implement the desired behavior. 2. Change the move into moveAndMark to see which path Fryde has really traveled to get to the corner.

3.2

Using attributes

The solution to this problem is to add a variable/attribute to the Barnfield class which allows Fryde to remember the subtask she is currently performing. The easiest way is to introduce (inside the Barnfield class) the following enumeration type in which the successive subtasks are enumerated:
enum Task { TurningNorthwards, WalkingUpwards, TurningLeft, WalkingLeftwards, JobDone }

Programming in Greenfoot

Building more complex tasks

Assignment 5

Moreover, an attribute of this type is added to the class in which the current phase is remembered. The example scenario used in this assignment already contains a denition of Barnfield including this extension. 1. The method that is implemented in this fashion is called private void stepToULCorner(). Examine the carefully and be sure that you understand whats going on. 2. In the example Fryde is facing westwards. Suppose you do not know her starting direction in advance, does stepToULCorner still work correctly? You can try this by turning Fryde manually before you run the scenario. 3. Extend the Fryde class in such a way that at the end of Frydes tour to the corner she is heading the same direction as she had initially. How does Fryde remember her initial direction? Hint: count how many times turnRight() is called before Fryde faces northwards. 4. The next step is to learn Fryde how to move to a specic location in his world, expressed by an x and ycoordinate. These coordinates should be added as attributes to the Fryde class and can be set by a method setDestination. Extend the Fryde class with this method. What are the parameters of method? Does it return anything? 5. Rewrite the act method such that it makes Fryde walk to the indicated position. You may assume that the world is free of obstacles, so you can remove the code for making the fence from the ChickenWorld class. Do not remove this code from the class: you still need it later on. 6. Your implementation should work for any starting position of Fryde. As usual, before implementing your algorithm you should draw a top-down scheme for your solution. 7. Compile and test your method. Does it work as expected?

3.3

Walking around the fence

Now we can go back to the starting problem of this assignment: walking around the fence. Youve already designed an algorithm on paper by drawing an appropriate top-down scheme for your solution. 1. Implement your algorithm by adding an enumeration type for separating the subtasks, and new helper methods to the Barnfield class and adjusting the act method accordingly. 2. Compile and test your method. Does it work as expected?

Finishing up

When you have nished the above exercises call one of the teachers to come and check your work. They will note that you have completed the assignment and allow you to continue with the next assignment. You will need the work you have done today during the next assignments. Before continuing, save a copy of your work so that you can always refer back to it when necessary. In Greenfoot, select Scenario in the top-menu, and then Save a copy as. Change the le name so that it has you own name in it and the assignment number, such as: Fryde Tom ass5.

Delivering of your products


Adjust the README.TXT le as usual, compress the scenario folder and hand the compressed le in via Blackboard. Mind the deadline that is mentioned in the assignment in Blackboard! Programming in Greenfoot 3

You might also like