teaching machines

CS 145 Lecture 9 – Blackboxes

September 26, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

We have seen methods as a means of capturing a process into a reusable component. Methods have some really nice advantages:

You will hear people take about methods as a means of abstraction, hiding away low-level details. We can think of them as blackboxes, exposing certain inputs and one output but obscuring their inner workings. What goes on inside the box is not relevant to the purpose who just wants the job done, allowing that person to operate more quickly and at a higher level. Today, then, we start with some blackboxes. I will show some encapsulated processes, and its your job to figure out what functionality they afford us:

Blackbox #1
Blackbox #2
Blackbox #3

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.

One of my favorite illustrations of the difference between interface and implementation comes from an old textbook I used during my first teaching gig in 2008. When you look at an outlet, you see the standard interface first designed by Harvey Hubbell in 1904. But behind the wall, you have no idea how the electricity is getting to you. It could be a coal-fired power plant, it could be Niagara Falls, or it could be a hamster spinning a wheel.

Now it’s your turn to compose a 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.

For our next exercise, let’s demonstrate something what happens in software industries all across the world: cooperation. Let’s split a larger task up into smaller pieces, and different sections of the class can complete the smaller pieces independently. The overall task is to compute the area of a circular bow. Here are the bite-sized pieces:

Assume that your classmates write their methods correctly. Use their work to make your own easier!

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…

ScannerEg.java

package lecture0926;

import java.util.Scanner;

public class ScannerEg {
  public static void main(String[] args) {
    Scanner in = new Scanner("Andrew,Brian,Harrison");
    in.useDelimiter(",");
    
    in.next();
//    System.out.println(name);
    
    String name = in.next();
    System.out.println(name);
  }
}

EauClairea.java

package lecture0926;

public class EauClairea {
  public static double circleArea(double r) {
    double area = Math.PI * r * r;
    return area;
  }
  
  public static double ringArea(double radiusOuter, double radiusInner) {
    double areaOuter = circleArea(radiusOuter);
    double areaInner = circleArea(radiusInner);
    return areaOuter - areaInner;
  }
  
  public static double bowArea(double radiusOuter,
                           double radiusInner,
                           double degrees) {
    double ringArea = ringArea(radiusOuter, radiusInner);
    double radians = Math.toRadians(degrees);
    return ringArea * radians / (2 * Math.PI;
  }
}