SWE 637 Homework 2
Fall 2006
Graph Coverage: Due 9/19, beginning of class


Never test for a bug you don't know how to fix.
- Mike McCracken

Answer the following questions. Bring hardcopies of your answers to class, hand written or printouts. All homeworks are due before class on the due date. Please remember that the GMU Honor Code is in effect: You may discuss the assignment with each other, the professor, or the GTA, but each homework should be done individually.

Use the following class vendingMachine for the questions below. The complete program vendingMachine.java is available for your reference.


// A Java implementation of VendingMachine.
import java.util.*;
import java.io.*;

public class vendingMachine
{
private int credit;
private LinkedList stock;

// Maximum size of vendingMachine
private static final int MAX = 10;

//************************************************
// Constructor
// vendingmachine starts empty.
//************************************************
vendingMachine()
{
   credit = 0;
   stock  = new LinkedList(); // Empty stock.
}

//************************************************
// A coin is given to the vendingMachine.
// Must be a dime, quarter or dollar.
// Ignores invalid input
//************************************************
public void coin (int coin)
{
   if (coin != 10 && coin != 25 && coin != 100)
      return;
   if (credit >= 90)
      return;
   credit = credit + coin;
   return;
}

//************************************************
// User asks for a chocolate.
// Returns the change and the sets the
// parameter StringBuffer variable Choc.
// If not enough money or no chocolates,
// returns 0 and a blank string.
//************************************************
// Necessary because strings are immutable
// C++ version returned both choc and change as parameters
public int getChoc (StringBuffer choc)
{
   int change;

   if (credit < 90 || stock.size() <= 0)
   {
      change = 0;
      choc.replace (0, choc.length(), "");
      return (change);
   }
   change = credit - 90;
   credit = 0;

   choc.replace (0, choc.length(), (String) stock.removeFirst());

   return (change);
}

//************************************************
// Adds one new piece of chocolate to the machine
// If machine is full, nothing happens
//************************************************
public void addChoc (String choc)
{
   if (stock.size() >= MAX)
      return;
   stock.add (choc);
   return;
}

} // End class vendingMachine

  1. (4 pts) Draw the control flow graph for the four methods in vendingMachine (excluding the main() stub, that is, draw control flow graphs for vendingMachine(), coin(), getChoc(), and addChoc()).
  2. (10 pts) Enumerate the test requirements for Node Coverage, Edge Coverage, Edge-Pair Coverage and Prime Path Coverage for the graph for the four methods. If test requirements for some criteria are the same as for others, you do not need to copy them, just write "same as X".
  3. (2 pts) If possible, identify a set of test paths that achieves Node Coverage but not Edge Coverage on one of the graphs.
  4. (2 pts) If possible, identify a set of test paths that achieves Edge Coverage but not Prime Path Coverage on one of the graphs.
  5. (extra credit, 2 pts) Reimplement the vendingMachine class to more properly follow the concepts you learned in SWE 619.