teaching machines

CS1: Lecture 9 – Methods with Return Values

September 23, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

Today we dive deeper into our discussion of writing our own methods. We’ve actually been using methods all semester long; it’s just that now we’re starting to write our own. Before we dive in, let’s revisit our friend String.

Stringo

With Strings at our disposal, our programs start to feel human. We can write programs that process and generate words, and that’s pretty amazing! But we need more practice using them before we determine the authenticity of Shakespeare’s works. Let’s play some Stringo!

Generate a String of five characters. Use lowercase letters, numbers, and punctuation—all your choice. Label each character with its index. We’ll refer to your String through identifier s.

I will call out a series of predicates that describe parts of s. If a predicate is true for your String cross off the characters involved in making the predicate true. Write the predicate number next to the characters that it crossed off. First one to cross off all five characters wins a meager prize and enduring fame. Here are the predicates:

 0: s.charAt(0) is equal to s.charAt(s.length() - 1)
 1: s.charAt(3) is a vowel
 2: s.indexOf('a') % 2 is 0
 3: s.charAt(3) >= 'm' and s.charAt(3) <= 'z'
 4: s.indexOf('f') <= 3
 5: s.charAt(i) is punctuation and s.charAt(i + 1) is a letter
 6: s.substring(3) is all numbers
 7: s.substring(2, 4).charAt(0) is not a letter
 8: s.indexOf('e') + 1 < 5
 9: s.indexOf('o') < s.indexOf('k')
10: s.charAt(1) does not equal s.charAt(4)
11: s.charAt(i) is equal to s.charAt(i + 1)
12: s.indexOf('t') >= 0
13: s.indexOf(s.charAt(2)) is 2
14: s.charAt(i % 2) is a consonant

Methods

Last time we saw how methods turn a sequence of code into a reusable component. Methods have some really nice advantages:

That insulation is our concern for today. Most programming languages have a notion of insulation, which leads us to our next decree.

Decree 11: Data has a lifetime and a “lifespace.”

When we declare a variable, it’s scope begins. That variable is only accessible until the closing curly brace of the block in which it was declared. Methods have independent scopes and are therefore insulated from each other. But if methods can’t access each other’s data, how do they share stuff across the Wall of Braces? Through two mechanisms: return values and parameters.

I like to think of methods as vending machines. From the perspective of a customer, I drop some things in (the parameters), and some unhealthy food pops out (the return value). What happens in between is largely a mystery to me.

We will be more than customers, however. We will design our own vending machines.

Let’s discuss return values first.

Return Values

We saw earlier that we can “mail” data to other code. Guess what? The other code can reply!

Decree 12: Named code can mail data back to its caller.

Mailing data back is called returning. To write a method that returns a result to the customer or client or caller of the method, we must do a couple of things:

  1. Announce the type of data that will be sent back. This is announced right before the method’s name, where we’ve been writing void up to this point.
  2. Add a statement that catapults the data back to the caller, clearing the Wall of Braces.

We’ll now start to see this pattern in our code:

public static RETURN-TYPE methodName() {
  ...
  return SOME-EXPRESSION-HAVING-RETURN-TYPE;
}

Let’s write a method that generates a random letter, perhaps used to assign someone to a group. We could write it like this:

public static char getRandomLetter() {
  Random generator = new Random();
  int ordinal = generator.nextInt(26);
  char letter = (char) ('A' + ordinal);
  return letter;
}

Or we could generate a random index into a String holding the alphabet:

public static char getRandomLetter() {
  Random generator = new Random();
  String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char letter = alphabet.charAt(generator.nextInt(26));
  return letter;
}

Parameters

Generally, we add parameters to let the caller influence the behavior of the method. In a vending machine, for example, we give the customer some choice of what will be dispensed. They might enter C7 to get Reese’s Peanut Butter Cups—which is the only unhealthy food worth eating.

We add parameters by declaring—but not assigning—variables in the method’s parentheses:

public static RETURN-TYPE methodName(TYPE1 id1, TYPE2 id2, ...) {
  ...
  return SOME-EXPRESSION-HAVING-RETURN-TYPE;
}

These variables are now available to the statements of the method. In the case of getRandomLetter, it’d be nice to generalize the method so that the user could decide alphabet to choose from. We do this like so:

public static char getRandomLetter(String alphabet) {
  Random generator = new Random();
  char letter = alphabet.charAt(generator.nextInt(alphabet.length()));
  return letter;
}

Now in the caller we must supply the value for alphabet:

System.out.println(getRandomLetter("ABCDEFGHIJKLMNPOQRSTUVWXYZ"));
System.out.println(getRandomLetter("AB"));
System.out.println(getRandomLetter("HT"));
System.out.println(getRandomLetter("HHHHHHHHHHHHHHHT"));

Let’s write another method, one that yields the number of digits in a number. What is the return type? int. What parameters must we accept? A single int. There are several ways to answer this. We’ll convert the number to a String:

public static int getDigitCount(int n) {
  String nAsString = "" + Math.abs(n);
  return n.length();
}

We could also have solved this with logarithms. Or a loop that chisels away at the number digit by digit until we get to 0.

TODO

Here’s your TODO list to complete before next class:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

A magic 8-ball
Without question, it answers
That’s some good magic

P.P.S. Here’s the code we wrote together in class…

Scope.java

package lecture0923.cs145;

public class Scope {
//  public static void methodA() {
//    int x = 10;
//  }
//
//  public static void methodB() {
//    System.out.println(x);
//  }
}

RandomLetter.java

package lecture0923.cs145;

import java.util.Random;

public class RandomLetter {
//  public static RETURN-TYPE name() {
//    ...
//    return SOME-EXPRESSION-OF-RETURN-TYPE;
//  }
//
//  public static int name() {
//    return 5;
//    return nPeople;
//    return generator.nextInt();
//  }

  public static char getRandomLetter(String alphabet) {
    Random lucky = new Random();
    int i = lucky.nextInt(alphabet.length());
    return alphabet.charAt(i);
  }

  public static char getRandomLetter() {
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Random lucky = new Random();
    int i = lucky.nextInt(alphabet.length());
    return alphabet.charAt(i);
  }

  public static char getRandomLetter2() {
    Random lucky = new Random();
    int i = lucky.nextInt(26);
    char c = (char) ('a' + i);
    return c;
  }

  public static void main(String[] args) {
    System.out.println(getRandomLetter("HT"));
    System.out.println(getRandomLetter("HTTTTTTTTTTTTTT"));
  }
}

RandomLetter.java

package lecture0923.cs148;

import java.util.Random;

public class RandomLetter {
//  public static RETURN-TYPE name() {
//    ...
//    return SOME-EXPRESSION-OF-RETURN-TYPE;
//    return 6;
//    return 6 * 6;
//    return generator.nextInt();
//  }

  public static char getRandomLetter(String alphabet) {
    Random rando = new Random();
    int i = rando.nextInt(alphabet.length());
    return alphabet.charAt(i);
  }

  public static char getRandomLetter() {
    Random rando = new Random();
    int i = rando.nextInt(26);
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return alphabet.charAt(i);
  }

  public static char getRandomLetter2() {
    Random rando = new Random();
    int i = rando.nextInt(26);
    char c = (char) (i + 'A');
    return c;
  }

  public static void main(String[] args) {
    for (int i = 0; i < 10; ++i) {
      System.out.println(getRandomLetter("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHT"));
    }
  }
}