• No results found

Learning objectives

N/A
N/A
Protected

Academic year: 2021

Share "Learning objectives"

Copied!
21
0
0

Bezig met laden.... (Bekijk nu de volledige tekst)

Hele tekst

(1)

Algorithmic Thinking and Structured Programming (in Greenfoot)

2017 Renske Smetsers-Weeda & Sjaak Smetsersc 1

Contents

Introduction 1

Learning objectives 1

Instructions 1

Theory 2

6.1 Object types and variables . . . 2

6.2 Calling a method from another class . . . 3

6.3 Lists . . . 4

6.4 For-each Plan . . . 6

6.5 Surprise eggs . . . 7

6.6 Average loop plan . . . 7

6.7 Selection Sort . . . 8

Challenges 10 6.1 Walking through a list . . . 10

6.2 Java methods for lists . . . 11

6.3 Print coordinates and values of all eggs in a list . . . 12

6.4 Which is is the most valuable? . . . 14

6.5 Where is the most valuable egg? . . . 14

6.6 Print average value of list . . . 14

6.7 Reversing a list . . . 14

6.8 Sort a list withSelection Sort . . . . 16

6.9 Calculate distance . . . 16

6.10 Goto the most distant egg in the world . . . 16

6.11 Re-factoring . . . 16

Reflection 18

Saving and Handing in 20

1Licensed under the Creative Commons Attribution 4.0 license:https://creativecommons.org/licenses/by/4.0/

(2)

Introduction

Lists are commonly used to store data that belongs together. Lists can comprise of almost anything.

Think of the lists that you use in every day life: Shopping lists, homework lists, lists of favorite websites.

In challenge 5.1 you wrote the a method to count all the eggs in the world: Dodo walks through the entire world and counts all the eggs she finds. However, there’s an even smarter way to look for all the eggs. For this, we will use aList. Using aListgives you the opportunity to come up with smarter algorithms. By having a list of all of theEggobjects in the world, you would also have their coordinates and values at your fingertips (using getter methods). You could use a list to help a lazy Dodo find all the eggs in the world using the least amount of steps.

In this assignment you will first learn how to use aListto store variables. You will then learn how to do specific things with a list, like walk through its elements, swap elements, or sort the elements.

Later you will learn how to use lists withobject types such asEggs. This will be helpful for Dodo’s race in the assignment that follows.

Learning objectives

After completing this assignment, you will be able to:

• declare and useListvariables;

• use Java Library Documentation to look for and use existing Java methods;

• apply existingListmethods, such as adding and removing elements from a list;

• use a for-each-loop for traversing the elements of a list;

• swap elements in a list;

• sort objects in a list;

• apply plans for calculating the average of a list of values;

• explain whatnullmeans and what it is used for;

• use a list to store primitive types (such asint,String) or object types (such asEgg).

Instructions

For this assignment you will need:

• scenario ’DodoScenario6’: to be downloaded from the course website2.

Throughout the assignment you will also need to answer some questions. The following must be handed in:

• All flowcharts: use pencil and paper, or go to https://www.draw.io/;

• Your code: the fileMyDodo.javcontains all your code and must be handed in;

• The reflection sheet: complete and hand it in.

2http://course.cs.ru.nl/greenfoot/

(3)

You must discuss all other answers with a programming partner. Jot down a short answer on (the assignment) paper.

There are three types of challenges:

Recommended. Students who need more practice or with limited programming experi- ence should complete all of these.

Mandatory. Everyone must complete these.

Excelling. More inquisitive tasks, designed for students who completed 2 star tasks and are ready for a bigger challenge.

Students who skip 1-star challenges should complete all 3-star challenges.

A note in advance:

• In this assignment you may only make changes to theMyDodoclass;

• You may use methods from theMyDodoorDodoclass, not from theActorclass;

• Teleportation is not permitted: if Mimi needs to get somewhere, she must walk there!

Theory

Theory 6.1: Object types and variables

We have learned that Java has primitive types such asintandboolean. Primitive types are built-in Java: you get primitive types as a gift.

