## CS1: Lecture 10 – Abstraction

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:

- Left third, write a method that computes the area of a circle.
- Middle third, write a method that computes the area of a ring.
- Right third, write a method that computes the area of a bow.

Let’s complete this in two stages:

**Stage 1**: write out the “shape” or interface of the method. What is its name, return type, and parameters? Do not write anything but the first line of the method, which is sometimes called its*signature*. (Technically, the signature doesn’t include the return type.)**Stage 2**: implement the method. Write the code that goes between the curly braces. Assume that your classmates write their methods correctly. Use their work to make your own easier!

### Blackbox

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

- Blackbox #1
- Blackbox #2
- Blackbox #3
- Blackbox #4
- Blackbox #5
- Blackbox #6
- Blackbox #7
- Blackbox #8
- Blackbox #9
- Blackbox #10

### TODO

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

- If you want to get a feel for what companies are looking for in interns and new hires, visit the career fair on Thursday. For an extra credit participation point, report back what you learned on a quarter sheet of paper to be turned in at the beginning of our next lecture.
- CS 148, your lab is posted. I invite you to start early.
- Homework 2 is posted. If homework 1 didn’t go as you would have liked, adjust accordingly. It’s due by the end of October 7.

See you next class!

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;
}
}
```