class Circle {
static final double PI = 3.14159;
double x, y; // the center of the circle
double radius;
Circle(double nx, double ny, double nr) {x=nx; y=ny; r=nr;}
Circle(double nr) {this(0.0; 0.0, nr);}
double area() { return PI * radius * radius; }
double circumference() { return 2 * PI * radius; }
static double nTimesPI(int n) {return n * PI;}
}
class Circle {
static final double PI = 3.14159;
double x, y;
double radius;
...
}
class Circle {
class Circle {
Circle c2 = new Circle(1.0, 2.0, 3.0); // calls first constructor Circle c1 = new Circle(1.9); // calls second constructor
double aSum = c1.area() + c2.area(); double rSum = c1.radius + c2.radius;
double fourPI = c1.nTimesPI(2) + Circle.nTimesPI(2);
class FileWrapper
{
if (fd != null) close();}
class GraphicsCircle
extends Circle
{
GraphicsCircle(double nx, double ny, double nr) { super(nx,ny,nr); }
boolean isOnScreen = false;
void draw(DrawWindow w)
{
w.drawCircle(x, y, radius, outline, noFill);
}
}
class FilledCircle
extends GraphicsCircle
{
void draw(DrawWindow w)
{
if (!do_fill)
super.draw(w); // calls GraphicsCircle.draw() on this
else
w.drawCircle(x,y,radius, outline, fill);
}
}
Circle c = new FilledCircle(); c.draw(); // calls FilledCircle.draw() c = new GraphicsCircle(...); c.draw() // calls GraphicsCircle.draw()
int i = 1; float f = i;
FilledCircle fc = new FilledCircle(); Circle C = fc; // always ok
float f = 1.1; int i = (int)f;
Circle C = new FilledCircle(); FilledCircle f = (FilledCircle)C; // throws ClassCastException if C is not FilledCircle if (C instanceof FilledCircle) // checking actual type is wise f = (FilledCircle)C;
public abstract class Shape
{
public abstract double area();
public abstract double circumference();
}
public class Circle
extends Shape
{
public class Shape
{
public abstract double area();
public abstract double circumference();
public static double sumArea(Shape[] a)
{
double total = 0.0;
for (int i=0; i<a.length; i++)
total += a[i].area();
return total;
}
}
Shape[] a = {new Circle(...), new Square(...), new GraphicsCircle(...)};
double totalArea = Shape.sumArea(a);
public interface Runable
{
public void init();
public void start();
public void stop();
public void destroy();
}
public Class RunningCircle
extends Circle
implements Runable
{
public void init() { ... }
...
}
public interface RunableCar
extends Runable
{ // in addition to the abstract methods of Runable, add the following
public void turnLeft();
public void turnRight();
public void break();
public void accelerate();
}
char s[] = new char[100]; char[] s1 = new char[50]; // brackets may go either place in declaration
for (int i=0; i<s.length; i++)
int i = -1, j = 100; char c = s[i] + s[j] ; // either will throw IndexOutOfBoundsException
Circle[] ca = new Circle[100]; // elements are all null
Circle[] ca1 = {new Circle(1.0), new Circle(2.0), new Circle(3.0)};
char[] s2 = {'a', 'b', 'c'};
class Object {
Object()
Object clone () // component copy
boolean equals(Object o) // component equality
String toString() // gives String image of object
void finalize() // done when object is freed
Class getClass() // gives run-time class of an object
int hashCode() // allows use in a Hashtable
void wait() wait(long) wait(long, int) // thread wait-for-notify
void notify() notifyAll() // unblocks waiting threads
}
String s = "Hello there";
char[] a = {'H','e','l','l','o',' ','t','h','e','r','e'};
String s = new String(a);
String s2 = s + " everyone";
s2 += " here";
s2 = s2.concat(" and now");
class String {
String () // same as ""
String(String)
String(char[])
String(char[], int start, int end)
String(byte[], int, int, int)
String(byte[], int)
String(StringBuffer)
int length() // number of chars in String
char charAt (int index)
boolean equals(Object o)
boolean equalsIgnoreCase(String s)
boolean compareTo(String s) // like C's strcmp
String concat(String s) // called by + operator
int indexOf(int ch)
int indexOf(int ch, int start)
int indexOf(String s)
int indexOf(String s, int start)
int lastIndexOf(int ch)
int lastIndexOf(int ch, int start)
int lastIndexOf(String s)
int lastIndexOf(String s, int start)
bolean regionMatches(int start, String s, int sstart, int len)
boolean regionMatches(boolean ignoreCase, int start, String s, int sstart, int len)
String replace(char ch, char replacement)
boolean startsWith(String s, int skip)
boolean startsWith(String s)
boolean endsWith(String s)
String substring(int len)
String substring(int start, int len)
String toLowerCase()
String toString()
String toUpperCase()
String trim() // trims leading and trailing whitespace
String valueOf(Object o)
String valueOf(char[] s)
String valueOf(char[] s, int start, int len)
String valueOf(boolean b)
String valueOf(char c)
String valueOf(int i)
String valueOf(long l)
String valueOf(float f)
String valueOf(double d)
char[] toCharArray()
void getBytes(int start, int end, byte[] b, int bStart)
void getChars(int start, int end, char[] b, int bStart)
String copyValueOf(char[] b, int start, int end)
String copyValueOf(char[] b)
int hashCode()
int intern() // allows faster equals comparison with ==
}
StringBuffer b = new StringBuffer("Hello");
b.append(" there");
b.setCharAt(0, 'M');
b.setLength(b.length()-3); // chops off last three characters
String s = b.toString(); // converts b to a String
StringBuffer b1(100); // initial value is "" b1.capacity() // returns 100 StringBuffers are reallocated if more space is necessary
IntegerStack first = new IntegerStack(); first.push(10); first.push(20); IntegerStack second = first.clone(); second.push(30); first.push(40);
Object clone()
{
IntegerStack copy = (IntegerStack)super.clone();
copy.buffer = (int[])buffer.clone();
return copy;
}