university of limerick1 work with api’s. university of limerick2 learning oo programming u...
TRANSCRIPT
University of Limerick 1
Work with API’s
University of Limerick 2
Learning OO programming
Learning a programming language can be broadly split into two part– Learning the language
» Sequence, selection and iteration
– Learning the API» Commonly used structure and services
» GUI’s, Input\Output, Data storage, Networking
University of Limerick 3
Learning the language
abstract
boolean
break
byte
case
catch
char
class
const*
continue
default
do
int
interface
long
native
new
null
package
private
protected
public
return
short
double
else
extends
final
finally
float
for
goto*
if
implements
import
instanceof
static
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
N.B. Keyword maked with * are unused
University of Limerick 4
Learning the API
API stands for Application Programming Interface The interface (calling conventions) by which an application
programmer accesses the standard Java classes Java
– 2,000 Classes and Interfaces
– Spread over more than 75 packages
University of Limerick 5
Java Class Library As we can see the Java provides a large collection of
classes that support and simplify many common programming activities– GUIs, TCP/IP sockets, CORBA, Compression (ZIP), 2D
Graphics, Encryption
University of Limerick 6
Commonly used packages
java.lang – Provides classes that are fundamental to the design of the Java
programming language (including the Math class)
java.math – Provides classes for performing math functionality
University of Limerick 7
Scale
At first the number of classes may seem impossible to learn but in most cases programmers are only required to know a specific subset
The subset they know often depends on what they are developing
What is of importance is that you can find and use new classes as you require or discover them in preexisting code
University of Limerick 8
Comparison The Standard Dictionary
– Over 100,000 entries
Commonly used words– First 25 words make up about one-third of all printed material in
English
– First 100 words make up about one-half of all written material
– First 300 make up about sixty-five percent of all written material in English
www.duboislc.org/EducationWatch/First100Words.html
University of Limerick 9
The Math Class
Description provided in Java API Collection of common math functions (sin, cos, sqrt, etc.). And two constants: PI and E Math.PI
– 3.141592653589793
Math.sqrt(25)– 5.0
Math.pow(2,10)– 1024
Math.cos(0)– 1.0
Math.cos(2 * Math.PI)– 1.0
University of Limerick 10
How the Math Class Works
public class Math{ public static final double PI = 3.141592653589793;
public static double sin(double d){ .. } public static double sqrt(double d) { .. }
private Math(){} ..}
> Math.PI3.141592653589793> Math.sqrt(25)5.0
University of Limerick 11
What's different about Math Class
It’s different from a typical Java class – It is a “stateless” class
– We only need one Math class (not multiple instances)
– No need to instantiate it (hence, no public constructor)
– All of its variables and methods are static
– static means “applies to the class as a whole” vs. “applies to an individual instance”
University of Limerick 12
Math Class Description
When we look at the Maths class using the API documentation notice the phrase java.lang at the top of the main panel above the word Math– This means that the Math class is part of the core Java language
and hence can be used directly
Math Class Interface– Field Summary: Has two constants PI and E
– Constructor Summary: has no public constructor
– Methods Summary: many methods all which are static
– Method Details: e.g. sqrt() takes a double and returns a double
University of Limerick 13
Packages and import Statements If a class is not part of java language i.e. java.lang, you'll see
package name
What is a package?– Basically it's a directory that has a collection of related classes – E.g. Random Class description contains: java.util.Random– Indicating that the Random code is stored in a file called
java/util/Random.class somewhere on your machine. – The java/util directory/folder is known as the "util", or utility package.
Since Random is not part of Java Language we need to tell Java where to find it by saying– import java.util.Random; – Another way is to use the asterisk "wildcard character": import java.util.*;
University of Limerick 14
Random Class A class to create Random numbers
Constructor Summary shows the objects of this type can be created
– E.g. Random ran = new Random();
Method Summary shows that it can generate random values of types:
– integers, doubles etc.
– E.g. r.nextInt(6) – Generate a integer numbers between 0 (inclusive) and 6 (exclusive)
– How do I generate a number between 1 and 6 ?
University of Limerick 15
Pseudorandom numbers The computer is not capable of generating truly random
numbers – The computer can only generate pseudorandom numbers--
numbers that are generated by a formula
– Pseudorandom numbers look random, but are perfectly predictable if you know the formula
» Pseudorandom numbers are good enough for most purposes, but not all--for example, not for serious security applications
– Devices for generating truly random numbers do exist » They are based on environmental noise, or on lava lamps