In addition to primitive types, Java also hasobject types(sometimes calledreference types). Object types are types that belong to a class. You can make a new object type by writing (or importing) a new class. Examples of object types areMyDodoandEgg.

Use of object types

You can use object types in the same way as you use primitive types. As a reminder, the following table summarizes how you have already used types:

Location Example Type

result int methodName( ) int

parameter void methodName( String text ) String

local variable int value = 4 int

instance variable private int myNrEggsFound = 0 int

Object variables

You have also already seen object types. For example, previously we gently skipped over the following (from Assignment 4):

World myWorld = getWorld( );

Here the variable with the namemyWorldand typeWorldis declared (made) and immediately initialised (given a value) with the result ofgetWorld( ). The result type isWorld. You can see this by looking at the signaturepublic World getWorld( ). BecauseWorldis a class, its type is an object type (rather than a primitive type). A variable’s type determines the values it may contain. For example, a variable

(4)

with typeint(such asint nrOfEggs)can only store integers. It can’t store abooleanorString. The same applies to object types. A variable whose type is an object type, such asEgg, can storeEggobjects, but it can’t store aFenceobject or anintvalue. Any attempt to assign an object to a variable with a different type will result in a compiler error. However, there is an exception to this rule, namely, we can store aBlueEggor aGoldenEggobject in anEggvariable. This is because eachBlueEggorGoldenEgg object is also an Egg(have a look back at the theory 1.5 on ’Inheritance’). An example of a variable declaration and initialization with an object type is:Egg firstEgg = new BlueEgg( );

In our figures, we draw variables storing objects using an arrow (Figure 1).

Figure 1: A variable referring to an object

Here, the variablefirstEggstores areference to the egg object. In fact, object variables always store references to objects, and not the objects themselves.

Theory 6.2: Calling a method from another class

When calling a method, the method can either belong to the object itself or belong to another object.

How you call the method depends on who the method belongs to:

1. The method belongs to an object’s own class (or a class that is inherited). In this case the object calls the method directly (as you have been doing all along).

2. The method belongs to a different class. To be able to call a method from another class, you first need to have an object from that class.

General explanation

: Consider an object from that class calledobject, and a method from object’s class calledmethod. Assuming that the method has no parameters, useobject.method( ); to call the method. You can read the instruction as follows:objectis asked to execute his methodmethod.

Example of calling a method from a different class

: Imagine there is aBlueEggobject calledbabyBlueEgg. TheBlueEggclass has a methodint getX( ) (inherited fromActor) which returns the egg’s x-coordinate.

Mimi wants to know where the egg is (its coordinates). Mimi can askbabyBlueEgg’s x-coordinate by using:babyBlueEgg.getX( );. This call is done from Mimi’sMyDodoclass.

Note

Tip: After typing the object’s name followed by ’.’ (in the example above: ’babyBlueEgg.’), use ’Ctrl+Space’

to see a list of all the methods available for that object.

(5)

Figure 2: To see a list of all available methods, use: ’Ctrl+Space’

Theory 6.3: Lists

The object variables that we used in the previous assignments could hold exactly one object. However, sometimes it can be useful to store a whole series (or row) of objects, rather than just one object.

Obviously you could make a separate variable for each object, but if you have lots of objects that’s not practical. What you need is a way to group objects. There are several ways to do this in Java, the easiest is using aList.

AListis a series of variables. These variables are theelements of a list. They are stored in sequence, and therefore you can number them. And that is exactly the way in which you can select an element.

Namely, you can select (or find) an element by using its position orindex. We start counting at 0. This is done as follows: the first element has index (is at position) 0, the second element has index 1, ... , the tenth element has index 9. So, you can select the 10th element out of a list by asking for the element with index 9.

Elements in lists

All elements in a list are of the same type. You can’t storeEggs andDodos in the same list: it is either a list ofEggs or a list ofDodos.

Operations on lists

The number of elements in a list is called its size. You can add elements to a list. You can remove elements out of a list. You can add or remove anywhere in a list: the front, back, or somewhere in the middle (using the index to indicate exactly where). A list with no elements isempty.

Lists in Java

