APIs

public class Math

double

abs(double a)

absolute value of a

double

max(double a, double b)

maximum of a and b

double

min(double a, double b)

minimum of a and b

Note 1: abs(), max(), and min() are defined also for int, long, and float.

double

sin(double theta)

sine of theta

double

cos(double theta)

cosine of theta

double

tan(double theta)

tangent of theta

Note 2: Angles are expressed in radians. Use toDegrees() and toRadians() to convert. Note 3: Use asin(), acos(), and atan() for inverse functions.

double

exp(double a)

exponential (ea)

double

log(double a)

natural log (loge a, or ln a)

double

pow(double a, double b)

raise a to the bth power (ab)

long

round(double a)

round a to the nearest integer

double

random()

random number in [0, 1)

double

sqrt(double a)

square root of a

double

E

value of e (constant)

double

PI

value of π (constant)

public class String

 

String(String s)

create a string with the same value as s

 

String(char[] a)

create a string that represents the same sequence of characters as a[]

int

length()

string length

char

charAt(int i)

ith character

String

substring(int i, int j)

ith through (j-1)st characters

boolean

contains(String sub)

does string contain sub as a substring?

boolean

startsWith(String pre)

does string start with pre?

boolean

endsWith(String post)

does string end with post?

int

indexOf(String p)

index of first occurrence of p

int

indexOf(String p, int i)

index of first occurrence of p after i

String

concat(String t)

this string with t appended

int

compareTo(String t)

string comparison

String

replaceAll(String a, String b)

result of changing as to bs

String[]

split(String delim)

strings between occurrences of delim

boolean

equals(String t)

is this string’s value the same as t’s?

public class System.out/StdOut/Out

 

Out(String name)

create output stream from name

void

print(String s)

print s

void

println(String s)

print s, followed by newline

void

println()

print a newline

void

printf(String format, ...)

print the arguments to standard output, as specified by the format string format

Note: For System.out/StdOut, methods are static and constructor does not apply.

public class StdIn/In

 

In(String name)

create input stream from name

methods for reading individual tokens

boolean

isEmpty()

is input stream empty (or only whitespace)?

int

readInt()

read a token, convert it to an int, and return it

double

readDouble()

read a token, convert it to a double, and return it

boolean

readBoolean()

read a token, convert it to a boolean, and return it

String

readString()

read a token and return it as a String

methods for reading characters

boolean

hasNextChar()

does input stream have any remaining characters?

char

readChar()

read a character from input stream and return it

methods for reading lines from standard input

boolean

hasNextLine()

does input stream have a next line?

String

readLine()

read the rest of the line and return it as a String

methods for reading the rest of standard input

int[]

readAllInts()

read all remaining tokens and return them as an int array

double[]

readAllDoubles()

read all remaining tokens and return them as a double array

boolean[]

readAllBooleans()

read all remaining tokens and return them as a boolean array

String[]

readAllStrings()

read all remaining tokens and return them as a String array

String[]

readAllLines()

read all remaining lines and return them as a String array

String

readAll()

read the rest of the input and return it as a String

Note 1: For StdIn, methods are static and constructor does not apply.

Note 2: A token is a maximal sequence of non-whitespace characters.

Note 3: Before reading a token, any leading whitespace is discarded.

Note 4: Analogous methods are available for reading values of type byte, short, long, and float.

Note 5: Each method that reads input throws a run-time exception if it cannot read in the next value, either because there is no more input or because the input does not match the expected type.

public class StdDraw/Draw

 

Draw()

create a new Draw object

drawing commands

void

line(double x0, double y0, double x1, double y1)

void

point(double x, double y)

void

circle(double x, double y, double radius)

void

filledCircle(double x, double y, double radius)

void

square(double x, double y, double radius)

void

filledSquare(double x, double y, double radius)

void

rectangle(double x, double y, double r1, double r2)

void

filledRectangle(double x, double y, double r1, double r2)

void

polygon(double[] x, double[] y)

void

filledPolygon(double[] x, double[] y)

void

text(double x, double y, String s)

control commands

void

setXscale(double x0, double x1)

reset x-scale to (x0, x1)

void

setYscale(double y0, double y1)

reset y-scale to (y0, y1)

void

setPenRadius(double radius)

set pen radius to radius

void

setPenColor(Color color)

set pen color to color

void

setFont(Font font)

set text font to font

void

setCanvasSize(int w, int h)

set canvas size to w-by-h

void

enableDoubleBuffering()

enable double buffering

void

disableDoubleBuffering()

disable double buffering

void

show()

copy the offscreen canvas to the onscreen canvas

void

clear(Color color)

