Computer Science 15-110, Spring 2010
Class Notes:  Writing Static Methods


  1. void methods with no parameters
  2. void methods with one parameter
  3. void methods with multiple parameters
  4. int methods (Methods that return a value)
  5. Methods calling other methods
  6. Other return types (double, boolean, char, String)
  7. Local variables
  8. Parameter and Local variable scope
  9. Test methods

Writing Static Methods

  1. void methods with no parameters

    class MyCode {
      public static void doSomething() {
        System.out.println("Carpe diem");
      }


      public static void main(String[] args) {
        doSomething();
        doSomething();
      }
    }
     
  2. void methods with one parameter

    class MyCode {
      public static void printSquare(int x) {
        System.out.println(x + "^2 = " + (x*x));
      }

      public static void main(String[] args) {
        printSquare(2);
        printSquare(3);
      }
    }
     
  3. void methods with multiple parameters

    class MyCode {
      public static void printSum(int x, int y) {
        System.out.println(x + " + " + y + " = " + (x+y));
      }

      public static void main(String[] args) {
        printSum(2,3);
        printSum(3,4);
      }
    }

     
  4. int methods (Methods that return a value)

    class MyCode {
      public static int square(int n) {
        return n*n;
      }

      public static void main(String[] args) {
        System.out.println("3^2 = " + square(3));
        System.out.println("4^2 = " + square(4));
      }
    }
     
  5. Methods calling other methods

    class MyCode {
      public static int square(int n) {
        return n*n;
      }

      public static void printSquare(int x) {
        System.out.println(x + "^2 = " + square(x));
      }

      public static void main(String[] args) {
        printSquare(3);
        printSquare(4);
      }
    }
     
  6. Other return types (double, boolean, char, String)

    class MyCode {
      public static double cubeRoot(double d) {
        return Math.pow(d, 1.0/3.0);
      }

      public static boolean isPositive(int x) {
        return (x > 0);
      }

      public static char firstChar(String s) {
        return s.charAt(0);
      }

      public static String initials(String firstName, String lastName) {
        return "" + firstName.charAt(0) + lastName.charAt(0);
      }

      public static void main(String[] args) {
        System.out.println(cubeRoot(8));
        System.out.println(isPositive(8));
        System.out.println(isPositive(-8));
        System.out.println(firstChar("ABCD"));
        System.out.println(initials("Douglas","Adams"));
        System.out.println(firstChar(initials("Douglas","Adams")));
      }
    }
     
  7. Local variables
    class MyCode {
      public static int minSquared(int x, int y) {
        int min = Math.min(x, y);
        return (min * min);
      }

      public static void main(String[] args) {
        System.out.println(minSquared(3, 4));
        System.out.println(minSquared(4, 3));
      }
    }

    Another example:

    class MyCode {
      // This method computes the area in the first quadrant under
      // the line y=mx+b.  It assumes m is negative and b is positive,
      // so we in fact have a triangle in the first quadrant.
      public static int areaUnderLine(int m, int b) {
        // The x-intercept of y=mx+b is where y=0, so
        // mx+b=0, so x=-b/m
        int width = -b/m;
        // b is the y-intercept
        int height = b;
        // now we have the width and height of the right triangle
        // under the line in the first quadrant, so...
        return width*height/2;
      }

      public static void main(String[] args) {
        System.out.println("Area under y=-2x+8 is: " + areaUnderLine(-2,8));
      }
    }


    Yet another example:

    class MyCode {
      public static boolean isEvenPositive(int x) {
        boolean isEven = ((x % 2) == 0);
        boolean isPositive = (x > 0);
        return (isEven && isPositive);
      }

      public static void main(String[] args) {
        System.out.println(isEvenPositive(-2));
        System.out.println(isEvenPositive(-1));
        System.out.println(isEvenPositive(0));
        System.out.println(isEvenPositive(1));
        System.out.println(isEvenPositive(2));
      }
    }


    And yet another example:

    class MyCode {
      public static String initials(String firstName, String lastName) {
        char firstInitial = firstName.charAt(0);
        char lastInitial = lastName.charAt(0);
        return "" + firstInitial + lastInitial;
      }

      public static void main(String[] args) {
        System.out.println(initials("Donald","Knuth"));
      }
    }

     

  8. Parameter and Local variable scope

    class MyCode {
      public static int addOne(int x) {
        x = x + 1;
        System.out.println("in addOne, x = " + x);
        return x;
      }

      public static void main(String[] args) {
        int x = 5;
        System.out.println("in main, x = " + x);
        System.out.println(addOne(x));
        System.out.println("in main, x = " + x);
      }
    }


    Another example:

    class MyCode {
      public static void printN() {
        System.out.println(n);  // ERROR: n is not defined here!
      }

      public static void main(String[] args) {
        int n = 5;
        printN();
      }
    }


    Yet another example:

    class MyCode {
      public static void main(String[] args) {
        int n = 5;
        System.out.println(n);
        int n = 10;  // ERROR: n is already defined!
        System.out.println(n);
      }
    }

     
  9. Test methods

    Be sure you have enabled assertions before running this code!

    class MyCode {
      public static int minSquared(int x, int y) {
        int min = Math.min(x, y);
        return (min * min);
      }

      public static void testMinSquared() {
        System.out.print("Testing minSquared... ");
        assert(minSquared(2,3) == 4);
        assert(minSquared(3,2) == 4);
        System.out.println("Passed all tests!");
      }

      public static void main(String[] args) {
        // remember to enable assertions (with -ea flag)
        testMinSquared();
      }
    }


    Another example:

    class MyCode {
      public static boolean isOdd(int x) {
        return ((x % 2) == 1); // ERROR: This contains a bug!
      }

      public static void testIsOdd() {
        System.out.print("Testing isOdd... ");
        assert(!isOdd(2));
        assert(isOdd(1));
        assert(!isOdd(0));
        assert(isOdd(-1));  // ERROR: This assertion will fail!
        assert(!isOdd(-2));
        System.out.println("Passed all tests!");
      }

      public static void main(String[] args) {
        testIsOdd();
      }
    }

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem