CSE 131 Module 5: Modular Design

Practice Problems


Directions: Suppose you are given three classes, Widget, Machine, and Truck. Widgets have a color and a weight, and also are sometimes defective.

Machines can transform one widget into another to do things to them, like change their weight. Some machines can make a widget become defective, or can fix a defective widget so it is no longer defective. Sometimes machines are broken.

Trucks can be loaded with widgets, one at a time. Widgets are loaded and unloaded at the back of the truck, so the widget that gets loaded first will be the last widget to be unloaded. For example, suppose you load widgets a, b, and c into a truck. If you then unload them, they will come out in the order c, b, and a. If a truck is full, you can't load any more widgets into it. If a truck is empty, you can't unload any widgets from it.

Widget Machine Truck
  • Widget()
  • boolean isDefective()
  • double getWeight()
  • Machine()
  • Widget transform(Widget w)
  • boolean isBroken()
  • Truck()
  • void load(Widget w)
  • Widget unload()
  • boolean isEmpty()
  • boolean isFull()
  • double getTotalWidgetWeight()
  • Using the methods and constructors summarized above, provide a Java code fragment to perform each of the following tasks.

    1. Unload a single widget from truck t1, let machine m transform it, and put the resulting widget into truck t2.
    2. Unload a single widget from truck t1. If the widget is defective, let machine m transform it, and put the resulting widget into truck t2. Otherwise, just put the widget directly into truck t2.
    3. Unload widgets from truck t1. As you do so, put them into truck t2 unless t2 is full. Any widgets that aren't put into t2 should remain in t1. (Use a loop.)
    4. Unload all the widgets from truck t1. As you do so, put them into truck t2 unless adding a widget would make the total weight of the items in t2 exceed 1000. (Assume that t2 is initially empty.)
    5. Put widget w through machines m1, m2, and m3 in that order. (Think of this as function composition. You can write this as a single line of code. Be careful about the order of execution.)
    6. Unload all the widgets from truck t1, and sort them into three different trucks. All defective widgets should go into truck defectiveWidgets. Non-defective widgets that weigh less than 50 should go into truck lightWidgets. All remaining widgets go into truck heavyWidgets. Assume there is enough room in all of the trucks.
    7. Create 100 widgets, and put each one into a different new truck.
    8. Reverse the order of the widgets in truck t1. In otherwords, after you are finished, t1 will contain the same widgets as it did at the start, but the items will be loaded into the truck in the reverse order. (Hint: You may need some extra trucks, and you may want to write a short method to help simplify your code. Assume that any new trucks you create are sufficiently large.)
    9. Repeately put widget w through machines m1 and m2 until either the widget is defective or you are about to put w into a defective machine.
    10. Machine m can fix defective widgets, but sometimes a widget must be transformed multiple times by m before it is no longer defective. Unload all the widgets from truck t1. As you do so, put them into truck t2, but first let m fix any defective widgets. Assume t2 is sufficiently large.

      Note that this week's quiz will have two parts. The first part is based on the first ten practice problems above. The second part counts as an optional extension on the topic of test-driven development, as described in Lab 4. You will be asked to show the stages in test-driven development that might arise in the development of a particular method, similar to the following practice problem. Each part of the quiz will be graded separately. A grade of 85% or higher on the test-driven development portion will give you credit for the optional extension.

    11. Complete the following table showing the stages of development of a method using test-driven development. In each row, the developer's solution should be the simplest general solution that fits the test cases.

      TesterDeveloper
      @Test
      public void testFoo() {
         Widget w = new Widget();
         assertEquals(0, w.foo(7));
      }
      
      
      
      @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 = Math.max(v,n);
            return old;
         }
      }
      

    Solutions