n e w. s t a t i c s t a t i c 3 1
3 2 p u b l i c p r i v a t e p r o t e c t e d j a v a. l a n g. O b j e c t O b j e c t Rect R e c t x 1 y 1 x 2 y 2 R e c t t o S t r i n g ( ) j a v a. l a n g. O b j e c t R e c t t o S t r i n g ( ) t o S t r i n g ( ) R e c t S t r i n g R e c t 2-1 R e c t. j ava package com.davidflanagan.examples.classes; * * Rect public class Rect { // public int x1, y1, x2, y2; * *
3 3 public Rect(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; * public Rect(int width, int height) { this(0,0,width,height); public Rect() { this(0,0,0,0); public void move(int deltax, int deltay) { x1 += deltax; x2 += deltax; y1 += deltay; y2 += deltay; public boolean isinside(int x, int y) { return ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2)); * union * public Rect union(rect r) { return new Rect((this.x1 < r.x1)? this.x1 : r.x1, (this.y1 < r.y1)? this.y1 : r.y1, (this.x2 > r.x2)? this.x2 : r.x2, (this.y2 > r.y2)? this.y2 : r.y2);
3 4 2-1 R e c t. j ava * intersection * public Rect intersection(rect r) { Rect result = new Rect((this.x1 > r.x1)? this.x1 : r.x1, (this.y1 > r.y1)? this.y1 : r.y1, (this.x2 < r.x2)? this.x2 : r.x2, (this.y2 < r.y2)? this.y2 : r.y2); if (result.x1 > result.x2) { result.x1 = result.x2 = 0; if (result.y1 > result.y2) { result.y1 = result.y2 = 0; return result; * Object * Rect + * System.out.println() public String tostring() { return "[" + x1 + "," + y1 + "; " + x2 + "," + y2 + "]"; Rect R e c t T e s t R e c t R e c t n e w R e c t () R e c t R e c t + R e c t t o S t r i n g ()
3 5 2-2 R e c t Te s t. j ava package com.davidflanagan.examples.classes; Rect public class RectTest { public static void main(string[] args) { Rect r1 = new Rect( 1, 1, 4, 4); // Rect Rect r2 = new Rect( 2, 3, 5, 6); Rect u = r1.union(r2); // Rect Rect i = r2.intersection(r1); if (u.isinside(r2.x1, r2.y1)) // Rect Rect System.out.println("(" + r2.x1 + "," + r2.y1 + ") is inside the union"); // Rect.toString() System.out.println(r1 + " union " + r2 + " = " +u); System.out.println(r1 + " intersect " + r2 + " = " + i); Rect R e c t D r a w a b l e R e c t R e c t d r a w () j a v a. a w t. G r a p h i c s G r a p h i c s D r a w a b l e R e c t R e c t e x t e n d s R e c t D r a w a b l e R e c t 2-3 D rawabl e R e c t. j ava package com.davidflanagan.examples.classes; * Rect * Rect * java.awt.graphics
3 6 2-3 D rawab l e R e c t. j ava public class DrawableRect extends Rect { DrawableRect Rect() public DrawableRect(int x1, int y1, int x2, int y2) { super(x1,y1,x2,y2); DrawableRect public void draw(java.awt.graphics g) { g.drawrect(x1, y1, (x2 - x1), (y2 - y1)); C o l o r e d R e c t D r a w a b l e R e c t R e c t D r a w a b l e R e c t R e c t O b j e c t C o l o r e d R e c t j a v a. a w t. C o l o r C o l o r e d R e c t D r a w a b l e R e c t d r a w () C o l o r e d R e c t d r a w () D r a w a b l e R e c t 2-4 C o l o re d R e c t. j ava package com.davidflanagan.examples.classes; import java.awt.*; * DrawableRect public class ColoredRect extends DrawableRect { // // x1 y1 x2 y2 super-superclass Rect protected Color border, fill;
3 7 * super() * public ColoredRect(int x1, int y1, int x2, int y2, Color border, Color fill) { super(x1, y1, x2, y2); this.border = border; this.fill = fill; * draw() public void draw(graphics g) { g.setcolor(fill); g.fillrect(x1, y1, (x2 - x1), (y2 - y1)); g.setcolor(border); g.drawrect(x1, y1, (x2 - x1), (y2 - y1)); C o m p l e x N u m b e r d o u b l e p r i v a t e r e a l () i m a g i n a r y () C o m p l e x N u m b e r C o m p l e x N u m b e r
3 8 C o m p l e x N u m b e r a d d () m u l t i p l y () a d d () C o m p l e x N u m b e r C o m p l e x N u m b e r C o m p l e x N u m b e r ComplexNumber sum = a.add(b); ComplexNumber sum = ComplexNumber.add(a, b); 2-5 C o m p l ex N u m b e r. j ava package com.davidflanagan.examples.classes; public class ComplexNumber { // ComplexNumber double x y // // real() imaginary() private double x, y; x y public ComplexNumber(double real, double imaginary) { this.x = real; this.y = imaginary; * * setreal() * ComplexNumber public double real() { return x; public double imaginary() { return y;
3 9 public double magnitude() { return Math.sqrt(x*x + y*y); * ComplexNumber * Object * System.out.println() public String tostring() { return "{" + x + "," + y + ""; * * this * * ComplexNumber c = ComplexNumber.add(a, b) public static ComplexNumber add(complexnumber a, ComplexNumber b) { return new ComplexNumber(a.x + b.x, a.y + b.y); * * * ComplexNumber c = a.add(b); public ComplexNumber add(complexnumber a) { return new ComplexNumber(this.x + a.x, this.y + a.y); public static ComplexNumber multiply(complexnumber a, ComplexNumber b) { return new ComplexNumber(a.x*b.x - a.y*b.y, a.x*b.y + a.y* b.x); public ComplexNumber multiply(complexnumber a) { return new ComplexNumber(x*a.x - y*a.y, x*a.y + y*a.x);
4 0 R a n d o m i z e r R a n d o m i z e r s e e d F a c t o r i a l. f a c t o r i a l () f a c t o r i a l () R a n d o m i z e r r a n d o m () R a n d o m i z e r s e e d R a n d o m i z e r s t a t i c f i n a l s t a t i c f i n a l 2-6 R a n d o m i z e r. j ava package com.davidflanagan.examples.classes; * * public class Randomizer { // static final static final int m = 233280; static final int a = 9301; static final int c = 49297; // Randomizer long seed = 1;
4 1 * Randomizer * pseudo-randomness public Randomizer(long seed) { this.seed = seed; * 0 1 * Math.random() java.util.random public float randomfloat() { seed = (seed * a + c) % m; return (float)seed / (float)m; * 0 * randomfloat() public int randomint(int max) { return Math.round(max * randomfloat()); * 10 * Randomizer public static class Test { public static void main(string[] args) { Randomizer r = new Randomizer(new java.util.date().gettime()); for(int i = 0; i < 10; i++) System.out.println(r.randomInt(100));
4 2 R a n d o m i z e r T e s t R a n d o m i z e r. T e s t m a i n () R a n d o m i z e r R a n d o m i z e r. T e s t % java Randomizer.Test R a n d o m i z e r. T e s t $ % java Randomizer$Test $ \ % java Randomizer\$Test % java 'Randomizer$Test' a d d D a t u m () A v e r a g e r R a n d o m i z e r A v e r a g e r A v e r a g e r A v e r a g e r R a n d o m i ze r. j av a p a c k a g e % j a v a c o m. d a v i d f l a n a g a n. e x a m p l e s. c l a s s e s. R a n d o m i z e r $ T e s t
4 3 m a i n ( ) T e s t A v e r a g e r 2-7 Ave rage r. j ava package com.davidflanagan.examples.classes; * public class Averager { // private int n = 0; private double sum = 0.0, sumofsquares = 0.0; * public void adddatum(double x) { n++; sum += x; sumofsquares += x * x; adddatum() public double getaverage() { return sum/n; adddatum() public double getstandarddeviation() { return Math.sqrt(((sumOfSquares - sum*sum/n)/n)); adddatum() public double getnum() { return n; adddatum() public double getsum() { return sum; adddatum()
4 4 2-7 Ave rage r. j ava public double getsumofsquares() { return sumofsquares; Averager public void reset() { n = 0; sum = 0.0; sumofsquares = 0.0; * * public static class Test { public static void main(string args[]) { Averager a = new Averager(); for(int i = 1; i <= 100; i++) a.adddatum(i); System.out.println("Average: " + a.getaverage()); System.out.println("Standard Deviation: " + a.getstandarddeviation()); System.out.println("N: " + a.getnum()); System.out.println("Sum: " + a.getsum()); System.out.println("Sum of squares: " + a.getsumofsquares()); L i n k e d L i s t L i n k a b l e L i n k e d L i s t L i n k a b l e i m p l e m e n t s L i n k a b l e L i n k a b l e L i n k e d L i s t L i n k a b l e
4 5 j a v a. u t i l. L i n k e d L i s t L i n k e d L i s t L i n k e d L i s t h e a d L i n k a b l e L i n k a b l e L i n k e d L i s t L i n k a b l e L i n k e d L i s t T e s t L i n k e d L i s t T e s t L i n k a b l e I n t e g e r L i n k a b l e I n t e g e r L i n k a b l e 2-8 L i n ke d L i s t. j ava package com.davidflanagan.examples.classes; * Linkable * * public class LinkedList { public interface Linkable { public Linkable getnext(); // public void setnext(linkable node); // // public LinkedList() { Linkable head; node
4 6 2-8 L i n ke d L i s t. j ava public synchronized Linkable gethead() { return head; public synchronized void insertathead(linkable node) { node.setnext(head); head = node; public synchronized void insertattail(linkable node) { if (head == null) head = node; else { Linkable p, q; for(p = head; (q = p.getnext())!= null; p = q); p.setnext(node); public synchronized Linkable removefromhead() { Linkable node = head; if (node!= null) { head = node.getnext(); node.setnext(null); return node; public synchronized Linkable removefromtail() { if (head == null ) return null; Linkable p = head, q = null, next = head.getnext(); if (next == null) { head = null; return p;
4 7 while((next = p.getnext())!= null) { q = p; p = next; q.setnext(null); return p; * * equals() == public synchronized void remove(linkable node) { if (head == null) return; if (node.equals(head)) { head = head.getnext(); return; Linkable p = head, q = null; while((q = p.getnext())!= null) { if (node.equals(q)) { p.setnext(q.getnext()); return; p = q; * main() LinkedList public static class Test { Linkable static class LinkableInteger implements Linkable { int i; // Linkable next; // public LinkableInteger(int i) { this.i = i; public Linkable getnext() { return next; // // Linkable
4 8 2-8 L i n ke d L i s t. j ava public void setnext(linkable node) { next = node; // Linkable public String tostring() { return i + ""; // public boolean equals(object o) { // if (this == o) return true; if (!(o instanceof LinkableInteger)) return false; if (((LinkableInteger)o).i == this.i) return true; return false; * * 4 6 3 1 5 public static void main(string[] args) { LinkedList ll = new LinkedList(); ll.insertathead(new LinkableInteger(1)); ll.insertathead(new LinkableInteger(2)); ll.insertathead(new LinkableInteger(3)); ll.insertathead(new LinkableInteger(4)); ll.insertattail(new LinkableInteger(5)); ll.insertattail(new LinkableInteger(6)); System.out.println(ll.removeFromHead()); System.out.println(ll.removeFromTail()); ll.remove(new LinkableInteger(2)); // // // // // // for(linkable l = ll.gethead(); l!= null; l = l.getnext()) System.out.println(1);
4 9 S o r t e r S o r t e r s o r t () s o r t () s o r t () j a v a. u t i l. L o c a l e j a v a. t e x t. C o l l a t o r j a v a. t e x t. C o l l a t i o n K e y S o r t e r S o r t e r C o m p a r e r C o m p a r a b l e C o m p a r e r s o r t () C o m p a r e r c o m p a r e () C o m p a r e r C o m p a r a b l e c o m p a r e T o () j a v a. u t i l. A r r a y s s o r t () j a v a. u t i l. C o l l e c t i o n s s o r t () j a v a. u t i l. L i s t A r r a y s C o l l e c t i o n s j a v a. u t i l. C o m p a r a t o r j a v a. l a n g. C o m p a r a b l e C o m p a r e r C o m p a r a b l e T e s t
5 0 2-9 S o rt e r. j ava package com.davidflanagan.examples.classes; // import java.text.collator; import java.text.collationkey; import java.util.locale; * String * public class Sorter { * compare() * Comparer * compare() public static interface Comparer { * * if (a > b) return > 0; * if (a == b) return 0; * if (a < b) return < 0; public int compare(object a, Object b); * * Comparable * compareto() public static interface Comparable { * * if (this > other) return > 0; * if (this == other) return 0;
5 1 * if (this < other) return <0; public int compareto(object other); * Comparer * ASCII sortascii private static Comparer ascii_comparer = new Comparer() { public int compare(object a, Object b) { return ((String)a).compareTo((String)b); ; * Comparer Comparable * sort() private static Comparer comparable_comparer = new Comparer() { public int compare(object a, Object b) { return ((Comparable)a).compareTo(b); ; ASCII public static void sortascii(string[] a) { // ascii_comparer sort(a, null, 0, a.length-1, true, ascii_comparer); * up ASCII public static void sortascii(string[] a, int from, int to, boolean up) { // ascii_comparer sort(a, null, from, to, up, ascii_comparer);
5 2 2-9 S o rt e r. j ava ASCII public static void sortasciiignorecase(string[] a) { sortasciiignorecase(a, 0, a.length-1, true); * ASCII * up true public static void sortasciiignorecase(string[] a, int from, int to, boolean up) { if ((a == null) (a.length < 2)) return; // String b[] = new String[a.length]; for(int i = 0; i < a.length; i++) b[i] = a[i].tolowercase(); // // ascii_comparer sort(b, a, from, to, up, ascii_comparer); * locale collation order * public static void sort(string[] a) { sort(a, 0, a.length-1, true, false, null); * * up true * ignorecase true public static void sort(string[] a, int from, int to, boolean up, boolean ignorecase) { sort(a, from, to, up, ignorecase, null);
5 3 * * up true * ignorecase true public static void sort(string[] a, int from, int to, boolean up, boolean ignorecase, Locale locale) { // if ((a == null) (a.length < 2)) return; // java.text.collator // Collator c; if (locale == null) c = Collator.getInstance(); else c = Collator.getInstance(locale); // // JDK 1.1.1 American English // if (ignorecase) c.setstrength(collator.secondary); // Collator CollationKey // CollationKey String CollationKey[] b = new CollationKey[a.length]; for(int i = 0; i < a.length; i++) b[i] = c.getcollationkey(a[i]); // Comparer CollationKey Comparer comp = new Comparer() { public int compare(object a, Object b) { return ((CollationKey)a).compareTo((CollationKey)b); ; // CollationKey // sort(b, a, from, to, up, comp);
5 4 2-9 S o rt e r. j ava Comparable public static void sort(comparable[] a) { sort(a, null, 0, a.length-1, true); * Comparable * up true public static void sort(comparable[] a, int from, int to, boolean up) { sort(a, null, from, to, up, comparable_comparer); * Comparable * up true * a b public static void sort(comparable[] a, Object[] b, int from, int to, boolean up) { sort(a, b, from, to, up, comparable_comparer); * Comparer c * public static void sort(object[] a, Comparer c) { sort(a, null, 0, a.length-1, true, c); * Comparer c * up true
5 5 public static void sort(object[] a, int from, int to, boolean up, Comparer c) { sort(a, null, from, to, up, c); * sort() routine * from to a * up true false * Comparer c * a b public static void sort(object[] a, Object[] b, int from, int to, boolean up, Comparer c) { // sort if ((a == null) (a.length < 2)) return; // // // // // // Comparer compare() int i = from, j = to; Object center = a[(from + to) / 2]; do { if (up) { // while((i < to) && (c.compare(center, a[i]) > 0)) i++; while((j > from) && (c.compare(center, a[j]) < 0)) j--; else { // while(( i < to) && (c.compare(center, a[i]) < 0)) i++; while((j > from) && (c.compare(center, a[j]) > 0)) j--; if (i < j) { Object tmp = a[i]; a[i] = a[j]; a[j] = tmp; // if (b!= null) { tmp = b[i]; b[i] = b[j]; b[j] = tmp; //
5 6 2-9 S o rt e r. j ava if (i <= j) { i++; j--; while(i <= j); if (from < j) sort(a, b, from, j, up, c); // if ( i < to) sort(a, b, i, to, up, c); * * Sorter ComplexNumber public static class Test { * ComplexNumber Comparable * compareto() * magnitude * static class SortableComplexNumber extends ComplexNumber implements Sorter.Comparable { public SortableComplexNumber(double x, double y) { super(x, y); public int compareto(object other) { return sign(this.magnitude() - ((ComplexNumber)other).magnitude()); public static void main(string[] args) { // SortableComplexNumber SortableComplexNumber[] a = new SortableComplexNumber[5]; for(int i = 0; i < a.length; i++) a[i] = new SortableComplexNumber(Math.random() *10, Math.random()*10); // SortableComplexNumber compareto() System.out.println("Sorted by magnitude:"); Sorter.sort(a);
5 7 for(int i = 0; i < a.length; i++) System.out.println(a[i]); // Comparer // System.out.println("Sorted by sum of their real and imaginary parts:"); Sorter.sort(a, new Sorter.Comparer() { public int compare(object a, Object b) { ComplexNumber i = (ComplexNumber)a, j = (ComplexNumber)b; return sign((i.real() + i.imaginary()) - (j.real() + j.imaginary())); ); for(int i = 0; i < a.length; i++) System.out.println(a[i]); // Comparer // System.out.println("Sorted descending by real part, then imaginary:"); Sorter.sort(a, 0, a.length-1, false, new Sorter.Comparer() { public int compare(object a, Object b) { ComplexNumber i = (ComplexNumber) a, j = (ComplexNumber) b; double result = i.real() - j.real(); if (result == 0) result = i.imaginary() - j.imaginary(); return sign(result); ); for(int i = 0; i < a.length; i++) System.out.println(a[i]); sort public static int sign(double x) { if (x > 0) return 1; else if (x < 0) return -1; else return 0;
5 8 R e c t C i r c l e m o v e () i s I n s i d e () b o u n d i n g B o x () C i r c l e R e c t t o S t r i n g () S o r t. C o m p a r e r S o r t. C o m p a r a b l e S e a r c h s e a r c h () s e a r c h ()