Animals, Vehicles, Houses, Shapes
class Car extends Vehicle { ... }
class Car extends Vehicle { Engine e; ... }
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 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
}
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()
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;
ArrayList l = new ArrayList(10); l.insert( new FilledCircle() ); Object x = l.remove(); FilledCircle c = (FilledCircle)x;
abstract class Shape
{
abstract double area();
abstract double circumference();
}
class Circle
extends Shape
{
class Shape
{
abstract double area();
abstract double circumference();
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 class Shape
{
protected Point origin;
protected Color color;
Shape(Point newOrigin, Color newColor)
{
origin = newOrigin;
color = newColor;
}
public void moveTo(Point to)
{
origin = to;
}
}
public class Circle
extends Shape
{
private static final double PI = 3.14159;
protected double radius;
public Circle( double newRadius, Point newOrigin, Color newColor )
{
super( newOrigin, newColor );
radius = newRadius;
}
// inherits moveTo from Shape
public double circumference()
{
return 2.0 * PI * radius;
}
}
abstract class Shape
{
public abstract double area();
}
class Triangle
extends Shape
{
protected double base;
protected double height;
public Triangle( double newBase, double newHeight )
{
base = newBase;
height = newHeight;
}
public double area()
{
return base * height / 2.0;
}
}
class Square
extends Shape
{
protected double side;
public Square( double newSide )
{
side = newSide;
}
public double area()
{
return side * side;
}
}
class ListNode
{
Shape info;
ListNode next;
ListNode( Shape newInfo, ListNode newNext )
{
info = newInfo;
next = newNext;
}
}
class Picture
{
ListNode head;
Picture()
{
head = null;
}
void enter( Shape a )
{
head = new ListNode( a, head );
}
double totalArea()
{
double total = 0.0;
for (ListNode p = head; p != null; p = p.next)
total += p.info.area();
return total;
}
}
public static void main(String [] args)
{
Picture p;
p.enter( new Triangle(10,10) );
p.enter( new Square(10) );
System.out.println("Total area = " + p.totalArea() + '\n');
}
interface Runable
{
void init();
void start();
void stop();
void destroy();
}
Class RunningCircle
extends Circle
implements Runable
{
void init() { ... }
...
}
interface RunableCar
extends Runable
{ // in addition to the abstract methods of Runable, add the following
void turnLeft();
void turnRight();
void break();
void accelerate();
}
try
{
// this statement execute,
// if an exception is thrown, its caught below
}
catch (SomeException e)
{
// come here if SomeException is thrown
}
catch (OtherException e)
{
// come here if OtherException is thrown
}
finally
{
// always excute this statement no matter how we leave the try block
}
public void openFile()
throws SomeException, OtherException
{
throw new SomeException("Oops!");
}
import java.io.*;
class FileIOTest
{
public static void main(String argv[])
{
String pat = "for"; // or whatever you want to find
String inFile = "input.txt"
String outFile = "output.txt";
String s;
try {
BufferedReader br =
new BufferedReader(new FileReader(inFile));
PrintWriter pw =
new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
while ( (s = br.readLine()) != null )
if (s.indexOf(pat) > -1)
pw.println(s);
br.close();
pw.close();
}
catch (IOException e) {system.out.println(e.toString());}
}
}