CS 145 Lecture 9 – Blackboxes
Dear students,
We have seen methods as a means of capturing a process into a reusable component. Methods have some really nice advantages:
- They drop us into a smaller world with a smaller problem to solve. Instead of thinking about the grand mission of our program, we think about the baby step of the method.
- They can be written and tested independently of the larger program. Considering how many mistakes we are going to make, this is good news.
- They are insulated from other parts of our program. A method can’t clobber data in another method.
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:
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: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.public static int mystery(int x) { int value = x + 1; return value; }
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:
- 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.
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:
- Start homework 2, which is due before October 10. Recall that early and frequent commit-pushes earn you more points. You should be committing and pushing after every coding session.
- Some tweaks were made to the SpecChecker and PDF for the homework 2. These tweaks were made in my template repository. To bring these changes down to your own repository, as described in part 2 of homework 0, run Team / Pull… in Eclipse. Choose twodee as the remote and click Finish.
- The syllabus had a mismatched day/date for the two midterms. The first is Monday, October 10. There will be no lab the following day, as I will be grading.
See you next class!
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; } }