clear the canvas to color color

void

pause(int dt)

pause dt milliseconds

void

save(String filename)

save to a .jpg or .png file

Note 1: For StdDraw, the methods are static and the constructor does not apply.

Note 2: Methods with the same names but no arguments reset to the default values.

public class StdAudio

void

play(String filename)

play the given .wav file

void

play(double[] a)

play the given sound wave

void

play(double x)

play sample for 1/44,100 second

void

save(String filename, double[] a)

save to a .wav file

double[]

read(String filename)

read from a .wav file

public class Stopwatch

 

Stopwatch()

create a new stopwatch and start it running

double

elapsedTime()

return the elapsed time since creation, in seconds

public class Picture

 

Picture(String filename)

create a picture from a file

 

Picture(int w, int h)

create a blank w-by-h picture

int

width()

return the width of the picture

int

height()

return the height of the picture

Color

get(int col, int row)

return the color of pixel (col, row)

void

set(int col, int row, Color c)

set the color of pixel (col, row) to c

void

show()

display the picture in a window

void

save(String filename)

save the picture to a file

public class StdRandom

void

setSeed(long seed)

set the seed for reproducible results

int

uniform(int n)

integer between 0 and n-1

double

uniform(double lo, double hi)

floating-point number between lo and hi

boolean

bernoulli(double p)

true with probability p, false otherwise

double

gaussian()

Gaussian, mean 0, standard deviation 1

double

gaussian(double mu, double sigma)

Gaussian, mean mu, standard deviation sigma

int

discrete(double[] p)

i with probability p[i]

void

shuffle(double[] a)

randomly shuffle the array a[]

public class StdArrayIO

double[]

readDouble1D()

read a one-dimensional array of double values

double[][]

readDouble2D()

read a two-dimensional array of double values

void

print(double[] a)

print a one-dimensional array of double values

void

print(double[][] a)

print a two-dimensional array of double values

Note 1. 1D format is an integer n followed by n values.

Note 2. 2D format is two integers m and n followed by m × n values in row-major order.

Note 3. Methods for int and boolean are also included.

public class StdStats

double

max(double[] a)

largest value

double

min(double[] a)

smallest value

double

mean(double[] a)

average

double

var(double[] a)

sample variance

double

stddev(double[] a)

sample standard deviation

double

median(double[] a)

median

void

plotPoints(double[] a)

plot points at (i, a[i])

void

plotLines(double[] a)

plot lines connecting points at (i, a[i])

void

plotBars(double[] a)

plot bars to points at (i, a[i])

Note: Overloaded implementations are included for all numeric types.

public class Stack<Item> implements Iterable<Item>

 

Stack()

create an empty stack

boolean

isEmpty()

is the stack empty?

int

size()

number of items in the stack

void

push(Item item)

insert an item onto the stack

Item

pop()

return and remove the item that was inserted most recently

public class Queue<Item> implements Iterable<Item>

 

Queue()

create an empty queue

boolean

isEmpty()

is the queue empty?

int

size()

number of items in the queue

void

enqueue(Item item)

insert an item into the queue

Item

dequeue()

return and remove the item that was inserted least recently

public class SET<Key extends Comparable<Key>> implements Iterable<Key>

 

SET()

create an empty set

boolean

isEmpty()

is the set empty?

int

size()

number of elements in the set

void

add(Key key)

add key to the set

void

remove(Key key)

remove key from set

boolean

contains(Key key)

is key in the set?

public class ST<Key extends Comparable<Key>, Value>

 

ST()

create an empty symbol table

void

put(Key key, Value val)

associate val with key

Value

get(Key key)

value associated with key

void

remove(Key key)

remove key (and its associated value)

boolean

contains(Key key)

is there a value paired with key?

int

size()

number of key–value pairs

Iterable<Key>

keys()

all keys in sorted order

Key

min()

minimum key

Key

max()

maximum key

int

rank(Key key)

number of keys less than key

Key

select(int k)

kth smallest key in symbol table

Key

floor(Key key)

largest key less than or equal to key

Key

ceiling(Key key)

smallest key greater than or equal to key

public class Graph

 

Graph()

create an empty graph

 

Graph(String filename, String delimiter)

create graph from a file

void

addEdge(String v, String w)

add edge v-w

int

V()

number of vertices

int

E()

number of edges

Iterable<String>

vertices()

vertices in the graph

Iterable<String>

adjacentTo(String v)

neighbors of v

int

degree(String v)

number of neighbors of v

boolean

hasVertex(String v)

is v a vertex in the graph?

boolean

hasEdge(String v, String w)

is v-w an edge in the graph?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116