The Java class for lists is calledList. Before using lists you must import a special library. This sounds more complicated than it is: just add the following line of code to the top of the class document where you want to use lists:import java.util.List;.

The JavaListlibrary has lots of useful methods which you can use:

(6)

Method call Effect

size( ) Returns the number of elements in the list (its length).

get(int index) Returns the element at position ’index’.

add(int index, E element) Adds ’element’ to the list at position ’index’, increasing the length of the list.

remove(E element) Removes ’element’ from the list, decreasing the length of the list.

remove(int index) Removes an element from list at position ’index’.

isEmpty( ) Returns whether or not the list is empty. Because you can’t much with an empty list, this is a very useful check before trying to do anything with any elements.

contains(Object o) Returns whether or not object ’o’ is in the list.

Knowing which methods are available to you, and understanding how to use them can save you a lot of time. Why reinvent the wheel when these methods have been written and tested for you!

Have a look at the complete Java libraryList. To get an overview of all the methods available to you, you can do one of the following two steps:

• Go to https://docs.oracle.com/javase/8/docs/api/java/util/List.html.

• In the Greenfoot menu choose ’Help’, and then ’Java Library Documentation’. Then search for

’List’.

A list can contain objects. But it’s important to realize that lists are objects too! A cookbook has a list of recipes and each recipe has a list of ingredients. So, a list can contain a list of other objects.

Declaring a list

The same rules which apply to any other objects also apply to list objects. If you want to use a list, you first declare a variable in which you can store a reference to a list object. For example, a list of compliments for Mimi can be declared as follows:

List<String> dodosCompliments;

wheredodosComplimentsis the variable’s name andStringis the type of the objects in the list. Notice that a special notation is used to indicate the type of the elements: the element-type is placed between

’<’ and ’>’ as a parameter.

Formally, this is called ageneric type or a parameterized types, because it accepts an additional type (surrounded by < . . . >) as parameter. Generic types require that such a type parameter is an object type, and not a primitive type. In the previous example this is okay becauseStringis indeed an object type.List<int>, however, would be illegal becauseintis primitive. In case you need a list of integers, you should use the object typeIntegerinstead ofint. As a matter of fact, Java provides object types for all its primitive types.

TheMyDodoclass has an example of lists:

public List<Egg> getListOfEggsInWorld( )

This method returns a list of all the eggs in the world. This can be useful when programming Mimi to find the eggs in the world which she has to hatch.

Using a list

To use this list of eggs, a variable to store the list itself must first be declared:

(7)

List<Egg> eggsInTheWorld = getListOfEggsInWorld( );

In the code above:

• A variable with the nameeggsInTheWorldis declared;

• The type ofeggsInTheWorldisList<Egg>(a list of eggs);

• The variableeggsInTheWorldis set to the result ofgetListOfEggsInWorld(so theeggsInTheWorld is a list of all the eggs in the world at that moment).

Theory 6.4: For-each Plan

To traverse a list (i.e. to go through each of the elements one-by-one), in Java afor-each-loop is used.

void methodDoSomethingWithList ( ) {

for ( ElemType elemVar: listOfElems ) { doSomethingWithElement ( elemVar );

} }

Here a list namedlistOfElemsis traversed. For each element in the list, the code in the loop is ex- ecuted. So for a list with 5 elements, the code in the loop is executed 5 times. During each loop the variableelemVarattains the next object in the list. The object’s type isElemType. In the method call doSomethingWithElement, something is done with that particular object. The corresponding flowchart is as follows:

Figure 3: Flowchart for afor-each-loop

Note: A for-eachis should not be used for actions which change the list during the loop, such as swapping elements. If the intention is to change the list during the loop, use awhile.

Example:

The following code traverses a list ofEggelements and increments their value by 1:

public void incrementValueOfEachEggInWorld ( ) {

List<Egg> eggList = getListOfEggsInWorld( ); //get the list of eggs for ( Egg egg: eggList ) { // get an egg from the list

int currentEggValue = egg.getValue( ); // get the value of the current egg currentEggValue ++; // and increment its value

(8)

egg.setValue ( currentEggValue ); // store new value in egg }

}

