teaching machines

CS1: Lecture 11 – More Methods

Dear students,

We’ll spend today continuing our discussion of methods—because we need practice. Java is a language, one invented by someone else, and we need to both speak it and hear it regularly in order to acquire it. Let’s start with an exercise in hearing Java, but with some parts inaudible.

Fill in the Blanks

Sometimes Java is criticized for being verbose. But one of the great advantages of verbosity is the reinforcement that it brings to an expression. We may read a passage and not know every word, but there are often many clues lying in the context to help us determine their meaning. Java has these same advantages. We can look at a method and figure out a lot of things from how a values are operated upon.

To gain some sensitivity for reading this context, let’s complete several fill-in-the-blank exercises. Your task is to jot what goes in each blank of the following methods. One of the blanks is the method name. Pick something meaningful. The other blanks are not ambiguous. You should be able to deduce exactly what goes there.

  • FIB #1
    public static _______ _______(int n) {
      ______ z = n + "";
      return z.startsWith("-");
    }
    
  • FIB #2
    public static _______ _______(______ ______) {
      return text + " :)";
    }
    
  • FIB #3
    public static _______ _______(______ ______) {
      System.out.printf("[%d]", foo);
    }
    
  • FIB #4
    public static _______ _______(______ ______) {
      return s.charAt(s.length() - 1);
    }
    
  • FIB #5
    public static _______ _______(int n) {
      _______ = "" + n;
      _______ = s.replace("0", ""); 
      _______ = s.length() - t.length();
      return d;
    }
    
  • FIB #6
    public static _______ _______(______ ________) {
      return g.nextInt(6) + g.nextInt(6) + 2;
    }
    

Blackbox

Let’s play a little game called Blackbox. After we figure out what these blackboxes do, we’ll implement them in Java.

  • Blackbox #6
  • Blackbox #7
  • Blackbox #8
  • Blackbox #9
  • Blackbox #10
  • Blackbox #11

TODO

Here’s your TODO list for next time:

  • I’ve posted dates for the two midterms. Check the syllabus for details. The best way to prepare is not magic: complete your homework, labs, and readings and go through the old exams. Click on classes at the top of this page and find past courses labeled CS 1 or CS 145.
  • If you haven’t pulled down the latest SpecChecker for homework 2 (as described in the Homework 0 – Updating SpecCheckers post), you are inviting ruin upon yourself.
  • Implement a method that computes something meaningful to you. What parameters does it need to get the job done? What does it return? What is a good name for your method? Write the method on a quarter sheet of paper to be turned in at the beginning of next lecture.

See you next class!

Sincerely,

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

Why is it method
Surely there’s a better name
But meth was taken

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

Frankenstring.java

package lecture0927.cs145;

public class Frankenstring {
  public static void main(String[] args) {
    System.out.println(frankenstring("eau claire", "nice"));
  }

  public static String frankenstring(String a, String b) {
    int i = a.length() / 2;
    int j = b.length() / 2;
    String monster = a.substring(0, i) + b.substring(j);
    return monster;
  }
}

Blackboxes.java

package lecture0927.cs148;

public class Blackboxes {
  public static void main(String[] args) {
    System.out.println(repeat('x', 'o', -3));
  }

  public static String repeat(char x, char o, int n) {
//    (("" + x + "" + o) * n) + x
    return ("" + x + o).repeat(Math.max(n, 0)) + x;
//    subject.repeat(n)
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *