CS 1073 Introductory Programming
for Scientific Applications
Practice with Functions: Answers


Overview: This page gives answers to the Practice with Functions questions. The additions and changes needed will be given in red. In each case you should actually try out your solution in NetBeans before looking at my answer.


First Starting Point: The function min: :


// Min: test a min function
public class Min {

   public static int min(int x, int y) {
      if (x < y) return x;
      else return y;
   }

public static void main(String[] args) {
       System.out.println(min(13, 47));
   }
}
Output: 13

Practice Exercises:

  1. Add a function max very similar to min. Test it in the main function using System.out.println(max(13, 47));

    
    // MaxMin: test a max and min functions
    public class MaxMin {
    
       public static int min(int x, int y) {
          if (x < y) return x;
          else return y;
       }
    
       public static int max(int x, int y) {
          if (x > y) return x;
          else return y;
       }
    
       public static void main(String[] args) {
           System.out.println(min(13, 47));
           System.out.println(max(13, 47));
       }
    }
    
    Output:
    13
    47

  2. Write a function min with three int parameters. You can use the same name min because the system will keep the one with three parameters separate from the one with two parameters. Here's how you should write the version of min: Suppose the parameters are named x, y, and z. (Remember, these are formal parameters and we can give them any name we want.) First use the old min to take the minimum of x and y. Save the result in a new variable w. Now use the old minimum to find the minimum of w and z, and return this.

// Min3: test a max and min functions
public class Min3 {

   public static int min(int x, int y) {
      if (x < y) return x;
      else return y;
   }

   // using min with two parameters
   public static int min(int x, int y, int z) {
      int w = min(x, y);
      return min(w, z);
   }

   public static void main(String[] args) {
      System.out.println(min(13, 47, 96));
      System.out.println(min(96, 13, 47));
      System.out.println(min(47, 96, 13));
   }
}
// Min3: test a max and min functions
public class Min3 {

   public static int min(int x, int y) {
      if (x < y) return x;
      else return y;
   }

   // tricky use of min with 2 parameters
   public static int min(int x, int y, int z) {
      return min(min(x, y), z);
   }

   public static void main(String[] args) {
      System.out.println(min(13, 47, 96));
      System.out.println(min(96, 13, 47));
      System.out.println(min(47, 96, 13));
   }
}
// Min3: test a max and min functions
public class Min3 {

   // written without other functions
   public static int min(int x, int y, int z) {
      int w = x;
      if (y < w) w = y;
      if (z < w) w = z;
      return w;
   }

   public static void main(String[] args) {
      System.out.println(min(13, 47, 96));
      System.out.println(min(96, 13, 47));
      System.out.println(min(47, 96, 13));
   }
}
Output:
13
13
13


Second Starting Point: : :


// Min: test a min function
public class Min {

   public static int min(int x, int y) {
      if (x < y) return x;
      else return y;
   }

public static void main(String[] args) {
       System.out.println(min(13, 47));
   }
}
 
 

Practice Exercises:

  1. Change the .

  2. Change the .