teaching machines

CS1: Lecture 8 – Methods

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

Dear students,

Last time we distinguished between syntax (the grammatical rules) and semantics (the meanings) that guide our programs. When we invoke a method on an object—as in object.method()—we must know or learn the semantics of method if we want our program to do the right thing.

With semantics in mind, let’s look at one more What’s Wrong with This?

Scanner in = new Scanner(System.in);

System.out.print("# of repetitions: ");
int i = in.nextInt();

System.out.print("Line to be repeated: ");
String line = in.nextLine();

String repeated = line.repeat(i);
System.out.println(repeated);

Scanner

Mixing nextLine and nextInt (or the other single token next methods) can cause trouble if you don’t know the underlying semantics of these methods. Suppose we enter this input into the program above:

5¶
foo¶

When the Scanner first starts up, it positions a “cursor” at the beginning of the input.

|5¶
foo¶

On calling nextInt, the Scanner advances the cursor along until it hits the first whitespace character. It consumes the digits, but not the whitespace. Thus after the call, the cursor is sitting here:

5|¶
foo¶

On calling nextLine, the Scanner advances the cursor along until it hits the first linebreak, which it does immediately. It returns the empty string and consumes the linebreak:

|foo¶

The text "foo" is never read.

If you are mixing token- and line-oriented input, you may need to insert a call to nextLine to advance the cursor. None of the programs in homework 1 require you to perform line-oriented input, so you shouldn’t need to do this in your code. However, some of you are seeing this weird behavior in lab.

Random

Let’s a new data type to our repertoire: a random number generator. If I tell you that the name of this type is Random, could you create a new variable that is capable of generating random numbers?

You should be able to. We can tell by its capitalization that it is a class, rather than a primitive, and its should be declared and assigned like this:

Random generator = new Random();

Okay, we have a random number generator. Now how do we use it? In particular, how do we find information on to use it? I can think of a few ways:

  1. Search the web for java CLASSNAME.
  2. Create a variable of the desired type, then use IntelliJ’s autocompletion to see what methods are available.
  3. Click inside the name of a class in IntelliJ, and click View / External Documentation to bring up the documentation. This only works if you’ve added the URL to the documentation in File / Project Structure / Platform Settings / SDKs / Documentation Paths.

We will use Random many times this semester, because random behavior adds an element of surprise to otherwise boring programs. But now we move on to the next big idea of this class. We enter the Computer as Chef chapter of our lives together.

Computer as Chef

Sometimes we write code that we want to execute again and again. Let’s see an example of this. We will generate some random circle art using the SVG protocol. An outlined circle looks like this in SVG:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle cx="CENTERX" cy="CENTERY" r="RADIUS" stroke="black" stroke-width="2" fill="rgb(RED, GREEN, BLUE)"/>
</svg>

The capitalized words are just placeholders. The first three are in pixel units, and the last three are in [0, 255]. Let’s start by filling them in with random values. We’ll use the Random class this time, because unlike Math.random, it can generate ints for us.

System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
    
Random generator = new Random();
int originX = generator.nextInt(1000);
int originY = generator.nextInt(800);
int radius = generator.nextInt(600);
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);

System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);

System.out.println("</svg>");

Once we get that working, let’s add a second random circle. And a third. And a fourth. And so on.

System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
Random generator = new Random();

// First circle
int originX = generator.nextInt(1000);
int originY = generator.nextInt(800);
int radius = generator.nextInt(600);
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);

// Second circle
originX = generator.nextInt(1000);
originY = generator.nextInt(800);
radius = generator.nextInt(600);
r = generator.nextInt(256);
g = generator.nextInt(256);
b = generator.nextInt(256);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);

// ...

System.out.println("</svg>");

We’ll find that the code quickly gets away from us if we simply copy and paste. That leads us to…

Decree 9: Code can be named.

Instead of repeating ourselves, we factor out the repeated code to a method. For the time being, our programs with multiple methods will have this structure:

public class ClassWithMultipleMethods {
  public static void main(String[] args) {
    helperMethodA();
    helperMethodB();
  }

  public static void helperMethodA() {
    // ...
  }

  public static void helperMethodB() {
    // ...
  }
}

Let’s make a method that generates a random circle and call it a bunch of times! We’ll move the code outside of main into a helper method. That helper method will have a very narrow purpose: generate one circle. Then in main, we’ll call our own method a bunch of times.

public class Circles {
  public static void main(String[] args) {
    System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    System.out.println("</svg>");
  }