In this example, the methodgetListOfEggsInWorld( ) returns a list of eggs in the world. The list variableeggListis declared and initialized, it stores the list of eggs. In the for-each-loop, one-by-one, we take a look at eachEgg in theeggList. For this egg the methodsgetValueandsetValueare both called. So, at the end of the for-each-loop, each of the eggs in the world will have an increased valued.

Note: The code above can be written shorter, and without a helper-variable:

public void incrementValueOfEachEggInWorld ( ) {

List<Egg> eggList = getListOfEggsInWorld( ); //get the list of eggs for ( Egg egg: eggList ) { // get an egg from the list

egg.setValue ( egg.getValue( )+1 ); // get, increment and store the value of the current egg

} }

Theory 6.5: Surprise eggs

New to Dodo’s world are surprise eggs. The classSurpriseEggis a subclass ofEgg(just likeBlueEgg and GoldenEgg). Blue eggs are worth one point, golden eggs are worth five points, and how many points a surprise egg is worth, well, that’s a surprise! In the constructor ofSurpriseEggits value is set randomly. This is done using the Greenfoot methodgetRandomNumber. So, every surprise egg can have a different value.

There is a method for making a list of surprise eggs in the classSurpriseEgg. The first parameter indicates the number of eggs you want to have in the list, the second is the world.

public static List<SurpriseEgg> generateListOfSurpriseEggs( int size, World world );

For example, the following code makes a list of 10 surprise eggs:

Theory 6.6: Average loop plan

Finding the average of a series of numbers is a common task in programming. This is called theAverage -Loop Plan. To calculate the average, we need the number of items and their sum. The calculation is then:average = sum / nrOfItems. We already used this in Challenge 5.6. However, this time we have all the values in a list instead of the world. So now we need to traverse the list to find the sum and the number of elements. For this plan, follow the next steps:

a) Declare and initialize the variables needed:

(a) The number of items.

(9)

(b) The sum of the items thus far (a running total).

b) Traverse all items using afor-each-loop;

c) For each item, add its value to the running total (current sum) and increase the number of items by one;

d) Check that the number of items is larger than 0 (to guard for a ’divide-by-zero’), if not report an error;

e) Calculate the average by dividing the sum by the number of items. Note that the average may be a decimal value (requiring acast to double, see Theory 5.3);

f ) Return the average.

Figure 4: Flowchart for calculating the average of several values in a list

Theory 6.7: Selection Sort

A sorting algorithm is used to sort the elements in a list into a particular order. A sorting algorithm can be used to sort numbers into order from smallest to largest, or largest to smallest. For example, a list of people’s lengths can be sorted from smallest to tallest. Or the results of a 100m race can be sorted from fastest to slowest.

A well known sorting algorithm is calledselection sort. This is how it’s done:

a) Determine where (at which index) the unsorted list starts (initially this is at the front of the list, 0).

b) Traverse the unsorted part of the list and find the smallest element.

(10)

c) Swap that smallest element with the element in the front of the unsorted list.

d) Update the index where the unsorted list starts.

e) Repeat the last three steps until the entire list has been sorted.

The following site shows an animation of the selection sort algorithm: https://visualgo.net/

en/sorting.

(11)

Challenges

Please read Theory 6.1: Object types and variables.

Please read Theory 6.2: Calling a method from another class.

Please read Theory 6.3: Lists.

Please read Theory 6.4: For-each Plan.

Challenge 6.1: Walking through a list

nIn this challenge we will practice using a list of integers. We will now use the list created by createListOfNumbersto experiment with.

a) Find theList<Integer> createListOfNumbers( )method inMyDodo.

b) Add comments to the code ofcreateListOfNumbers( )explaining what the method does. Tip:

The method has a helper methodasListwhich makes a list with the given values.

c) We will now adjust the methodpracticeWithListsso that we can use it:

i. Add comments to the methodpracticeWithListsexplaining what the first line does.

ii. The second line is supposed to print the first element to the console. What’s wrong? Fix the code and add comments.

