teaching machines

CS 1: Lecture 10 – Blackboxes

September 27, 2017 by . Filed under cs1, cs145, cs148, fall 2017, lectures.

Dear students,

Let’s start with a little game called Blackbox!

Write on paper a method that takes 1+ parameters. For the body of the method, compute a simple value based on the parameters. Have it return the value. For example, I might write this mysterious method:
public static int mystery(int x) {
  int value = x + 1;
  return value;
}
Tell a neighbor what the parameter types and return type of your method are—without telling or showing them how your method is implemented. Neighbor, guess values of the parameters and try to figure out what’s going on inside the blackbox based on the returned values.

Now let’s do some together. After we figure out what they do, we’ll implement them in Java.

When we discuss methods, there are really two areas of concern: the interface and the implementation. The interface is the method’s “shape,” detailing the method’s name, it number and type of parameters, and its return type. The implementation is the actual instruction that make the method do its jobs. Callers of a method really only need to know the interface.

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

See you next class!

Sincerely,

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

I gave her my heart
She returned me a double
Now it’s twice as big

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

Self.java

package lecture0927;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class Self {
  public static void main(String[] args) throws AWTException {
    Robot ironGiant = new Robot();
    ironGiant.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    ironGiant.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  }
}

Blackboxes.java

package lecture0927;

import java.util.Random;
import java.util.Scanner;

public class Blackboxes {
  public static void main(String[] args) {
//    Scanner in = new Scanner(System.in);
//    int aaa = in.nextInt();
//    int bbb = in.nextInt();
//    int ccc = in.nextInt();
//    Random g = new Random();
//    System.out.println(average(g.nextInt(), bbb, ccc));
    System.out.println(perimeter(2, 3));
    System.out.println(substring("Respect covers the empty hole where love should be.", 'e'));
  }
  
  public static double average(int a, int b, int c) {
    double average = (a + b + c) / 3.0;
    return average;
    
//    return (a + b + c) / 3;
  }
  
  public static int perimeter(int a, int b) {
    return 2 * a + 2 * b;
  }
  
  public static String substring(String s, char c) {
    int i = s.indexOf(c);
    return s.substring(0, i + 1);
  }
}

Blackboxes.java

package lecture0927;

public class Blackboxes {
  public static void main(String[] args) {
    System.out.println(pad("apple", '?', 10));
//    System.out.println(average(5, 6, 9, 10, 20));
//    System.out.println(substring("Respect covers the empty hole where love should be.", 'w'));
  }
  
  public static String pad(String s, char c, int n) {
    String padded = String.format("%" + n + "s", s).replace(' ', c);
    return padded;
  }
  
  public static String substring(String s, char c) {
    int i = s.indexOf(c);
    return s.substring(0, i + 1);
  }
  
  public static int abs(int a) {
    if (a < 0) {
//      a = a * -1;
      a *= -1;
    }
    return a;
  }
  
  public static double average(int... a) {
    double sum = 0;
    for (int i = 0; i < a.length; ++i) {
      sum += a[i];
    }
    return sum / a.length;
  }
  
  public static int max(int a, int b) {
    return a > b ? a : b;
//    if (a > b) {
//      return a;
//    } else {
//      return b;
//    }
    
//    switch (a > b) {
//    case 0:
//      // handle 0
//    case 1:
//      // handle 1
//    }
    
//    if (a == 0) {
//      
//    } else if (a == 1) {
//      
//    }
  }
}