  public static void generateCircle() {
    Random generator = new Random();
    int originX = generator.nextInt(1000);
    int originY = generator.nextInt(800);
    int radius = generator.nextInt(600);
    int r = generator.nextInt(256);
    int g = generator.nextInt(256);
    int b = generator.nextInt(256);
    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
  }

This is much better. There’s a mantra in software development that’s worth repeating here: don’t repeat yourself.

One thing that’s less than awesome, however, is that every time we invoke generateCircle, we create a brand new Random. That’s wasteful. The designers of Java anticipated this and established this rule:

Decree 10: Code can have its own data, or it can borrow data from its caller.

Borrowed data comes to a method in the way of parameters.

Parameters

Let’s seems smarter for us to just create one up front, and then share it. But this brings in an issue of variable scope. We’ll solve it using parameters.

Parameters are variable declarations. They declare data that the method does not have. If we rewrite generateCircle to not have a Random, we have this:

public static void generateCircle() {
  int originX = generator.nextInt(1000);
  int originY = generator.nextInt(800);
  int radius = generator.nextInt(600);
  int r = generator.nextInt(256);
  int g = generator.nextInt(256);
  int b = generator.nextInt(256);
  System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
}

This code complains of an unknown generator. To declare it as coming from the outside world, we add it as a parameter:

public static void generateCircle(Random generator) {
  int originX = generator.nextInt(1000);
  int originY = generator.nextInt(800);
  int radius = generator.nextInt(600);
  int r = generator.nextInt(256);
  int g = generator.nextInt(256);
  int b = generator.nextInt(256);
  System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
}

Now, when we call generateCircle back in main, we must supply something that fits the Random type. We could do this:

generateCircle(new Random());
generateCircle(new Random());
generateCircle(new Random());
generateCircle(new Random());

But that would be silly. Let’s make a single, reusable Random:

Random lucky = new Random();
generateCircle(lucky);
generateCircle(lucky);
generateCircle(lucky);
generateCircle(lucky);

TODO

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

See you next time!

Sincerely,

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

Named things live longer
That’s why I named my goldfish
Hope, Les, Lee, Slim, Chance

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

Foofoofoofoofoo.java

package lecture0920.cs145;

import java.util.Scanner;

public class Foofoofoofoofoo {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("# of repetitions: ");
    int i = in.nextInt();
    in.nextLine();

    System.out.print("Line to be repeated: ");
    String line = in.nextLine();

    String repeated = line.repeat(i);
    System.out.println(repeated);
  }
}

Circler.java

package lecture0920.cs145;

import java.util.Random;

public class Circler {
  public static void main(String[] args) {
    System.out.printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">%n");

    Random lucky = new Random();

    generateCircle(lucky);
    generateCircle(lucky);
    generateCircle(lucky);
    generateCircle(lucky);

    System.out.printf("</svg>%n");
  }

  public static void generateCircle(Random lucky) {
    int centerX = lucky.nextInt(2000);
    int centerY = lucky.nextInt(1100);
    int radius = lucky.nextInt(100);

    int r = lucky.nextInt(56) + 200;
    int g = lucky.nextInt(101);
    int b = lucky.nextInt(101);

    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"0\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
  }
}

Foofoofoofoo.java

package lecture0920.cs148;

import java.util.Scanner;

public class Foofoofoofoo {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("# of repetitions: ");
    int i = in.nextInt();
    in.nextLine();

    System.out.print("Line to be repeated: ");
    String line = in.nextLine();

    String repeated = line.repeat(i);
    System.out.println(repeated);
  }
}

Circler.java

package lecture0920.cs148;

import java.util.Random;

public class Circler {
  public static void main(String[] args) {
    System.out.printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">%n");

    Random randall = new Random();
    for (int i = 0; i < 4000; ++i) {
      generateCircle(randall);
    }

    System.out.printf("</svg>%n");
  }

  public static void generateCircle(Random generator) {
    int centerX = generator.nextInt(1920);
    int centerY = generator.nextInt(1080);
    int radius = generator.nextInt(100);
    int r;
    int g = generator.nextInt(40);
    int b;
    if (generator.nextBoolean()) {
      r = generator.nextInt(200) + 55;
      b = 255 - r;
    } else {
      b = generator.nextInt(200) + 55;
      r = 255 - b;
    }
    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
  }
}

Utilities.java

package lecture0920.cs148;

import java.util.Random;

public class Utilities {
  public static void generateCircle() {
    Random randall = new Random();
    int centerX = randall.nextInt(1920);
    int centerY = randall.nextInt(1080);
    int radius = randall.nextInt(100);
    int r = randall.nextInt(256);
    int g = randall.nextInt(256);
    int b = randall.nextInt(256);
    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
  }
}