Purpose:
You may make a copy of a worksheet and complete this activity, or simply type your answers in any text editor.
You may work alone or with at most two other students in this course..
For this activity, we will create graph models representing the following Java methods. We will stop periodically, and solve the problem one step at a time with discussion between steps.
Alternatively, you may transform the "If You're Happy and You Know It" song [https://www.youtube.com/watch?v=Im5i7EqZE1A] into an IfYouAreHappy() method using any program control structures and then generate a Control Flow Graph (CFG) representing the method.
Consider the following Java method
public static String IfYouAreHappy_v1(boolean happy, boolean know)
{
String result = "";
if (happy && know)
result += "Clap-Your-Hands ";
if (happy && know)
result += "Stomp-Your-Feet ";
if (happy && know)
result += "Shout-Hooray ";
return result;
}
Draw a Control Flow Graph (CFG) for the IfYouAreHappy_v1() method. Annotate all information (i.e., source code).
Consider the following Java method
public static String IfYouAreHappy_v2(boolean happy, boolean know)
{
List<String> actions = new ArrayList<>(Arrays.asList("Clap-Your-Hands", "Stomp-Your-Feet", "Shout-Hooray"));
String result = "";
for (int i=0; i<actions.size(); i++)
{
if (happy && know)
result += actions.get(i) + " ";
}
return result;
}
Draw a Control Flow Graph (CFG) for the IfYouAreHappy_v2() method. Annotate all information (i.e., source code).
Consider the following Java method
public static String IfYouAreHappy_v3(boolean happy, boolean know, List<String> actions)
{
String result = "";
for (int i=0; i<actions.size(); i++)
{
if (happy && know)
result += actions.get(i) + " ";
}
return result;
}
Draw a Control Flow Graph (CFG) for the IfYouAreHappy_v3() method. Annotate all information (i.e., source code).
Consider the graphs from parts 1, 2, and 3. Reflect on how the graphs are generated for the different program structures and how the graphs represent the execution flows of the programs.
| Test requirements | Test paths | Test cases (input values and expected output) |
| Test requirements | Test paths | Test cases (input values and expected output) |
| Test requirements | Test paths | Test cases (input values and expected output) |
| Test requirements | Test paths | Test cases (input values and expected output) |
CC-BY-NC-SA 4.0 license.