iii. Declare a variable to hold a list of integers. Initialize it with the list returned bycreateListOfNumbers .

Tip: useList<Integer> listOfNumbers = createListOfNumbers( );

iv. Print the first element to the console.

v. Add code to print the number of elements (size) to the console.

d) Write a new methodvoid printNumberList( List<Integer> listOfNumbers )which prints all the numbers in a given list. Tips:

• Use a for-each-loop to step through each element in the list. Apply the For-each Plan de- scribed in Theory 6.4;

• Print the value of each element to the console (tip: to print a value without a new line, use System.out.printinstead ofprintln);

• The following flowchart visualizes this algorithm:

(12)

e) Write a method which prints the sum of all the elements of a list to the console. Call the method in yourpracticeWithListsmethod.

The following flowchart visualizes the algorithm of the Sum Plan (described in Theory 5.1) using a list:

Challenge 6.2: Java methods for lists

We will now have a look atListin the Java library. You will learn how to read the Java library docu- mentation so that you can use predefined methods.

(13)

a) Go to the Java Library for lists:

https://docs.oracle.com/javase/8/docs/api/java/util/List.html. Scroll down until you see the following summary of methods:

b) Change yourpracticeWithListsmethod so that it adds the value ’8’ at the third position in your list. Tip: there is a method called add( int index, E element ) which adds the element E at positionindex.

c) Call yourprintNumberListto test your code.

d) Match the Java Library methods to the description in the following table:

Method call Effect

removes an element from a list at a particularindex returns the third element in the list

adds an element to the front of the list adds an element to the end of the list

returns how many element a list has (i.e. how long it is) removes the last element in the list

checks if the list is empty

checks if the list exists (does thelistOfNumbersvariable point to a list?) e) Add the following code to thepracticeWithListsmethod:

i. Add the value 6 to the end of the list.

ii. Call yourprintNumberListto print the list.

iii. Print the index of the ’8’.

iv. Remove the fifth value in the list.

v. Test your method by printing the list after each modification.

vi. What happens if you try to remove a number that isn’t in the list?

Please read Theory 6.5: Surprise eggs.

Challenge 6.3: Print coordinates and values of all eggs in a list

In the following challenge you will write several methods to print data stored in lists.

a) Create a list of surprise eggs:

i. Come up with an appropriate name for this method.

(14)

ii. Use the generateListOfSurpriseEggsmethod to make a list of 10 surprise eggs. See also Theory 6.5.

b) Print the coordinates of the first element in the list:

i. Write a sub-method to print the coordinates of an egg: void printCoordinatesOfEgg(Egg egg). This can be very useful in other parts of the program (as you will see later) or while debugging.

• Instead of restricting this method to surprise eggs only, we can make it slightly more general by defining it forEggobjects. In that case, it can also be used for blue and golden eggs.

• Pass the egg to be printed as a parameter, and add this method toMyDodo. Tip: With egg.getX( )andegg.getY( )you can get the coordinates of an egg (with the nameegg).

See Theory 6.2.

• Test your sub-method by calling the method and then clicking on an egg.

• Ensure that coordinates are printed as follows: (3,4)

ii. Write a method which prints the coordinates of the first surprise egg in a given list:

printCoordinatesOfFirstSurpriseEgg( List<SurpriseEgg> mySurpriseEggList ).

• Get the first egg in the list (the list is given as a parameter) and print it using the printCoordinatesOfEgg( )sub-method you just wrote.

• To test your sub-method, write a method practicePrintingLists( ) which creates a list of surprise eggs, and then calls yourprintCoordinatesOfFirstSurpriseEgg()sub- method:

/**

* Method for practicing with printing data in lists public void*/ practicePrintingLists( ){

List<SurpriseEgg> surpriseEggList =

SurpriseEgg.generateListOfSurpriseEggs( 10, getWorld( ) );

printCoordinatesOfFirstSurpriseEgg( surpriseEggList );

}

c) Print the coordinates of all the eggs in the list:

i. Write a methodvoid printCoordinatesOfSurpriseEggList(List<SurpriseEgg> surpriseEggList )which prints the coordinates of each egg in the list.

