teaching machines

CS1: Lecture 10 – Abstraction

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

Dear students,

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 fulfill its purpose.

As consumers or callers of a method, we need only know its interface. How it’s implemented is not terribly important to us. As producers of a method, we are very much concerned with its implementation. Many times we jump back and forth between the roles of producer and consumer, and it will be easy to mix up what role you are currently in.

Today we’ll complete a few exercises that get us thinking the distinction between the interface and the implementation.

Abstraction

Methods are providers of abstraction. They hide away low-level details. We can think of them as black boxes, exposing certain inputs and one output but obscuring their inner workings. What goes on inside the box is not relevant to the person who just wants the job done, allowing that person to operate more quickly and at a higher level.

For our next exercise, let’s demonstrate something what happens in software industries all across the world: collaborative implementation. 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:

Let’s complete this in two stages:

Blackbox

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

TODO

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…

Shapespeare’s autopsy
Inside him they found monkeys
Too many to count

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

BowArea.java

package lecture0925.cs145;

public class BowArea {
  public static double circleArea(double radius) {
    double area = Math.PI * Math.pow(radius, 2);
    return area;
  }

  public static double ringArea(double outerRadius, double innerRadius) {
//    double innerArea = Math.PI * Math.pow(innerRadius, 2);
//    double outerArea = Math.PI * Math.pow(outerRadius, 2);
    double ringArea = circleArea(outerRadius) - circleArea(innerRadius);
    return ringArea;
  }

  public static double bowArea(double outerRadius, double innerRadius, double degrees) {
    double area = ringArea(outerRadius, innerRadius) * degrees / 360.0;
    return area;
  }

  public static void main(String[] args) {
    System.out.println(bowArea(10, 5, 150));
  }
}

Blackboxes.java

package lecture0925.cs145;

public class Blackboxes {
  public static void main(String[] args) {
    System.out.println(average(1, 2, 4));
    System.out.println(perimeter(2, -2));
    System.out.println(sumOneTens(132));
  }

  public static double average(int a, int b, int c) {
    return (a + b + c) / 3.0;
  }

  public static int perimeter(int a, int b) {
    return (a + b) * 2;
  }

  public static int sumOneTens(int a) {
    int ones = a % 10;
    int tens = a % 100 / 10;
    return ones + tens;
  }
}

Bow.java

package lecture0925.cs148;

public class Bow {
  public static double areaOfCircle(double radius) {
    return Math.PI * radius * radius;
  }

  public static double areaOfRing(double bigRadius, double smallRadius) {
//    return Math.PI * Math.pow(bigRadius, 2) - Math.PI * Math.pow(smallRadius, 2);
    return areaOfCircle(bigRadius) - areaOfCircle(smallRadius);
  }

  public static double areaOfBow(double bigRadius, double smallRadius, double percentShaded) {
    return areaOfRing(bigRadius, smallRadius) * percentShaded;
//    return areaOfRing(bigRadius, smallRadius) * degrees / 360;
  }
}

Blackbox.java

package lecture0925.cs148;

public class Blackbox {
  public static void main(String[] args) {
    System.out.println(average(1, 2, 4));
    System.out.println(sumOnesTens(-2));
    System.out.println(frankenstring("foobag", "duck"));
  }

  public static double average(int a, int b, int c) {
    return (a + b + c) / 3.0;
  }

  public static int perimeter(int a, int b) {
    return (a + b) * 2;
  }

  public static int sumOnesTens(int a) {
    int ones = a % 10;
    int tens = a / 10 % 10;
    return ones + tens;
  }

  public static String frankenstring(String a, String b) {
    String halfA = a.substring(0, a.length() / 2);
    String halfB = b.substring(b.length() / 2);
    return halfA + halfB;
  }
}