javaexample-02.pdf

Similar documents
新・解きながら学ぶJava

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Microsoft Word - 01.DOC

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

untitled


Microsoft Word - 第3章.doc

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP: ******************* * 关于 Java 测试试题 ******

Chapter 9: Objects and Classes

JavaIO.PDF

untitled

软件工程文档编制

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

chp6.ppt

Java

Microsoft Word - ch04三校.doc

Chapter 9: Objects and Classes

《大话设计模式》第一章

3.1 num = 3 ch = 'C' 2

EJB-Programming-4-cn.doc

untitled

java2d-4.PDF

CHAPTER VC#

FY.DOC

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

CHAPTER 1

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

untitled

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Microsoft Word - mei.doc

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

第5章修改稿

新版 明解C++入門編

untitled

全国计算机技术与软件专业技术资格(水平)考试

Swing-02.pdf

CC213

Microsoft Word - 第6章.doc

Microsoft PowerPoint - course2.ppt

untitled

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

第3章.doc

EJB-Programming-3.PDF

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Microsoft Word - 新1-12.doc

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

北 风 网 讲 师 原 创 作 品 ---- 仅 供 学 员 内 部 交 流 使 用 前 言 吾 尝 终 日 而 思 矣, 不 如 须 臾 之 所 学 也 ; 吾 尝 跂 而 望 矣, 不 如 登 高 之 博 见 也 登 高 而 招, 臂 非 加 长 也, 而 见

JAVA String常用APi

untitled

untitled

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

Adobe® Flash® 的 Adobe® ActionScript® 3.0 程式設計

C 1

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票

附录J:Eclipse教程

第一章 章标题-F2 上空24,下空24

基于CDIO一体化理念的课程教学大纲设计

Microsoft PowerPoint - ch02

新版 明解C言語入門編

<4D F736F F F696E74202D20B5DA3035D5C220C3E6CFF2B6D4CFF3B8DFBCB6B3CCD0F2C9E8BCC6>

JBuilder Weblogic

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点

untitled

Visual Basic D 3D

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

Microsoft PowerPoint - 14Threads.ppt

Strings

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

Learning Java

untitled

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

C/C++程序设计 - 字符串与格式化输入/输出

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

Chapter12 Derived Classes

51 C 51 isp 10 C PCB C C C C KEIL

Microsoft Word - Learn Objective-C.doc

Java第一天 年4月23日

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

Java 1 Java String Date

09 (File Processes) (mkdir) 9-3 (createnewfile) 9-4 (write) 9-5 (read) 9-6 (deletefile) 9-7 (deletedir) (Exercises)

chap07.key

untitled

[剑指offer] 面试题43:n个骰子的点数(Java),[剑指offer] 面试题42: 翻转单词顺序 VS左旋转字符串(Java),[剑指offer] 面试题41:和为s的两个数字VS和为s的连续序列

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

(京)新登字063号

,,,,,,,,,,,,, :,, ;,,,,, ( ),,,, : ( ) ; ( ) ; ( ) ( ) ; ( ) ( A ) ; ( ) ( ),,,,,,, 80

Transcription:

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 ()