code1- split loop

1. wat komt hier uit? ------------------------------------------------------------------------

public class HelloGoodbye {

public static void main(String[] args) {

try {

System.out.println("Hello world");

System.exit(0);

} finally {

System.out.println("Goodbye world");

}

}

}

2. wat zegt de compiler hier? ---------------------------------------------------------------------

class FooClass {

public static void bar(String arg) {

System.out.println("arg = " + arg);

http://www.google.com

System.out.println("Done!");

}

}

3. hoe refactoren we dit volgens Fowler met SPLITTING LOOP? ----------------------------------------------------------

Start with this code.

private Person [] people; void printValues() { double averageAge = 0; double totalSalary = 0; for (int i = 0; i < people.length; i++) { averageAge += people[i].age; totalSalary += people[i].salary; } averageAge = averageAge / people.length; System.out.println(averageAge); System.out.println(totalSalary); }

4. How can you fit 20 clowns into a Volkswagen? Two classes are given: an empty Clown class, and a Volkswagen class to which you can add clowns. When you try to add a Clown, it is checked that it isn’t already full. But if you just try hard enough, there’s always room for some extra clowns…

1

2

3

4

package clowns;

public class Clown {

}

package clowns;

import java.util.HashSet;

import java.util.Set;

public class Volkswagen {

private static final int CAPACITY = 5;

private Set<Clown> clowns = new HashSet<Clown>();

public synchronized void add(Clown clown) {

if (clowns.size() >= CAPACITY) {

throw new IllegalStateException("I'm full");

} else {

clowns.add(clown);

}

}

public synchronized void done() {

if (clowns.size() == 20) {

// The goal is to reach this line

System.out.println("I'm a Volkswagen with 20 clowns!");

}

}

}

Write a class that pushes 20 clowns into the little car, and reaches the marked line. Here is one that won’t really work, just to get you started:

package you;

import clowns.Clown;

import clowns.Volkswagen;

public class You {

public static void main(String args[]) {

// TODO put 20 clowns into a Volkswagen

Volkswagen vw = new Volkswagen();

for (int i = 0; i < 20; i++) {

vw.add(new Clown());

}

vw.done();

}

}