ii. Use afor-each-loop traverse the eggs in a given list of surprise eggs.

iii. For each egg, print its coordinates to the console (re-use your own sub-method for this).

(15)

iv. Call your sub-method inpracticePrintingListsfor testing.

d) Print the values of all the elements in the list: For each surprise egg, get its value and print it in the console. Again, write a sub-method and call it inpracticePrintingListsfor testing.

Challenge 6.4: Which is is the most valuable?

Write a method which prints the value of the most valuable (surprise) egg in the world.

a) Fill the world with 10 surprise eggs.

b) Come up with an algorithm to find (and return) the egg in the list that has the highest value. Tip:

Use theMin/Max plan as explained in Theory 5.2.

c) Implement this algorithm.

d) Test your code, by calling it in yourpracticePrintingListsmethod. Tip: For testing purposes, first print the values of all the eggs in the surprise eggs list.

e) Is the result correct? Execute the code several more times and check if the correct value is found each time. Fix any errors before you continue onto the next challenge.

Challenge 6.5: Where is the most valuable egg?

Your mission is:

”Print the coordinates of the most valuable egg in a list.”

It may be possible for two eggs to have the maximum value. Come up with an appropriate strategy to deal with this case. Add appropriate (JavaDoc) comments to your code

Please read Theory 6.6: Average loop plan.

Challenge 6.6: Print average value of list

Your mission is:

”Get the average of the egg-values in a list.”

Tips:

• The Average Loop Plan is described in Theory 6.6;

• Theory 5.3 describes how to cast anintvalue to a (decimal)doublevalue.

• Also test your method for an empty list (generate a list of 0 eggs using:

generateListOfSurpriseEggs(0, getWorld( )). Does your program deal with this situation ap- propriately?

(16)

Challenge 6.7: Reversing a list

Write a method that turns the list of surprise eggs around. That is to say, reorganizes the list so that the last element is put in the front, followed by the second to last, etc.

a) Come up with a name for your method.

b) Generate a new list of 10 surprise eggs, and print their values.

c) Come up with an algorithm to reorder the elements in the list accordingly. Tips:

i. You already learned how to switch two values in theory 4.4, the ’The Swap Plan’.

ii. Have a look at the different methods available to you in the Java Library Documentation (such asadd,get,set,remove). Choose which one(s) you want to use in your algorithm.

d) First implement a small part of the algorithm and test this:

i. Write a method

swapElements(List<Integer> listOfNumbers,int indexFirstElement,int indexSecondElement )which swaps two elements in a list of integers. The method should use the List methods getandset.

ii. Start by swapping the first and last element.

iii. Print the list again.

iv. Test this. Also check if, in the end, the list still has just as many elements (size).

e) Think about the rest of the algorithm, without implementing it:

• Place 5 items in front of you. Organise them from smallest to largest. Mark their indexes (see the figure above). Then use your algorithm to sort them (thus by only swapping) from largest to smallest.

• After the first swap, which are the next two elements that must be swapped? Name the elements using their indexes.

• How many swaps do you need all together?

• Test your algorithm with 6 (an even number of) items.

f ) Now, implement the algorithm to turn the whole list around. Tips:

• Because you want to change the list within a loop, use awhile-loop. Don’t use afor-each- loop).

• Use a variable called eggIndexto keep track of where you are in the list. Don’t forget to increment this variable in thewhileloop.

g) Compile and test your code by printing the values of each element in the list.

(17)

h) Does your program work as you expect it to? If not, think of why it doesn’t work properly. To help debugging, print the list of values throughout the code to figure out what happens. Fix your program.

i) Does your program also work if there are 11 eggs in your list? And also if there are 0?

Please read Theory 6.7: Selection Sort.

Challenge 6.8: Sort a list with Selection Sort

Write a method which sorts a list of numbers using selection sort (see Theory 6.7). Tips:

• Write a sub-method which returns the position of the smallest value in a part of the list (for the unsorted part of the list).

• Use awhileloop

• Make use of yourswapElementsmethod that you wrote in Challenge 6.7.

