# CS131 Module 5: Solutions to Practice Problems

1. ```Widget w = t1.unload();
w = m.transform(w);
```
or, alternatively:
```t2.load(m.transform(t1.unload()));
```

2. ```Widget w = t1.unload();
if (w.isDefective())
else
```

3. ```while (!t1.isEmpty() && !t2.isFull())
```

4. ```while (!t1.isEmpty()) {
if (w.getWeight() + t2.getTotalWidgetWeight() <= 1000)
}
```

5. ```m3.transform(m2.transform(m1.transform(w)));
```

6. ```while (!t1.isEmpty()) {
if (w.isDefective())
else if (w.getWeight() < 50)
else
}
```

7. ```for (int i = 0; i < 100; i++)
```

8. ```Truck t2 = new Truck();
Truck t3 = new Truck();
moveAll(t1, t2);  // t2 will have them in reverse order
moveAll(t2, t3);  // t3 will have them in the original order
moveAll(t3, t1);  // t1 will have them in reverse order

void moveAll(Truck a, Truck b) {
while (!a.isEmpty())
}
```

9. ```boolean done = false;
while (!done) {
if (m1.isBroken || w.isDefective())
done = true;
else {
w = m1.transform(w);
if (m2.isBroken() || w.isDefective())
done = true;
else
w = m2.transform(w);
}
}
```

10. ```while (!t1.isEmpty()) {
while (w.isDefective())
w = m.transform(w);
}
```

11. (for the test-driven development optional extension) Answers may vary, but the test must force the developer implementation in each row. That is, the developer should not assume semantics beyond those required by the tests. The goal of the developer is to provide the simplest implementation that will pass the tests.

TesterDeveloper
```@Test
public void testFoo() {
Widget w = new Widget();
assertEquals(0, w.foo(7));
}
```
```public class Widget {
public int foo(int n) {
return 0;
}
}
```
```@Test
public void testFoo() {
Widget w = new Widget();
assertEquals(0, w.foo(7));
assertEquals(7, w.foo(3));
}
```
```public class Widget {
int v = 0;
public int foo(int n) {
int old = v;
v = n;
return old;
}
}
```
```@Test
public void testFoo() {
Widget w = new Widget();
assertEquals(0, w.foo(7));
assertEquals(7, w.foo(3));
assertEquals(7, w.foo(10));
}

```
```public class Widget {
int v = 0;
public int foo(int n) {
int old = v;
v = Math.max(v,n);
return old;
}
}
```