Using API in Java
EECS1021:
Object Oriented Programming: from Sensors to Actuators Winter 2019
CHEN-WEI WANG
Learning Outcomes
Understand:
Self-Exploration of Java API
Method Header
Parameters vs. Arguments
Non-Static Methods and Collection Library Static Methods and Math Library
2 of 13
Application Programming Interface (API)
Each time before you start solving a problem:
X As a beginner, crucial to implement everything by yourself.
X As you get more experienced, first check to see if it is already
solved by one of the library classes or methods. Rule of the Thumb: DO NOT REINVENT THE WHEEL!
An is a collection of programming facilities for and building your applications. Java API contains a library of classes (e.g., Math, ArrayList, HashMap) and methods (e.g., sqrt, add, remove):
https://docs.oracle.com/javase/8/docs/api/ To use a library class, put a corresponding import statement:
import java.util.ArrayList; class MyClass {
ArrayList myList;
…
}
3 of 13
Application Programming Interface (API)
reuse
Classes vs. Methods
A is a named block of code by its name. e.g., As a user of the sqrt method (from the Math class):
method
Implementation code of sqrt is hidden from you. You only need to know how to call it in order to use it.
X A non-static method must be called using a
e.g., Illegal to call ArrayList.add(“Suyeon”). Instead:
.
ArrayList
reusable
context object
X A static method can be called using the
e.g., By calling Math.sqrt(1.44), you are essentially reusing a block of code, hidden from you, that will be executed and calculate the square root of the input value you supply (i.e., 1.44).
A class contains a collection of related methods.
e.g., The Math class supports methods related to more advanced
mathematical computations beyond the simple arithmetical
operations we have seen so far (i.e., +, -, *, /, and %). 4 of 13
name of its class
.
Parameters vs. Arguments
Parameters of a method are its input variables that you read from the API page.
e.g., double pow(double a, double b) has:
X two parameters a and b, both of type double X one output/return value of type double
Arguments of a method are the specific input values that you supply/pass in order to use it.
e.g., To use the pow method to calculate 3.45, we call it by writing Math.pow(3.4, 5).
Argument values must conform to the corresponding parameter types.
e.g., Math.pow(“three point four”, “5”) is an invalid
call!
5 of 13
Header of a Method
Header ofamethodinformsusersoftheintendedusage: X Name of method
X List of inputs (a.k.a. parameters) and their types X Type of the output (a.k.a. return type)
Methods with the void return type are mutators. Methods with non-void return types are accessors.
e.g. In Java API, the Method Summary section lists headers and descriptions of methods.
6 of 13
Example Method Headers: Math Class
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Method Overloading : multiple methods sharing the same name, but with distinct lists of parameters (e.g., abs method). The abs method being static allows us to write
Math.abs(-2.5). 7 of 13
Case Study: Guessing a Number
Problem: Your program:
X internally and randomly sets a number between 0 and 100
X repeatedly asks the user to enter a guess, and hints if they got it,
or should try something smaller or larger
X once the user got it and still wishes to continue, repeat the game
with a different number
Hints:
(int) Math.random() * 100 or
(int) (Math.random() * 100)
??
8 of 13
Example Method Headers: ArrayList Class
An ArrayList acts like a “resizable” array (indices start with 0).
9 of 13
Case Study: Using an ArrayList
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23
import java.util.ArrayList; public class ArrayListTester {
public static void main(String[] args) {
ArrayList
println(list.size());
println(list.contains(“A”));
println(list.indexOf(“A”));
list.add(“A”);
list.add(“B”);
println(list.contains(“A”)); println(list.contains(“B”)); println(list.contains(“C”)); println(list.indexOf(“A”)); println(list.indexOf(“B”)); println(list.indexOf(“C”)); list.add(1, “C”);
println(list.contains(“A”)); println(list.contains(“B”)); println(list.contains(“C”)); println(list.indexOf(“A”)); println(list.indexOf(“B”)); println(list.indexOf(“C”)); list.remove(“C”);
println(list.contains(“A”)); println(list.contains(“B”)); println(list.contains(“C”)); println(list.indexOf(“A”)); println(list.indexOf(“B”)); println(list.indexOf(“C”));
for(int i = 0; i < list.size(); i ++) { println(list.get(i));
} }
}
See Java Data Types (3.3.1) – (3.3.2) in Classes and Objects for another
example on ArrayList.
10 of 13
Example Method Headers: HashTable Class
A HashTable acts like a two-column table of (searchable) keys and values.
11 of 13
Case Study: Using a HashTable
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import java.util.Hashtable; public class HashTableTester {
public static void main(String[] args) {
Hashtable
System.out.println(“Key Alan exists: ” + grades.containsKey(“Alan”)); System.out.println(“Value B+ exists: ” + grades.containsValue(“B+”)); grades.put(“Alan”, “A”);
grades.put(“Mark”, “B+”);
grades.put(“Tom”, “C”);
System.out.println(“Size of table: ” + grades.size());
System.out.println(“Key Alan exists: ” + grades.containsKey(“Alan”)); System.out.println(“Key Mark exists: ” + grades.containsKey(“Mark”)); System.out.println(“Key Tom exists: ” + grades.containsKey(“Tom”)); System.out.println(“Key Simon exists: ” + grades.containsKey(“Simon”)); System.out.println(“Value A exists: ” + grades.containsValue(“A”)); System.out.println(“Value B+ exists: ” + grades.containsValue(“B+”)); System.out.println(“Value C exists: ” + grades.containsValue(“C”)); System.out.println(“Value A+ exists: ” + grades.containsValue(“A+”)); System.out.println(“Value of existing key Alan: ” + grades.get(“Alan”)); System.out.println(“Value of existing key Mark: ” + grades.get(“Mark”)); System.out.println(“Value of existing key Tom: ” + grades.get(“Tom”)); System.out.println(“Value of non-existing key Simon: ” + grades.get(“Simon”)); grades.put(“Mark”, “F”);
System.out.println(“Value of existing key Mark: ” + grades.get(“Mark”)); grades.remove(“Alan”);
System.out.println(“Key Alan exists: ” + grades.containsKey(“Alan”)); System.out.println(“Value of non-existing key Alan: ” + grades.get(“Alan”));
} }12 of 13
Index (1)
Learning Outcomes
Application Programming Interface (API) Classes vs. Methods
Parameters vs. Arguments
Header of a Method
Example Method Headers: Math Class Case Study: Guessing a Number
Example Method Headers: ArrayList Class Case Study: Using an ArrayList
Example Method Headers: HashTable Class
Case Study: Using a HashTable
13 of 13