Chunk 4, Module 2: How I Learned to Love the Java Application Programming Interface (the Java API)
Unlocking the power of the Java standard Library
We can harness the complex power of computers by learning how to use code that others folks have written and shared with us to use. This code is collected in a set of packages called the Java Standard Library. We access the features of the standard library through its interface -- the Application Programming Interface.
Just like we have to know how to operate a car to use its features (it rolls, it accellerates, it carries stuff), once we learn how to use the Java API, we can unlock the power its hundreds of classes and thousands of methods. We can use those classes to accomplish our own customized tasks with breathtaking ease.
This module will introduce you to the core skills of reading the API documentation with enough precision that you can use the library's packages in your own programs. We'll start by learning to use straightforward methods on common objects. Then we'll create a quiz program that uses a question class we will build and a linked list to manage the user's interaction with those questions.
Jump to a section
Module Hamburger Guide
Digest and diagram the Java library's Application Programming Interface documentation so you can explain what a method does and how one might use it in a program
Use a method on a library class to accomplish a useful task in a program, such as the String.subString(int startingIndex) method
Adapt an object-oriented quiz program which uses the Java Library's LinkedList class and its various methods to create a customized application
Exercise 1: Integer Method Chaining
Exercise type: Be The Compiler
Study the following code for two classes: IntegerMethods and IntegerMethodsChainClient. THEN reproduce the code with DIFFERENT variable values in Netbeans.
Overview of our two classes and their relationship
Note that this is the setup we've been using for some time now: we have a class that acts like a blueprint and a client class that instantiates that blueprint into an Object and then calls methods on that object.
Our method class: IntegerMethods
Peek into the add method:
We can diagram how each of these simple methods work. Add works like this:
Step 1: Reproduce this class in your code, using this skeleton:
Our client class: IntegerMethodChainClient
Step 2: Reproduce this class in your code, using this skeleton:
Step 3: Diagram the methods multiply and subtract
Using the above diagram of add above, create method diagrams on the back of your hamburger.
Step 4: Become the compiler and generate the value of returnedValFromCall3
BEFORE running your code, compute on your own, using your own brain, the value that will be displayed in the program output--the value of the variable returnedFromCall3.
DON'T Peek until you have your own answer!
Step 5: Review the entire process digram of the client class's method calls:\
Study this diagram carefully that shows how each of the methods in our client class work with the object IntegerMethods to accomplish useful stuff.
Exercise 2: Slightly more complicated method calls with double values
Exercise type: Be the compiler
Study the following code and prepare to Be the Compiler and compute the values that each of our methods generates on your own.
The output of the program is as follows, without the computed values:
Step 1: Study the class DoubleMethods
Step 2: Review the method diagram for calcPercent:
Step 3: Study our client class DoubleMethodChainClient
Step 4: Generate values for each method call result using your BRAIN
On the back of your hamburger, calculate the value of each of our output variables--This is the same output skeleton from above--complete it:
Check your answers after you've given it a good faith effort
Exercise 3: The Java API and reading the API documentation
Exercise type: Unpacking the API
We can use code that was written by other coders by learning to read the Java API documentation
Here's the String documentation which shows us all the methods available on each String object we've ever made and ever will make:
If we zoom into one particular method, such as toUpperCase, we can see the documentation signature is as follows:
We can diagram this method as follows:
And we can use this method in a simple Java program like this:
Which produces this output:
In a sandbox (or playground) find FIVE other methods on String objects and practice calling them. Screen shot your program and PRINT it with your hamburger.