IBCS1 - 1112 - May

From WLCS

Thursday (5/31/12)

Tuesday (5/21/12)

Thursday (5/17/12)

Friday - Tuesday (5/11/12 - 5/15/12)

Agenda:

Monday - Wednesday (5/7/12 - 5/9/12)

  • Methods & functions in Java
    1. Do you want the method/function to be usable by other files?
      • Yes - public
      • No - private
    2. static (just put it there)
    3. Do you want the method/function to return anything?
      • Yes - put the return type (e.g. int, double, String, etc.)
      • No - void
    4. What do you want the name of the method/function to be?
      • Must be all one word, no spaces (follows same rules as variable names)
    5. Do you want the method/function to have any input parameters?
      • No - just have empty parentheses
      • Yes - declare the variable parameters and their types in the parentheses
    • Example template: public/private static RETURNTYPE FUNCTIONNAME(PARAMETERS)
    • Example: public static int functionName(int x, int y)
  • Java Functions Practice
  1. int compare(int a, int b)
    • Define a compare(int a, int b) function that returns 1 if a > b, 0 if a == b, and -1 if a < b
    • Test your compare function with 3 different function calls:
      • compare(5, 4)
      • compare(7, 7)
      • compare(2, 3)
  2. double hypotenuse(double a, double b)
    • Define a hypotenuse(double a, double b) function that returns the length of the hypotenuse of a right triangle given the lengths of legs a and b
    • Test your hypotenuse function with the following function calls:
      • hypotenuse(3, 4)
      • hypotenuse(12, 5)
      • hypotenuse(7, 24)
  3. double f2c(double t)
    • Define a function named f2c(double t) that converts the temperature parameter t from Fahrenheit to Celsius and returns it.
    • If you don't remember the formula to convert, then look for it online!
    • Create your own function calls that test the function
  4. double c2f(double t)
    • Define a function named c2f(double t) that converts the temperature parameter t from Celsius to Fahrenheit and returns it.
    • If you don't remember the formula to convert, then look for it online!
    • Create your own function calls that test the function

Tuesday - Thursday (5/1/12 - 5/2/12)