Answer the following questions for your Selection Sort algorithm.

a) If you have a list of four numbers, how often are two values compared?

b) For a list with five values, how many comparisons are mare?

c) For a list which is twice as big (having ten numbers), how often are two values compared?

d) For a list which is ten times as big, how often are two values compared?

e) Can you write a general formula to calculate the number of comparisons needed for a list withn elements?

Challenge 6.9: Calculate distance

Help Mimi figure out how many steps it takes to get from where she is to any place she wishes to go to.

Write a methodint determineDistance( int x_coord, int y_coord )which calculates the number of steps (or moves) from the current location to another. Note:

• You don’t have to use Pythagoras’s theorem to calculate the distance, just count the number of moves she will need to take.

• If you need to turn a negative number into a positive number, the methodMath.abs( number ) will return the absolute value of any given number.

Challenge 6.10: Goto the most distant egg in the world

Help Mimi to determine which egg requires the most steps to get to, and walk to that egg. Break your algorithm down into several logical modules. Program new methods, re-use existing methods, and test each individually. Tip: check the coordinates of an egg by ’inspecting’ it.

(18)

Challenge 6.11: Re-factoring

An important part of programming is creating code that is made up of logical modules. Amongst other things, this makes re-using and testing so much easier.

Have a look back at the code you wrote in the previous Challenge 6.10 for ”Goto the most distant egg in the world”. Are there any fragments of code in your method that could have been written as a separate sub-method?

If you have not written a separate method to determine the distance from Mimi’s position to another location, do that now. Test your sub-method. Replace the code in your method for going to the most distant egg (Challenge 6.10) by your sub-methoddetermineDistance.

(19)

Reflection

In this assignment you practiced using list to store values. You also learned about the for-each-loop to traverse lists and how to use a list to store (Egg) objects. One of the most important steps in becoming good at anything is to evaluate and reflect on what you did and how it went:

Result

I know my solution works because . . . I am proud of my solution because . . . I could improve my solution by . . .

Method

My approach was good because . . . What I could do better next time is . . .

Fill the following table with smileys

indicating how things went. I can do it

I did it a bit but didn’t fully get it

I didn’t get it at all

(20)

I can use lists to store either primitive types or object types.

I can use afor-each-loop to walk through a list.

I can use the Java Library Documentation to look for and use existing Java methods.

I can swap elements in a list.

I can sort objects in a list.

I can calculating the average of a list of values.

I can break a complex algorithm down into subtasks and implement and test these separately.

(21)

Saving and Handing in

You have just finished the assignment. Save your work! You will need this for future assignments. In the Greenfoot menu at the top of the screen, select ’Scenario’ and then ’Save’. You now have all the scenario components in one folder. The folder has the name you chose when you selected ’Save As ...’.

Handing in

Hand in the following:

• Your code: The java fileMyDodo.jav;

• Flowcharts: paste (photo’s of) your flowcharts in a Word document;

• The reflection sheet: complete and hand it in.

Referenties

GERELATEERDE DOCUMENTEN

To truly make a good system one must, aside from the sodium uptake of the plant and relative growth rate, study the evapotranspiration, root depths, preferred soil type, and shape of

Here’s an \ac acronym Circular Restricted Three Body Problem (CRTBP), followed by some random text, but Hide the pages for One of These Abbreviation Systems; this is from

Which company or companies do you know that have been using Human Resource outsourcing services: please mark your choice. Shell P&amp;G HP Nokia All

The standard propositional account of necessary and sufficient conditions in many in- troductory logic textbooks is based on the material conditional.. In the appendix,

d) When calculating the average, type cast the variable for the total number of eggs found to a double (a decimal value).. e) Test your method. Pay careful attention to eggs in the

c) When calculating the average, type cast the variable for the total number of eggs found to a double (a decimal value).. d) Test your method. Pay careful attention to eggs in

The choice for primary classes must be based on the number of predictor variables included in regression analysis.. The rule of thumb for

I don't know why turkey eggs are not usually available for culinary purposes, but I suggest that if you do come across them, 1.. Charles Manners, fourth Duke of Rutland, an