slanted W3C logo

Day 06—APIs

Today's class introduces the idea of the Application Programming Interface (API). The client only requires the API document to understand how to use a class. The System utility and the Scanner class are introduced to provide the client with components to scan the keyboard for user input.

Interfaces

A driver drives a car using a reasonably small number of controls and information displays:

The controls and displays form the driver's interface to the car.

The car's interface allows the driver to easily control the car without the driver needing to know how the car exactly works. Imagine how difficult it would be to drive a car if the driver had to synchronize the control of all of the individual car components.

Software Interfaces

As a client programming in Java, you are primarily interested in two features when using a component:

  1. what attributes of the component you can directly use
  2. what methods of the component you can directly invoke

The attributes and methods that are directly usable by a client are called public features of the component. These public features are documented in the Application Programming Interface (API) of the component. The API only describes what actions a component can perform; it should not tell you how the component actually performs the action.

The component can also have features that are hidden from the client. These private features are not part of the component's API (see the discussion on access modifiers on page 62 of the textbook). These hidden features are typically related to how the component provides its functionality.

Features that are labelled protected are part of the API, but are not directly accessible to normal clients. They are used by special clients (class implementers) when extending an existing class.

In 1020, we use the term encapsulation to describe the separation of the interface from the implementation.

The System Utility

The System utility provides, among other features, public attributes related to the input of data from the user and the output of data to the user; on a PC, the input typically comes from the keyboard and the output typically goes to the console display.

The attributes for standard input and output are called in and out.

import java.io.InputStream;
import java.io.PrintStream;

public class Template
{
   public static void main(String[] args)
   {
      InputStream in = System.in;
      PrintStream out = System.out;
   }
}

Notice that we access the attributes in and out by specifying the name of the utility System followed by a .

If we look in the API of the System utility for the attributes in and out we find the following:

static InputStream in
The "standard" input stream.

static PrintStream out
The "standard" output stream.

Processing Input with Scanner

The Scanner class lets you create instances that can scan text. This is useful for processing the input from System.in:

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Template
{
   public static void main(String[] args)
   {
      InputStream in = System.in;
      PrintStream out = System.out;

      Scanner input = new Scanner(in);
   }
}

You should start with the above code whenever you see a problem in 1020 that asks you to process user input from the keyboard.

By default, Scanner instances will separate text into pieces (tokens) by looking for whitespace.

Scanner Example

You get the individual tokens from a Scanner instance one at a time. For example, suppose you were asked to write a program that asks the user to input two integers; the program then outputs the average of the two numbers.

We start with our Template class with the class name changed to something more appropriate:

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class UserIntAverage
{
   public static void main(String[] args)
   {
      InputStream in = System.in;
      PrintStream out = System.out;

      Scanner input = new Scanner(in);


   }
}

Scanner Example

Next we prompt the user for some input:

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class UserIntAverage
{
   public static void main(String[] args)
   {
      InputStream in = System.in;
      PrintStream out = System.out;

      Scanner input = new Scanner(in);

      out.println("Enter two integers for averaging:");
   }
}

Scanner Example

Now we ask the Scanner instance for the first integer input by the user:

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class UserIntAverage
{
   public static void main(String[] args)
   {
      InputStream in = System.in;
      PrintStream out = System.out;

      Scanner input = new Scanner(in);

      out.println("Enter two integers for averaging:");

      int first = input.nextInt();
   }
}

If we look in the API of the Scanner class for the method nextInt we find the following:

int nextInt()
Scans the next token of the input as an int.

In this case, the method name is nextInt and it returns an int value back to the client.

Scanner Example

Finally we ask the Scanner instance for the second integer input by the user, compute and output the average:

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class UserIntAverage
{
   public static void main(String[] args)
   {
      InputStream in = System.in;
      PrintStream out = System.out;

      Scanner input = new Scanner(in);

      out.println("Enter two integers for averaging:");

      int first = input.nextInt();
      int second = input.nextInt();
      double average = (first + second) / 2.0;

      out.println("The average is:");
      out.println(average);
   }
}

Scanning for Other Types

The Scanner class defines methods to scan for all of the primitive types (and more):

long nextLong()
Scans the next token of the input as a long.

float nextFloat()
Scans the next token of the input as a float.

double nextDouble()
Scans the next token of the input as a double.

Reading Method Headers

Let's look at one of the methods named min in the utility java.Math:

public static long min(long a, long b)
Returns the smaller of two long values.

The statement public static long min(long a, long b) is called the method header.

Reading Method Headers

public static long min(long a, long b)
Returns the smaller of two long values.

The keyword public is called an access modifier. Remember that only public (and protected) features appear in an API, so I will usually drop the keyword public from documentation examples.

Reading Method Headers

public static long min(long a, long b)
Returns the smaller of two long values.

The keyword static indicates that the method is associated with the class instead of an object. This means that you should invoke a static method using the class name instead of an object name. If the keyword static is missing, then the opposite is true; the method is associated with an object, and you must use an object to invoke the method.

Reading Method Headers

public static long min(long a, long b)
Returns the smaller of two long values.

The keyword long (after static) indicates that this method returns a value of type long back to the client.

The keyword void is used to indicate that the method returns nothing back to the client.

Reading Method Headers

public static long min(long a, long b)
Returns the smaller of two long values.

The identifier (or name) min is the method name.

Reading Method Headers

public static long min(long a, long b)
Returns the smaller of two long values.

The parentheses contain the parameter list of the method. In this case, the method takes as input two parameters both of type long.

Mini Quiz

Which of the following methods are associated with a class (invoked using the name of the class)?

static boolean disjoint(Collection c1, Collection c2)

void setIcon(Icon newIcon)

String toString()

static int round(double a)

static void showMessageDialog(Component parent, Object message)

All of the methods with the keyword static in the header are associated with a class.

Mini Quiz

What is the return type for each of the following methods?

static boolean disjoint(Collection c1, Collection c2)

void setIcon(Icon newIcon)

String toString()

static int round(double a)

static void showMessageDialog(Component parent, Object message)

boolean
void
String
int
void

Mini Quiz

What are the names of each of the following methods?

static boolean disjoint(Collection c1, Collection c2)

void setIcon(Icon newIcon)

String toString()

static int round(double a)

static void showMessageDialog(Component parent, Object message)

disjoint
setIcon
toString
round
showMessageDialog

Mini Quiz

How many parameters do each of the following methods take and what are their types?

static boolean disjoint(Collection c1, Collection c2)

void setIcon(Icon newIcon)

String toString()

static int round(double a)

PrintStream printf(String format, Object... args)

  • 2; Collection and Collection
  • 1; Icon
  • 0
  • 1; double
  • at least 1; String and Object

Self Check

  1. Key concepts 1—14, and 20 at the end of Chapter 2.
  2. Review questions 1—9, 12—14 at the end of Chapter 2.
  3. Modify your solution for eCheck02A so that it asks the user for the month, day, and year (don't try eCheck-ing it, though; that won't work)

To Do For Next Lecture

  1. Finish reading Chapter 2.