Print Page Print Page

CH4 – Flow of Execution Part II

  1. Open DrJava and create a new class named SimpleFlow. Be certain to check the “Include main method” checkbox.
  2. Replace the /* ADD YOUR CODE HERE */ with the following four methods.
public static void firstMethod(String x){
  System.out.println(x);
}
public static void secondMethod(String x){
 System.out.println(x);
}
public static void thirdMethod(String x){
 System.out.println(x);
}
public static void fourthMethod(String x){
 System.out.println(x);
}
  1. Add code in each method setting the value of x and calling the next method, except for fourthMethod. Add a line printing the value of x to the end of each method.
public static void firstMethod(String x){
  x = "1";
  secondMethod(x);
  System.out.println(x);
}
public static void secondMethod(String x){
  x = "2";
  thirdMethod(x);
  System.out.println(x);
}
public static void thirdMethod(String x){
  x = "3";
  fourthMethod(x);
  System.out.println(x);
}
public static void fourthMethod(String x){
  System.out.println("in fourth method");
}
  1. Add code to the main method calling firstMethod.
public static void main(String[] args) { 
  String x = "A";
  firstMethod(x);
}
  1. Click the Compile button followed by the Run button.

Note the output to the Console.

3
3
2
1
A