Organizing Your Code With Methods

The guide to everything controls for 1675 members.

Organizing Your Code With Methods

Definitions

Exercise #1

Exercise #2

Exercise #3

Bonus Exercise

 public final class Methods {

  public static double checkIfNumberIsGreater(double x, double y) {
    System.out.println("---------Cool Number Thing----------\n");
    // Fill in your method here
    
    // Think back to if/else statements. Call someone over if you need help.
    
    // Create code to check if x is greater than y, and if true, 
    // add 2 to x (x = x + 2), then print out "x is greater than y"
    
    // Then create code that adds 1 to y if it is greater than x,
    // and then print out "x is NOT greater than y"
    
    double z = 0;
    // Then set z equal to the sum of x and y
    System.out.println("The sum of x and y is: " + z + "\n");
    return z; 
  }

  public static void main(String... args) {
    double a = 9.4;
    double b = 8.7;

    // Replace this line with a call to checkIfNumberIsGreater() and pass a and b as parameters.
    // Then save the result in a double named c.

    a += c;
    b = b + (2.5 * c);

    // Call the method checkIfNumberIsGreater() here and pass a and b as parameters again.
  }
}