Bargains With Evil

From Huben's Wiki
Jump to: navigation, search

Betimes we must bargain with others, even with evil. In Java, these bargains are made with a fearsome form of contract, the method call. Method calls have arguments and return values. The arguments are what you give, and the return value is what you receive in exchange.

Contents

(A) Striking A Bargain

Compile and run these two classes in Dr. Java.

public class Innocent
{
  public static void main(String[] args)
  {
    String iAmGiving = "honor";
    String result = Evil.bargain(iAmGiving);
    System.out.println("Oh Woe!  All I have received is " + result);
    
    double sum = Evil.plus(2, 2.0);
    System.out.println(sum);
    System.out.println(Evil.plus(3, 4.5));
  }
}

and

public class Evil
{
  static public String bargain(String whatIsGiven)
  {
    System.out.println("Aha! I have received your " + whatIsGiven);
    String whatIsReturned = "despair";
    return whatIsReturned;
  }
  static public double plus(int a, double b)
  {
    return a + b;
  }
}
  1. Which class must you run? Why?

Read The Contract Carefully!

The first thing we must understand in these code examples is the idea of local variables. Local variables are declared within methods. Local variables can ONLY be used inside their own methods, and actually only within their own block and any inner blocks. This is a principle called scope. Arguments in method signatures are also local variables.

(The variables we have seen declared outside of methods are called fields. Fields can be used in any method. If they are public, then any method of any class can use them. If they are private, then only methods of their own class can use them. Demons do have secrets to keep, after all.)

A method call sets argument variables in the method. In the Innocent.main() call to Evil.bargain(), Evil.bargain()'s whatIsGiven argument is set to the String "honor".

Methods can return a single value, which can be placed in a variable, used, or ignored. A method returns a value using the return reserved word, as you see at the end of both methods of Evil. If the return type of a method is void, no value is returned and there is no need for a return statement. Evil.bargain() returns a String with the value "despair" to Innocent.main(), which places that value into the String named 'result'.

Arguments and returns can be any types or combination of types.

  1. What are the arguments to Evil.plus()?
  2. What is the return type of Evil.plus()?
  3. What is the argument to Innocent.main()?
  4. What are the three local variables in Innocent.main()?
  5. Why are the two arguments in the call to Evil.plus() different?
  6. In Evil.plus(), why can I add a and b together?
  7. What is the type of the result of a + b?

Create Your Own Contracts

  1. Add a method to Evil that has int[] as an argument which returns the sum of the first three elements in the array.
  2. In Innocent.main(), create a local variable array with at least 3 initialized elements (curly braces!)
  3. Add a call to your new Evil method using the array for the argument. Print the result.

Lawyer Up To Read Other Contracts

Seek ye the library class documentation that we spake of before..

  1. What are the argument types of Math.random()? And the return type?
  2. The same for Math.getExponent()?
  3. The same for String.endsWith()?
Personal tools
translate