├── 23-Spring ├── README.md ├── ref_source │ ├── ArrayDemo.java │ ├── FileIO │ │ ├── FileIO.java │ │ └── doc │ │ │ ├── FileIO.html │ │ │ ├── allclasses-frame.html │ │ │ ├── allclasses-noframe.html │ │ │ ├── constant-values.html │ │ │ ├── deprecated-list.html │ │ │ ├── help-doc.html │ │ │ ├── index-all.html │ │ │ ├── index.html │ │ │ ├── overview-tree.html │ │ │ ├── package-frame.html │ │ │ ├── package-list │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ ├── script.js │ │ │ └── stylesheet.css │ ├── MyList.java │ └── StringDemo.java └── slides │ ├── Lab1.pdf │ ├── Lab2.pdf │ ├── Lab3.pdf │ ├── Lab4.pdf │ ├── Lab5.pdf │ ├── Lab6.pdf │ ├── Lab7.pdf │ ├── Lab8.pdf │ ├── Lab9.pdf │ ├── course_review.pdf │ ├── lecture1.pdf │ ├── lecture10.pdf │ ├── lecture11.pdf │ ├── lecture12.pdf │ ├── lecture13.pdf │ ├── lecture2.pdf │ ├── lecture3.pdf │ ├── lecture4.pdf │ ├── lecture5.pdf │ ├── lecture6.pdf │ ├── lecture7.pdf │ ├── lecture8.pdf │ └── lecture9.pdf ├── 24-Spring ├── README.md ├── ref_source │ ├── ArrayDemo.java │ ├── FileIO │ │ ├── FileIO.java │ │ └── doc │ │ │ ├── FileIO.html │ │ │ ├── allclasses-frame.html │ │ │ ├── allclasses-noframe.html │ │ │ ├── constant-values.html │ │ │ ├── deprecated-list.html │ │ │ ├── help-doc.html │ │ │ ├── index-all.html │ │ │ ├── index.html │ │ │ ├── overview-tree.html │ │ │ ├── package-frame.html │ │ │ ├── package-list │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ ├── script.js │ │ │ └── stylesheet.css │ ├── MyList.java │ └── StringDemo.java └── slides │ ├── Lab1.pdf │ ├── Lab2.pdf │ ├── lecture1.pdf │ ├── lecture10.pdf │ ├── lecture11.pdf │ ├── lecture12.pdf │ ├── lecture13.pdf │ ├── lecture2.pdf │ ├── lecture3.pdf │ ├── lecture4.pdf │ ├── lecture5.pdf │ ├── lecture6.pdf │ ├── lecture7.pdf │ ├── lecture8.pdf │ └── lecture9.pdf ├── 25-Spring ├── README.md ├── ref_source │ ├── ArrayDemo.java │ ├── FileIO │ │ ├── FileIO.java │ │ └── doc │ │ │ ├── FileIO.html │ │ │ ├── allclasses-frame.html │ │ │ ├── allclasses-noframe.html │ │ │ ├── constant-values.html │ │ │ ├── deprecated-list.html │ │ │ ├── help-doc.html │ │ │ ├── index-all.html │ │ │ ├── index.html │ │ │ ├── overview-tree.html │ │ │ ├── package-frame.html │ │ │ ├── package-list │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ ├── script.js │ │ │ └── stylesheet.css │ ├── MyList.java │ └── StringDemo.java └── slides │ ├── Lab1.pdf │ ├── Lab2.pdf │ ├── Lab3.pdf │ ├── Lab4.pdf │ ├── course_review.pdf │ ├── lecture1.pdf │ ├── lecture10.pdf │ ├── lecture11.pdf │ ├── lecture12.pdf │ ├── lecture13.pdf │ ├── lecture2.pdf │ ├── lecture3.pdf │ ├── lecture4.pdf │ ├── lecture5.pdf │ ├── lecture6.pdf │ ├── lecture7.pdf │ ├── lecture8.pdf │ └── lecture9.pdf ├── README.md └── projects ├── build_data_types ├── README.md ├── big_integer.md ├── complex.md ├── docs │ ├── StdDraw.html │ └── StdDraw_files │ │ └── stylesheet.css ├── fractal.md ├── images │ ├── brown1.png │ ├── brown2.png │ ├── brown3.png │ ├── brown4.png │ ├── complex_polar.png │ ├── dragon0.png │ ├── dragon1.png │ ├── dragon2.png │ ├── dragon3.png │ ├── dragon4.png │ ├── dragon5.png │ ├── dragon6.png │ ├── dragon7.png │ ├── dragon8.png │ ├── dragon9.png │ ├── fudgeflake0.png │ ├── fudgeflake1.png │ ├── fudgeflake2.png │ ├── fudgeflake3.png │ ├── fudgeflake4.png │ ├── gosper0.png │ ├── gosper1.png │ ├── gosper2.png │ ├── gosper3.png │ ├── gosper4.png │ ├── hilbert0.png │ ├── hilbert1.png │ ├── hilbert2.png │ ├── hilbert3.png │ ├── hilbert4.png │ ├── koch0.png │ ├── koch1.png │ ├── koch2.png │ ├── koch3.png │ ├── koch_snow0.png │ ├── koch_snow1.png │ ├── koch_snow2.png │ ├── koch_snow3.png │ ├── kochrainbow1.png │ ├── levy0.png │ ├── levy1.png │ ├── levy10.png │ ├── levy2.png │ ├── levy3.png │ ├── levy4.png │ ├── levy5.png │ ├── levy6.png │ ├── levy7.png │ ├── levy8.png │ ├── levy9.png │ ├── ngon3.png │ ├── ngon4.png │ ├── ngon5.png │ ├── ngon6.png │ ├── sausage0.png │ ├── sausage1.png │ ├── sausage2.png │ ├── sausage3.png │ ├── sausage4.png │ ├── sierpinski-curve0.png │ ├── sierpinski-curve1.png │ ├── sierpinski-curve2.png │ ├── sierpinski-curve3.png │ ├── sierpinski-curve4.png │ ├── spiral3.png │ ├── spiral4.png │ ├── spiral5.png │ └── spiral6.png └── src │ └── fractal │ └── StdDraw.java ├── container_io ├── README.md ├── container.md ├── images │ ├── queue.jpg │ └── queue.odp ├── io.md └── printer.md ├── familiar_with_toolkits └── README.md ├── first_java └── README.md ├── inheritance ├── README.md ├── circuits.md ├── images │ └── circuits │ │ ├── bigcircuit.png │ │ ├── bigcircuit2.png │ │ ├── circuit.png │ │ ├── circuit2.png │ │ ├── circuit2withbattery.png │ │ ├── combination.png │ │ ├── parallel3.png │ │ ├── series3.png │ │ └── seriesparallel.png └── inheritance_basic.md ├── interface ├── README.md ├── function.md ├── interface_basic.md └── list.md ├── primitive_ctlflow └── README.md └── use_data_types ├── Picture.java ├── Picture ├── Picture.html └── Picture_files │ └── stylesheet.css ├── README.md ├── image_processing.md ├── images ├── baboon-grayscale.jpg ├── baboon.jpg ├── darwin.jpg ├── mandrill.jpg ├── mandrill200x200.jpg └── mandrill200x400.jpg ├── string.md └── svg ├── 2dDCnouYkM.svg ├── Jg0mTMrYI2.svg ├── SrlotW62jd.svg └── cPbkSX0r9M.svg /23-Spring/README.md: -------------------------------------------------------------------------------- 1 | # 面向对象程序设计 Java 2 | 3 | Dep. of Computer Science, ECNU, Spring 2023 4 | 5 | ## 时间地点 6 | 7 | * 理论课: 周四 8:00 - 9:35 @ 教书院226 8 | * 实验课: 周四 13:00 - 14:35 @ 理科楼B116 9 | 10 | ## 联系信息 11 | 12 | * 教师: 吴苑斌 13 | * Email: ybwu\[at\]cs.ecnu.edu.cn 14 | * 办公室: 理科楼B911 15 | 16 | * 助教: 杨润东 17 | * Email: 10205102454\[at\]stu.ecnu.edu.cn 18 | 19 | * 助教: 刘诺纬 20 | * Email: 10191900446\[at\]stu.ecnu.edu.cn 21 | 22 | ## 答疑 23 | 24 | * 周三 13:00 - 15:00 @ 理科楼B911 25 | 26 | ## 参考资料 27 | 28 | * Thinking in Java (4th Edition), Bruce Eckel 29 | * Java 编程编程思想 (第四版) 30 | 31 | ## 讲义 32 | 33 | * [Lecture 1](slides/lecture1.pdf) 34 | * [Lecture 2](slides/lecture2.pdf) 35 | * [Lecture 3](slides/lecture3.pdf) 36 | * [Lecture 4](slides/lecture4.pdf) 37 | * [Lecture 5](slides/lecture5.pdf) 38 | * [Lecture 6](slides/lecture6.pdf) 39 | * [Lecture 7](slides/lecture7.pdf) 40 | * [Lecture 8](slides/lecture8.pdf) 41 | * [Lecture 9](slides/lecture9.pdf) 42 | * [Lecture 10](slides/lecture10.pdf) 43 | * [Lecture 11](slides/lecture11.pdf) 44 | * [Lecture 12](slides/lecture12.pdf) 45 | * [Lecture 13](slides/lecture13.pdf) 46 | * [review](slides/course_review.pdf) 47 | 48 | ## 实验 49 | 50 | ### 提交信息 51 | 52 | 提交地址: ecnu_java_2023\[at\]163.com 53 | 54 | 提交格式: 实验编号_学号_姓名.zip (例如: Lab1_10215102000_陈东.zip) 55 | 56 | ### 实验讲义及相关资源 57 | 58 | - [Lab 1 slides](slides/Lab1.pdf) 59 | - [Lab 2 slides](slides/Lab2.pdf), [ArrayDemo.java](ref_source/ArrayDemo.java), [StringDemo.java](ref_source/StringDemo.java) 60 | - [Lab 3 slides](slides/Lab3.pdf) 61 | - [Lab 4 slides](slides/Lab4.pdf) 62 | - [Lab 5 slides](slides/Lab5.pdf) 63 | - [Lab 6 slides](slides/Lab6.pdf) 64 | - [Lab 7 slides](slides/Lab7.pdf) 65 | - [Lab 8 slides](slides/Lab8.pdf) 66 | - [Lab 9 slides](slides/Lab9.pdf) 67 | - 简便的文件输入包 [FileIO.java](ref_source/FileIO/FileIO.java)及[文档](ref_source/FileIO/doc/index.html) 68 | 69 | ### 实验内容 70 | 71 | * Project 1: [第一个 Java 程序](../projects/first_java/README.md) 72 | * Project 2: [基本类型与控制流](../projects/primitive_ctlflow/README.md), [补充实验](../projects/familiar_with_toolkits/README.md) 73 | * Project 3: [使用 Java 包](../projects/use_data_types/README.md) 74 | * Project 4: [创建 Java 类](../projects/build_data_types/README.md) 75 | * Project 5: [继承与组合](../projects/inheritance/README.md) 76 | * Project 6: [接口](../projects/interface/README.md) (资源: [链表](ref_source/tutorial_LinkedList.pdf), [MyList.java](ref_source/MyList.java)) 77 | * Project 7: [容器与I/O](../projects/container_io/README.md) 78 | 80 | -------------------------------------------------------------------------------- /23-Spring/ref_source/ArrayDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class ArrayDemo{ 4 | public static void main(String[] args) { 5 | 6 | // // 数组的定义形式 7 | // int []arrayInt; 8 | // // 数组的创建 9 | // arrayInt = new int[10]; 10 | 11 | // // // 可以在声明数组时直接初始化 12 | // int []arrayInt = {1,2,3,4,5}; 13 | 14 | // // System.out.println(arrayInt[0]); 15 | // // System.out.println(arrayInt[1]); 16 | // // System.out.println(arrayInt[2]); 17 | // // System.out.println(arrayInt[3]); 18 | // // System.out.println(arrayInt[4]); 19 | // // // error 20 | // // System.out.println(arrayInt[5]); 21 | // // System.out.println(arrayInt.length); 22 | 23 | // // 其他类型的数组 24 | // // double []arrayDouble; 25 | // // String []arrayString; 26 | 27 | // // 二维数组的定义形式: 28 | // int[][] array; 29 | // int array2[][]; 30 | 31 | // // 方法一:直接为每一维分配空间 32 | // int a[][] = new int[2][3]; 33 | // System.out.println(a.length); 34 | // System.out.println(a[0].length); 35 | 36 | // 方法二:从最高维开始,分别为每一维分配空间 37 | // int a[][] = new int[2][]; 38 | 39 | // System.out.println(a.length); 40 | 41 | // a[0] = new int[3]; 42 | // a[1] = new int[2]; 43 | // System.out.println(a[0].length); 44 | // System.out.println(a[1].length); 45 | 46 | // 多维数组初始化方法 47 | // (1)直接对每个元素进行赋值 48 | // (2)在定义数组的同时进行初始化 49 | // int a[][] = {{1,2},{3,4},{5,6}}; 50 | // for(int i = 0; i < a.length; i++) { 51 | // for(int j = 0 ; j < a[0].length; j++) { 52 | // System.out.print(a[i][j] + "\t"); 53 | // } 54 | // System.out.println(); 55 | // } 56 | 57 | 58 | 59 | 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/FileIO.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.ArrayList; 3 | 4 | /** 5 | * 这个类用于文件读写,便于同学后续写作业的时候使用 6 | * @author 王海洲,吴苑斌 7 | * @version 1.0 8 | */ 9 | 10 | public class FileIO{ 11 | public static void main(String args[]) {} 12 | 13 | /** 14 | * 这个方法将一个字符串追加地写入指定文件(支持中文)。 15 | * @param str String类型,要写入的字符。 16 | * @param fileName String类型, 文件名。 17 | * @exception Exception 输出流产生错误时。 18 | * @return void,无返回值。 19 | */ 20 | public static void writeStringToFile(String str, String fileName) { 21 | BufferedWriter bw = null; 22 | try { 23 | bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "UTF-8")); 24 | bw.write(str); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } finally { 28 | try { 29 | bw.close(); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | 37 | /** 38 | * 这个方法将读取指定文件的第pos个字符(支持中文)。 39 | * @param pos int类型, 要获取的字符的位置。 40 | * @param fileName String类型, 文件名。 41 | * @exception Exception 输入流产生错误或者文件不存在时。 42 | * @return char, 如果pos的值为负数或者大于文件内容长度,那么将返回0。如果正常,将返回该文件指定位置的字符。 43 | */ 44 | public static char getCharFromFile(int pos, String fileName) { 45 | char res = 0; 46 | BufferedReader br = null; 47 | int len = 0; 48 | try { 49 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 50 | String temp = null; 51 | while((temp = br.readLine()) != null && pos >= 0) { 52 | if(pos < temp.length()) { 53 | res = temp.charAt(pos); 54 | } 55 | pos -= temp.length(); 56 | } 57 | } catch (Exception e) { 58 | e.printStackTrace(); 59 | } finally { 60 | try { 61 | br.close(); 62 | } catch (Exception e) { 63 | e.printStackTrace(); 64 | } 65 | } 66 | return res; 67 | } 68 | 69 | 70 | /** 71 | * 这个方法将读取指定文件的第pos行的字符串(支持中文)。 72 | * @param pos int类型, 要获取的字符串的行的位置(从0开始)。 73 | * @param fileName String类型, 文件名。 74 | * @exception Exception 输入流产生错误或者文件不存在时。 75 | * @return String, 如果行下标pos为负数或者大于等于行数,那么将返回null。如果正常,将返回该文件指定行的字符串。 76 | */ 77 | public static String getLineFromFile(int pos, String fileName) { 78 | BufferedReader br = null; 79 | String res = null; 80 | try { 81 | 82 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 83 | String temp = null; 84 | int cnt = 0; 85 | while((temp = br.readLine()) != null && pos >= 0) { 86 | if(pos == 0) { 87 | res = temp; 88 | } 89 | pos--; 90 | } 91 | } catch (Exception e) { 92 | e.printStackTrace(); 93 | } finally { 94 | try { 95 | br.close(); 96 | } catch (Exception e) { 97 | e.printStackTrace(); 98 | } 99 | } 100 | return res; 101 | } 102 | 103 | 104 | /** 105 | * 这个方法将读取指定文件的所有行的字符串,并按照顺序储存在String类型数组中返回(支持中文)。 106 | * @param fileName String类型, 文件名。 107 | * @exception Exception 输入流产生错误或者文件不存在时。 108 | * @return String[], 文件中的内容将按照行号依次储存在String数组中。 109 | */ 110 | public static String[] getAllLinesFromFile(String fileName) { 111 | int numLines = 0; 112 | BufferedReader br = null; 113 | ArrayList strList = new ArrayList(); 114 | try { 115 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 116 | 117 | String temp = null; 118 | while((temp = br.readLine()) != null){ 119 | strList.add(temp); 120 | } 121 | numLines = strList.size(); 122 | } catch (Exception e) { 123 | e.printStackTrace(); 124 | } finally { 125 | try { 126 | br.close(); 127 | } catch (Exception e) { 128 | e.printStackTrace(); 129 | } 130 | } 131 | String[] res = new String[numLines]; 132 | for(int i = 0; i < numLines; i++) { 133 | res[i] = strList.get(i); 134 | } 135 | return res; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/allclasses-noframe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/constant-values.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 常量字段值 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

常量字段值

72 |

目录

73 |
74 | 75 |
76 | 77 | 78 | 79 | 80 | 81 | 82 | 90 |
91 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/deprecated-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 已过时的列表 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

已过时的 API

72 |

目录

73 |
74 | 75 |
76 | 77 | 78 | 79 | 80 | 81 | 82 | 90 |
91 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/index-all.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
F G M W  71 | 72 | 73 |

F

74 |
75 |
FileIO - <Unnamed>中的类
76 |
77 |
这个类用于文件读写,便于同学后续写作业的时候使用
78 |
79 |
FileIO() - 类 的构造器FileIO
80 |
 
81 |
82 | 83 | 84 | 85 |

G

86 |
87 |
getAllLinesFromFile(String) - 类 中的静态方法FileIO
88 |
89 |
这个方法将读取指定文件的所有行的字符串,并按照顺序储存在String类型数组中返回(支持中文)。
90 |
91 |
getCharFromFile(int, String) - 类 中的静态方法FileIO
92 |
93 |
这个方法将读取指定文件的第pos个字符(支持中文)。
94 |
95 |
getLineFromFile(int, String) - 类 中的静态方法FileIO
96 |
97 |
这个方法将读取指定文件的第pos行的字符串(支持中文)。
98 |
99 |
100 | 101 | 102 | 103 |

M

104 |
105 |
main(String[]) - 类 中的静态方法FileIO
106 |
 
107 |
108 | 109 | 110 | 111 |

W

112 |
113 |
writeStringToFile(String, String) - 类 中的静态方法FileIO
114 |
115 |
这个方法将一个字符串追加地写入指定文件(支持中文)。
116 |
117 |
118 | F G M W 
119 | 120 |
121 | 122 | 123 | 124 | 125 | 126 | 127 | 135 |
136 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 生成的文档 (无标题) 7 | 60 | 61 | 62 | 63 | 64 | 65 | <noscript> 66 | <div>您的浏览器已禁用 JavaScript。</div> 67 | </noscript> 68 | <h2>框架预警</h2> 69 | <p>请使用框架功能查看此文档。如果看到此消息, 则表明您使用的是不支持框架的 Web 客户机。链接到<a href="FileIO.html">非框架版本</a>。</p> 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/overview-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

所有程序包的分层结构

72 |
73 |
74 |

类分层结构

75 |
    76 |
  • java.lang.Object 77 | 80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <Unnamed> 7 | 8 | 9 | 10 | 11 | 12 |

<Unnamed>

13 |
14 |

15 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/package-list: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/package-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | 30 |
31 | 58 | 59 |
60 |

程序包 <Unnamed>

61 |
62 |
63 |
    64 |
  • 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 77 | 78 | 79 |
    类概要 
    说明
    FileIO 75 |
    这个类用于文件读写,便于同学后续写作业的时候使用
    76 |
    80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/package-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

程序包<Unnamed>的分层结构

72 |
73 |
74 |

类分层结构

75 |
    76 |
  • java.lang.Object 77 | 80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /23-Spring/ref_source/FileIO/doc/script.js: -------------------------------------------------------------------------------- 1 | function show(type) 2 | { 3 | count = 0; 4 | for (var key in methods) { 5 | var row = document.getElementById(key); 6 | if ((methods[key] & type) != 0) { 7 | row.style.display = ''; 8 | row.className = (count++ % 2) ? rowColor : altColor; 9 | } 10 | else 11 | row.style.display = 'none'; 12 | } 13 | updateTabs(type); 14 | } 15 | 16 | function updateTabs(type) 17 | { 18 | for (var value in tabs) { 19 | var sNode = document.getElementById(tabs[value][0]); 20 | var spanNode = sNode.firstChild; 21 | if (value == type) { 22 | sNode.className = activeTableTab; 23 | spanNode.innerHTML = tabs[value][1]; 24 | } 25 | else { 26 | sNode.className = tableTab; 27 | spanNode.innerHTML = "" + tabs[value][1] + ""; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /23-Spring/ref_source/MyList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class StudentRecord{ 4 | public int studentID; 5 | public String name; 6 | public double gpa; 7 | public StudentRecord(int studentID, String name, double gpa) { 8 | this.studentID = studentID; 9 | this.name = name; 10 | this.gpa = gpa; 11 | } 12 | public void printData() { 13 | System.out.println("student ID:" + studentID); 14 | System.out.println("name:" + name); 15 | System.out.println("gpa:" + gpa); 16 | } 17 | } 18 | 19 | class Node{ 20 | StudentRecord data; 21 | Node next; 22 | public Node(StudentRecord data) { 23 | setData(data); 24 | setNext(null); 25 | } 26 | public void setData(StudentRecord data) { 27 | this.data = data; 28 | } 29 | public void setNext(Node next){ 30 | this.next = next; 31 | } 32 | 33 | public void printNode() { 34 | data.printData(); 35 | } 36 | } 37 | 38 | public class MyList{ 39 | private Node head; 40 | 41 | public MyList() { 42 | setHead(new Node(null)); 43 | } 44 | 45 | private void setHead(Node head) { 46 | this.head = head; 47 | } 48 | 49 | private Node getHead() { 50 | return head; 51 | } 52 | 53 | public void insert(Node node) { 54 | Node moveNode = head; 55 | while(moveNode.next != null) { 56 | moveNode = moveNode.next; 57 | } 58 | moveNode.next = node; 59 | node.next = null; 60 | } 61 | 62 | public void insert(Node node, int pos) { 63 | Node moveNode = head; 64 | for(int i = 0; i < pos; i++) { 65 | moveNode = moveNode.next; 66 | } 67 | node.next = moveNode.next; 68 | moveNode.next = node; 69 | } 70 | 71 | // write it yourself, pos 0 means delete the first node of list. 72 | public void delete(int pos) { 73 | // 74 | 75 | } 76 | 77 | public void printList() { 78 | Node moveNode = head; 79 | 80 | while(moveNode.next != null) { 81 | moveNode = moveNode.next; 82 | moveNode.printNode(); 83 | } 84 | 85 | } 86 | 87 | 88 | public static void main(String args[]) { 89 | MyList myList = new MyList(); 90 | 91 | 92 | 93 | 94 | Node n1 = new Node(new StudentRecord(101221302, "Bob", 3.5)); 95 | myList.insert(n1); 96 | 97 | Node n2 = new Node(new StudentRecord(101221303, "Mary", 3.7)); 98 | myList.insert(n2); 99 | 100 | Node n3 = new Node(new StudentRecord(777, "Jack", 1.0)); 101 | myList.insert(n3, 0); 102 | 103 | Node n4 = new Node(new StudentRecord(1788, "Tom", 10.0)); 104 | myList.insert(n4, 2); 105 | 106 | myList.printList(); 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /23-Spring/ref_source/StringDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.* ; 2 | 3 | public class StringDemo{ 4 | public static void main(String[] args) { 5 | // example 1 6 | // String str; 7 | // str = new String("Hello World!"); 8 | // System.out.println(str); 9 | // str = "Hello World!"; 10 | // System.out.println(str); 11 | 12 | // example 2 13 | 14 | // String s1 = "abc"; 15 | // String s2 = "abc"; 16 | // String s3 = s1; 17 | // System.out.println(s1 == s2); 18 | // System.out.println(s3 == s2); 19 | // System.out.println(s1.equals(s2)); 20 | // System.out.println(s2.equals(s3)); 21 | 22 | // String s4 = new String("abc"); 23 | // String s5 = new String("abc"); 24 | // System.out.println(s4 == s5); 25 | // System.out.println(s4.equals(s5)); 26 | 27 | // ==============10 basic methods==================== 28 | String str1 = " I love Java! "; 29 | String str2 = " Java DON'T LOVE ME...."; 30 | System.out.println("==== str1 and str2 ===="); 31 | System.out.println(str1); 32 | System.out.println(str2); 33 | 34 | // 返回字符串的长度 35 | System.out.println("==== int length() ===="); 36 | System.out.println(str1.length()); 37 | System.out.println(str2.length()); 38 | 39 | //将串中字符变成大写 40 | System.out.println("==== String toUpperCase() ===="); 41 | System.out.println(str1.toUpperCase()); 42 | System.out.println(str2.toUpperCase()); 43 | 44 | //将串中字符变成小写 45 | System.out.println("==== String toLowerCase() ===="); 46 | System.out.println(str1.toLowerCase()); 47 | System.out.println(str2.toLowerCase()); 48 | 49 | //返回位置i处的字符 50 | System.out.println("==== char charAt(int i) ===="); 51 | System.out.println(str1.charAt(3)); 52 | System.out.println(str1.charAt(10)); 53 | 54 | // 返回从位置s到e的字符子串[s,e) 55 | System.out.println("==== String substring(int s,int e) ===="); 56 | System.out.println(str1.substring(3,7)); 57 | System.out.println(str2.substring(6, 12)); 58 | 59 | // 返回从位置s到末尾的字符子串 60 | System.out.println("==== String substring(int s) ===="); 61 | System.out.println(str1.substring(3)); 62 | System.out.println(str2.substring(6)); 63 | 64 | // 返回首次出现字符串s的位置 65 | System.out.println("==== int indexOf(String s) ===="); 66 | System.out.println(str1.indexOf("a")); 67 | System.out.println(str1.indexOf("kkk")); 68 | 69 | // 返回在位置i之后首次出现s的位置 70 | System.out.println("==== int indexOf(String s,int i) ===="); 71 | System.out.println(str1.indexOf("a", 11)); 72 | 73 | // 返回一个新串,去除前后空白字符 74 | System.out.println("==== String trim() ===="); 75 | System.out.println(str1.trim()); 76 | System.out.println(str2.trim()); 77 | 78 | // 返回一个新串,将a替换为b 79 | System.out.println("==== String replace(String a,String b) ===="); 80 | System.out.println(str1.replace("Java", "Python")); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /23-Spring/slides/Lab1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab1.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab2.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab3.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab4.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab5.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab6.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab7.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab8.pdf -------------------------------------------------------------------------------- /23-Spring/slides/Lab9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/Lab9.pdf -------------------------------------------------------------------------------- /23-Spring/slides/course_review.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/course_review.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture1.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture10.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture11.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture12.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture13.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture2.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture3.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture4.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture5.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture6.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture7.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture8.pdf -------------------------------------------------------------------------------- /23-Spring/slides/lecture9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/23-Spring/slides/lecture9.pdf -------------------------------------------------------------------------------- /24-Spring/README.md: -------------------------------------------------------------------------------- 1 | # 面向对象程序设计 Java 2 | 3 | Dep. of Computer Science, ECNU, Spring 2024 4 | 5 | ## 时间地点 6 | 7 | * 理论课: 周四 8:00 - 9:35 @ 教书院105 8 | * 实验课: 周四 13:00 - 14:35 @ 理科楼B116 9 | 10 | ## 联系信息 11 | 12 | * 教师: 吴苑斌 13 | * Email: ybwu\[at\]cs.ecnu.edu.cn 14 | * 办公室: 理科楼B713 15 | 16 | * 助教1: 吴昊伦 17 | * Email: 2505283948\[at\]qq.com 18 | 19 | * 助教2: 缪岱烨 20 | * Email: 2825467993\[at\]qq.com 21 | 22 | ## 答疑 23 | 24 | * 周三 13:00 - 15:00 @ 理科楼B713 25 | 26 | ## 参考资料 27 | 28 | * Thinking in Java (4th Edition), Bruce Eckel 29 | * Java 编程编程思想 (第四版) 30 | 31 | ## 讲义 32 | 33 | * [Lecture 1](slides/lecture1.pdf) 34 | * [Lecture 2](slides/lecture2.pdf) 35 | * [Lecture 3](slides/lecture3.pdf) 36 | * [Lecture 4](slides/lecture4.pdf) 37 | * [Lecture 5](slides/lecture5.pdf) 38 | * [Lecture 6](slides/lecture6.pdf) 39 | * [Lecture 7](slides/lecture7.pdf) 40 | * [Lecture 8](slides/lecture8.pdf) 41 | * [Lecture 9](slides/lecture9.pdf) 42 | * [Lecture 10](slides/lecture10.pdf) 43 | * [Lecture 11](slides/lecture11.pdf) 44 | * [Lecture 12](slides/lecture12.pdf) 45 | * [Lecture 13](slides/lecture13.pdf) 46 | 47 | ## 实验 48 | 49 | ### 提交信息 50 | 51 | 提交地址: ecnu_java_2024\[at\]163.com 52 | 53 | 提交格式: 实验编号_学号_姓名.zip (例如: Lab1_10215102000_陈东.zip) 54 | 55 | ### 实验讲义及相关资源 56 | 57 | - [Lab 1 slides](slides/Lab1.pdf) 58 | - [Lab 2 slides](slides/Lab2.pdf), [ArrayDemo.java](ref_source/ArrayDemo.java), [StringDemo.java](ref_source/StringDemo.java) 59 | - 简便的文件输入包 [FileIO.java](ref_source/FileIO/FileIO.java)及[文档](ref_source/FileIO/doc/index.html) 60 | 61 | ### 实验内容 62 | 63 | * Project 1: [第一个 Java 程序](../projects/first_java/README.md) 64 | * Project 2: [基本类型与控制流](../projects/primitive_ctlflow/README.md), [补充实验](../projects/familiar_with_toolkits/README.md) 65 | * Project 3: [使用 Java 包](../projects/use_data_types/README.md) 66 | * Project 4: [创建 Java 类](../projects/build_data_types/README.md) 67 | * Project 5: [继承与组合](../projects/inheritance/README.md) 68 | * Project 6: [接口](../projects/interface/README.md) (资源: [链表](ref_source/tutorial_LinkedList.pdf), [MyList.java](ref_source/MyList.java)) 69 | * Project 7: [容器与I/O](../projects/container_io/README.md) 70 | 72 | -------------------------------------------------------------------------------- /24-Spring/ref_source/ArrayDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class ArrayDemo{ 4 | public static void main(String[] args) { 5 | 6 | // // 数组的定义形式 7 | // int []arrayInt; 8 | // // 数组的创建 9 | // arrayInt = new int[10]; 10 | 11 | // // // 可以在声明数组时直接初始化 12 | // int []arrayInt = {1,2,3,4,5}; 13 | 14 | // // System.out.println(arrayInt[0]); 15 | // // System.out.println(arrayInt[1]); 16 | // // System.out.println(arrayInt[2]); 17 | // // System.out.println(arrayInt[3]); 18 | // // System.out.println(arrayInt[4]); 19 | // // // error 20 | // // System.out.println(arrayInt[5]); 21 | // // System.out.println(arrayInt.length); 22 | 23 | // // 其他类型的数组 24 | // // double []arrayDouble; 25 | // // String []arrayString; 26 | 27 | // // 二维数组的定义形式: 28 | // int[][] array; 29 | // int array2[][]; 30 | 31 | // // 方法一:直接为每一维分配空间 32 | // int a[][] = new int[2][3]; 33 | // System.out.println(a.length); 34 | // System.out.println(a[0].length); 35 | 36 | // 方法二:从最高维开始,分别为每一维分配空间 37 | // int a[][] = new int[2][]; 38 | 39 | // System.out.println(a.length); 40 | 41 | // a[0] = new int[3]; 42 | // a[1] = new int[2]; 43 | // System.out.println(a[0].length); 44 | // System.out.println(a[1].length); 45 | 46 | // 多维数组初始化方法 47 | // (1)直接对每个元素进行赋值 48 | // (2)在定义数组的同时进行初始化 49 | // int a[][] = {{1,2},{3,4},{5,6}}; 50 | // for(int i = 0; i < a.length; i++) { 51 | // for(int j = 0 ; j < a[0].length; j++) { 52 | // System.out.print(a[i][j] + "\t"); 53 | // } 54 | // System.out.println(); 55 | // } 56 | 57 | 58 | 59 | 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/FileIO.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.ArrayList; 3 | 4 | /** 5 | * 这个类用于文件读写,便于同学后续写作业的时候使用 6 | * @author 王海洲,吴苑斌 7 | * @version 1.0 8 | */ 9 | 10 | public class FileIO{ 11 | public static void main(String args[]) {} 12 | 13 | /** 14 | * 这个方法将一个字符串追加地写入指定文件(支持中文)。 15 | * @param str String类型,要写入的字符。 16 | * @param fileName String类型, 文件名。 17 | * @exception Exception 输出流产生错误时。 18 | * @return void,无返回值。 19 | */ 20 | public static void writeStringToFile(String str, String fileName) { 21 | BufferedWriter bw = null; 22 | try { 23 | bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "UTF-8")); 24 | bw.write(str); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } finally { 28 | try { 29 | bw.close(); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | 37 | /** 38 | * 这个方法将读取指定文件的第pos个字符(支持中文)。 39 | * @param pos int类型, 要获取的字符的位置。 40 | * @param fileName String类型, 文件名。 41 | * @exception Exception 输入流产生错误或者文件不存在时。 42 | * @return char, 如果pos的值为负数或者大于文件内容长度,那么将返回0。如果正常,将返回该文件指定位置的字符。 43 | */ 44 | public static char getCharFromFile(int pos, String fileName) { 45 | char res = 0; 46 | BufferedReader br = null; 47 | int len = 0; 48 | try { 49 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 50 | String temp = null; 51 | while((temp = br.readLine()) != null && pos >= 0) { 52 | if(pos < temp.length()) { 53 | res = temp.charAt(pos); 54 | } 55 | pos -= temp.length(); 56 | } 57 | } catch (Exception e) { 58 | e.printStackTrace(); 59 | } finally { 60 | try { 61 | br.close(); 62 | } catch (Exception e) { 63 | e.printStackTrace(); 64 | } 65 | } 66 | return res; 67 | } 68 | 69 | 70 | /** 71 | * 这个方法将读取指定文件的第pos行的字符串(支持中文)。 72 | * @param pos int类型, 要获取的字符串的行的位置(从0开始)。 73 | * @param fileName String类型, 文件名。 74 | * @exception Exception 输入流产生错误或者文件不存在时。 75 | * @return String, 如果行下标pos为负数或者大于等于行数,那么将返回null。如果正常,将返回该文件指定行的字符串。 76 | */ 77 | public static String getLineFromFile(int pos, String fileName) { 78 | BufferedReader br = null; 79 | String res = null; 80 | try { 81 | 82 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 83 | String temp = null; 84 | int cnt = 0; 85 | while((temp = br.readLine()) != null && pos >= 0) { 86 | if(pos == 0) { 87 | res = temp; 88 | } 89 | pos--; 90 | } 91 | } catch (Exception e) { 92 | e.printStackTrace(); 93 | } finally { 94 | try { 95 | br.close(); 96 | } catch (Exception e) { 97 | e.printStackTrace(); 98 | } 99 | } 100 | return res; 101 | } 102 | 103 | 104 | /** 105 | * 这个方法将读取指定文件的所有行的字符串,并按照顺序储存在String类型数组中返回(支持中文)。 106 | * @param fileName String类型, 文件名。 107 | * @exception Exception 输入流产生错误或者文件不存在时。 108 | * @return String[], 文件中的内容将按照行号依次储存在String数组中。 109 | */ 110 | public static String[] getAllLinesFromFile(String fileName) { 111 | int numLines = 0; 112 | BufferedReader br = null; 113 | ArrayList strList = new ArrayList(); 114 | try { 115 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 116 | 117 | String temp = null; 118 | while((temp = br.readLine()) != null){ 119 | strList.add(temp); 120 | } 121 | numLines = strList.size(); 122 | } catch (Exception e) { 123 | e.printStackTrace(); 124 | } finally { 125 | try { 126 | br.close(); 127 | } catch (Exception e) { 128 | e.printStackTrace(); 129 | } 130 | } 131 | String[] res = new String[numLines]; 132 | for(int i = 0; i < numLines; i++) { 133 | res[i] = strList.get(i); 134 | } 135 | return res; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/allclasses-noframe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/constant-values.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 常量字段值 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

常量字段值

72 |

目录

73 |
74 | 75 |
76 | 77 | 78 | 79 | 80 | 81 | 82 | 90 |
91 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/deprecated-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 已过时的列表 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

已过时的 API

72 |

目录

73 |
74 | 75 |
76 | 77 | 78 | 79 | 80 | 81 | 82 | 90 |
91 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/index-all.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
F G M W  71 | 72 | 73 |

F

74 |
75 |
FileIO - <Unnamed>中的类
76 |
77 |
这个类用于文件读写,便于同学后续写作业的时候使用
78 |
79 |
FileIO() - 类 的构造器FileIO
80 |
 
81 |
82 | 83 | 84 | 85 |

G

86 |
87 |
getAllLinesFromFile(String) - 类 中的静态方法FileIO
88 |
89 |
这个方法将读取指定文件的所有行的字符串,并按照顺序储存在String类型数组中返回(支持中文)。
90 |
91 |
getCharFromFile(int, String) - 类 中的静态方法FileIO
92 |
93 |
这个方法将读取指定文件的第pos个字符(支持中文)。
94 |
95 |
getLineFromFile(int, String) - 类 中的静态方法FileIO
96 |
97 |
这个方法将读取指定文件的第pos行的字符串(支持中文)。
98 |
99 |
100 | 101 | 102 | 103 |

M

104 |
105 |
main(String[]) - 类 中的静态方法FileIO
106 |
 
107 |
108 | 109 | 110 | 111 |

W

112 |
113 |
writeStringToFile(String, String) - 类 中的静态方法FileIO
114 |
115 |
这个方法将一个字符串追加地写入指定文件(支持中文)。
116 |
117 |
118 | F G M W 
119 | 120 |
121 | 122 | 123 | 124 | 125 | 126 | 127 | 135 |
136 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 生成的文档 (无标题) 7 | 60 | 61 | 62 | 63 | 64 | 65 | <noscript> 66 | <div>您的浏览器已禁用 JavaScript。</div> 67 | </noscript> 68 | <h2>框架预警</h2> 69 | <p>请使用框架功能查看此文档。如果看到此消息, 则表明您使用的是不支持框架的 Web 客户机。链接到<a href="FileIO.html">非框架版本</a>。</p> 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/overview-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

所有程序包的分层结构

72 |
73 |
74 |

类分层结构

75 |
    76 |
  • java.lang.Object 77 | 80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <Unnamed> 7 | 8 | 9 | 10 | 11 | 12 |

<Unnamed>

13 |
14 |

15 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/package-list: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/package-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | 30 |
31 | 58 | 59 |
60 |

程序包 <Unnamed>

61 |
62 |
63 |
    64 |
  • 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 77 | 78 | 79 |
    类概要 
    说明
    FileIO 75 |
    这个类用于文件读写,便于同学后续写作业的时候使用
    76 |
    80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/package-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

程序包<Unnamed>的分层结构

72 |
73 |
74 |

类分层结构

75 |
    76 |
  • java.lang.Object 77 | 80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /24-Spring/ref_source/FileIO/doc/script.js: -------------------------------------------------------------------------------- 1 | function show(type) 2 | { 3 | count = 0; 4 | for (var key in methods) { 5 | var row = document.getElementById(key); 6 | if ((methods[key] & type) != 0) { 7 | row.style.display = ''; 8 | row.className = (count++ % 2) ? rowColor : altColor; 9 | } 10 | else 11 | row.style.display = 'none'; 12 | } 13 | updateTabs(type); 14 | } 15 | 16 | function updateTabs(type) 17 | { 18 | for (var value in tabs) { 19 | var sNode = document.getElementById(tabs[value][0]); 20 | var spanNode = sNode.firstChild; 21 | if (value == type) { 22 | sNode.className = activeTableTab; 23 | spanNode.innerHTML = tabs[value][1]; 24 | } 25 | else { 26 | sNode.className = tableTab; 27 | spanNode.innerHTML = "" + tabs[value][1] + ""; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /24-Spring/ref_source/MyList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class StudentRecord{ 4 | public int studentID; 5 | public String name; 6 | public double gpa; 7 | public StudentRecord(int studentID, String name, double gpa) { 8 | this.studentID = studentID; 9 | this.name = name; 10 | this.gpa = gpa; 11 | } 12 | public void printData() { 13 | System.out.println("student ID:" + studentID); 14 | System.out.println("name:" + name); 15 | System.out.println("gpa:" + gpa); 16 | } 17 | } 18 | 19 | class Node{ 20 | StudentRecord data; 21 | Node next; 22 | public Node(StudentRecord data) { 23 | setData(data); 24 | setNext(null); 25 | } 26 | public void setData(StudentRecord data) { 27 | this.data = data; 28 | } 29 | public void setNext(Node next){ 30 | this.next = next; 31 | } 32 | 33 | public void printNode() { 34 | data.printData(); 35 | } 36 | } 37 | 38 | public class MyList{ 39 | private Node head; 40 | 41 | public MyList() { 42 | setHead(new Node(null)); 43 | } 44 | 45 | private void setHead(Node head) { 46 | this.head = head; 47 | } 48 | 49 | private Node getHead() { 50 | return head; 51 | } 52 | 53 | public void insert(Node node) { 54 | Node moveNode = head; 55 | while(moveNode.next != null) { 56 | moveNode = moveNode.next; 57 | } 58 | moveNode.next = node; 59 | node.next = null; 60 | } 61 | 62 | public void insert(Node node, int pos) { 63 | Node moveNode = head; 64 | for(int i = 0; i < pos; i++) { 65 | moveNode = moveNode.next; 66 | } 67 | node.next = moveNode.next; 68 | moveNode.next = node; 69 | } 70 | 71 | // write it yourself, pos 0 means delete the first node of list. 72 | public void delete(int pos) { 73 | // 74 | 75 | } 76 | 77 | public void printList() { 78 | Node moveNode = head; 79 | 80 | while(moveNode.next != null) { 81 | moveNode = moveNode.next; 82 | moveNode.printNode(); 83 | } 84 | 85 | } 86 | 87 | 88 | public static void main(String args[]) { 89 | MyList myList = new MyList(); 90 | 91 | 92 | 93 | 94 | Node n1 = new Node(new StudentRecord(101221302, "Bob", 3.5)); 95 | myList.insert(n1); 96 | 97 | Node n2 = new Node(new StudentRecord(101221303, "Mary", 3.7)); 98 | myList.insert(n2); 99 | 100 | Node n3 = new Node(new StudentRecord(777, "Jack", 1.0)); 101 | myList.insert(n3, 0); 102 | 103 | Node n4 = new Node(new StudentRecord(1788, "Tom", 10.0)); 104 | myList.insert(n4, 2); 105 | 106 | myList.printList(); 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /24-Spring/ref_source/StringDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.* ; 2 | 3 | public class StringDemo{ 4 | public static void main(String[] args) { 5 | // example 1 6 | // String str; 7 | // str = new String("Hello World!"); 8 | // System.out.println(str); 9 | // str = "Hello World!"; 10 | // System.out.println(str); 11 | 12 | // example 2 13 | 14 | // String s1 = "abc"; 15 | // String s2 = "abc"; 16 | // String s3 = s1; 17 | // System.out.println(s1 == s2); 18 | // System.out.println(s3 == s2); 19 | // System.out.println(s1.equals(s2)); 20 | // System.out.println(s2.equals(s3)); 21 | 22 | // String s4 = new String("abc"); 23 | // String s5 = new String("abc"); 24 | // System.out.println(s4 == s5); 25 | // System.out.println(s4.equals(s5)); 26 | 27 | // ==============10 basic methods==================== 28 | String str1 = " I love Java! "; 29 | String str2 = " Java DON'T LOVE ME...."; 30 | System.out.println("==== str1 and str2 ===="); 31 | System.out.println(str1); 32 | System.out.println(str2); 33 | 34 | // 返回字符串的长度 35 | System.out.println("==== int length() ===="); 36 | System.out.println(str1.length()); 37 | System.out.println(str2.length()); 38 | 39 | //将串中字符变成大写 40 | System.out.println("==== String toUpperCase() ===="); 41 | System.out.println(str1.toUpperCase()); 42 | System.out.println(str2.toUpperCase()); 43 | 44 | //将串中字符变成小写 45 | System.out.println("==== String toLowerCase() ===="); 46 | System.out.println(str1.toLowerCase()); 47 | System.out.println(str2.toLowerCase()); 48 | 49 | //返回位置i处的字符 50 | System.out.println("==== char charAt(int i) ===="); 51 | System.out.println(str1.charAt(3)); 52 | System.out.println(str1.charAt(10)); 53 | 54 | // 返回从位置s到e的字符子串[s,e) 55 | System.out.println("==== String substring(int s,int e) ===="); 56 | System.out.println(str1.substring(3,7)); 57 | System.out.println(str2.substring(6, 12)); 58 | 59 | // 返回从位置s到末尾的字符子串 60 | System.out.println("==== String substring(int s) ===="); 61 | System.out.println(str1.substring(3)); 62 | System.out.println(str2.substring(6)); 63 | 64 | // 返回首次出现字符串s的位置 65 | System.out.println("==== int indexOf(String s) ===="); 66 | System.out.println(str1.indexOf("a")); 67 | System.out.println(str1.indexOf("kkk")); 68 | 69 | // 返回在位置i之后首次出现s的位置 70 | System.out.println("==== int indexOf(String s,int i) ===="); 71 | System.out.println(str1.indexOf("a", 11)); 72 | 73 | // 返回一个新串,去除前后空白字符 74 | System.out.println("==== String trim() ===="); 75 | System.out.println(str1.trim()); 76 | System.out.println(str2.trim()); 77 | 78 | // 返回一个新串,将a替换为b 79 | System.out.println("==== String replace(String a,String b) ===="); 80 | System.out.println(str1.replace("Java", "Python")); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /24-Spring/slides/Lab1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/Lab1.pdf -------------------------------------------------------------------------------- /24-Spring/slides/Lab2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/Lab2.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture1.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture10.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture11.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture12.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture13.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture2.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture3.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture4.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture5.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture6.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture7.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture8.pdf -------------------------------------------------------------------------------- /24-Spring/slides/lecture9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/24-Spring/slides/lecture9.pdf -------------------------------------------------------------------------------- /25-Spring/README.md: -------------------------------------------------------------------------------- 1 | # 面向对象程序设计 Java 2 | 3 | Dep. of Computer Science, ECNU, Spring 2025 4 | 5 | ## 时间地点 6 | 7 | * 理论课: 周四 8:00 - 9:35 @ 文史楼101 8 | * 实验课: 周四 13:00 - 14:35 @ 理科楼B116 9 | 10 | ## 联系信息 11 | 12 | * 教师: 吴苑斌 13 | * Email: ybwu\[at\]cs.ecnu.edu.cn 14 | * 办公室: 理科楼B713 15 | 16 | * 助教1: 郭彬 17 | * Email: binguo\[at\]stu.ecnu.edu.cn 18 | 19 | * 助教2: 俞心如 20 | * Email: xryu\[at\]stu.ecnu.edu.cn 21 | 22 | * 助教3: 刘诺纬 23 | * Email: nwliu\[at\]stu.ecnu.edu.cn 24 | 25 | 26 | ## 答疑 27 | 28 | * 周三 13:00 - 15:00 @ 理科楼B713 29 | 30 | ## 参考资料 31 | 32 | * Thinking in Java (4th Edition), Bruce Eckel 33 | * Java 编程编程思想 (第四版) 34 | 35 | ## 讲义 36 | 37 | * [Lecture 1](slides/lecture1.pdf) 38 | * [Lecture 2](slides/lecture2.pdf) 39 | * [Lecture 3](slides/lecture3.pdf) 40 | * [Lecture 4](slides/lecture4.pdf) 41 | * [Lecture 5](slides/lecture5.pdf) 42 | * [Lecture 6](slides/lecture6.pdf) 43 | * [Lecture 7](slides/lecture7.pdf) 44 | * [Lecture 8](slides/lecture8.pdf) 45 | * [Lecture 9](slides/lecture9.pdf) 46 | * [Lecture 10](slides/lecture10.pdf) 47 | * [Lecture 11](slides/lecture11.pdf) 48 | * [Lecture 12](slides/lecture12.pdf) 49 | * [Lecture 13](slides/lecture13.pdf) 50 | * [course review](slides/course_review.pdf) 51 | 52 | ## 实验 53 | 54 | ### 提交信息 55 | 56 | 提交地址: ecnu_java_2025\[at\]163.com 57 | 58 | 提交格式: 实验编号_学号_姓名.zip (例如: Lab1_10215102000_陈东.zip) 59 | 60 | ### 实验讲义及相关资源 61 | 62 | - [Lab 1 slides](slides/Lab1.pdf) 63 | - [Lab 2 slides](slides/Lab2.pdf), [ArrayDemo.java](ref_source/ArrayDemo.java), [StringDemo.java](ref_source/StringDemo.java) 64 | - [Lab 3 slides](slides/Lab3.pdf) 65 | - [Lab 4 slides](slides/Lab4.pdf)) 66 | - 简便的文件输入包 [FileIO.java](ref_source/FileIO/FileIO.java)及[文档](ref_source/FileIO/doc/index.html) 67 | 68 | 69 | ### 实验内容 70 | 71 | * Project 1: [第一个 Java 程序](../projects/first_java/README.md) 72 | * Project 2: [基本类型与控制流](../projects/primitive_ctlflow/README.md), [补充实验](../projects/familiar_with_toolkits/README.md) 73 | * Project 3: [使用 Java 包](../projects/use_data_types/README.md) 74 | * Project 4: [创建 Java 类](../projects/build_data_types/README.md) 75 | * Project 5: [继承与组合](../projects/inheritance/README.md) 76 | * Project 6: [接口](../projects/interface/README.md) 77 | * Project 7: [容器与I/O](../projects/container_io/README.md) 78 | 81 | -------------------------------------------------------------------------------- /25-Spring/ref_source/ArrayDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class ArrayDemo{ 4 | public static void main(String[] args) { 5 | 6 | // // 数组的定义形式 7 | // int []arrayInt; 8 | // // 数组的创建 9 | // arrayInt = new int[10]; 10 | 11 | // // // 可以在声明数组时直接初始化 12 | // int []arrayInt = {1,2,3,4,5}; 13 | 14 | // // System.out.println(arrayInt[0]); 15 | // // System.out.println(arrayInt[1]); 16 | // // System.out.println(arrayInt[2]); 17 | // // System.out.println(arrayInt[3]); 18 | // // System.out.println(arrayInt[4]); 19 | // // // error 20 | // // System.out.println(arrayInt[5]); 21 | // // System.out.println(arrayInt.length); 22 | 23 | // // 其他类型的数组 24 | // // double []arrayDouble; 25 | // // String []arrayString; 26 | 27 | // // 二维数组的定义形式: 28 | // int[][] array; 29 | // int array2[][]; 30 | 31 | // // 方法一:直接为每一维分配空间 32 | // int a[][] = new int[2][3]; 33 | // System.out.println(a.length); 34 | // System.out.println(a[0].length); 35 | 36 | // 方法二:从最高维开始,分别为每一维分配空间 37 | // int a[][] = new int[2][]; 38 | 39 | // System.out.println(a.length); 40 | 41 | // a[0] = new int[3]; 42 | // a[1] = new int[2]; 43 | // System.out.println(a[0].length); 44 | // System.out.println(a[1].length); 45 | 46 | // 多维数组初始化方法 47 | // (1)直接对每个元素进行赋值 48 | // (2)在定义数组的同时进行初始化 49 | // int a[][] = {{1,2},{3,4},{5,6}}; 50 | // for(int i = 0; i < a.length; i++) { 51 | // for(int j = 0 ; j < a[0].length; j++) { 52 | // System.out.print(a[i][j] + "\t"); 53 | // } 54 | // System.out.println(); 55 | // } 56 | 57 | 58 | 59 | 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/FileIO.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.ArrayList; 3 | 4 | /** 5 | * 这个类用于文件读写,便于同学后续写作业的时候使用 6 | * @author 王海洲,吴苑斌 7 | * @version 1.0 8 | */ 9 | 10 | public class FileIO{ 11 | public static void main(String args[]) {} 12 | 13 | /** 14 | * 这个方法将一个字符串追加地写入指定文件(支持中文)。 15 | * @param str String类型,要写入的字符。 16 | * @param fileName String类型, 文件名。 17 | * @exception Exception 输出流产生错误时。 18 | * @return void,无返回值。 19 | */ 20 | public static void writeStringToFile(String str, String fileName) { 21 | BufferedWriter bw = null; 22 | try { 23 | bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "UTF-8")); 24 | bw.write(str); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } finally { 28 | try { 29 | bw.close(); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | 37 | /** 38 | * 这个方法将读取指定文件的第pos个字符(支持中文)。 39 | * @param pos int类型, 要获取的字符的位置。 40 | * @param fileName String类型, 文件名。 41 | * @exception Exception 输入流产生错误或者文件不存在时。 42 | * @return char, 如果pos的值为负数或者大于文件内容长度,那么将返回0。如果正常,将返回该文件指定位置的字符。 43 | */ 44 | public static char getCharFromFile(int pos, String fileName) { 45 | char res = 0; 46 | BufferedReader br = null; 47 | int len = 0; 48 | try { 49 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 50 | String temp = null; 51 | while((temp = br.readLine()) != null && pos >= 0) { 52 | if(pos < temp.length()) { 53 | res = temp.charAt(pos); 54 | } 55 | pos -= temp.length(); 56 | } 57 | } catch (Exception e) { 58 | e.printStackTrace(); 59 | } finally { 60 | try { 61 | br.close(); 62 | } catch (Exception e) { 63 | e.printStackTrace(); 64 | } 65 | } 66 | return res; 67 | } 68 | 69 | 70 | /** 71 | * 这个方法将读取指定文件的第pos行的字符串(支持中文)。 72 | * @param pos int类型, 要获取的字符串的行的位置(从0开始)。 73 | * @param fileName String类型, 文件名。 74 | * @exception Exception 输入流产生错误或者文件不存在时。 75 | * @return String, 如果行下标pos为负数或者大于等于行数,那么将返回null。如果正常,将返回该文件指定行的字符串。 76 | */ 77 | public static String getLineFromFile(int pos, String fileName) { 78 | BufferedReader br = null; 79 | String res = null; 80 | try { 81 | 82 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 83 | String temp = null; 84 | int cnt = 0; 85 | while((temp = br.readLine()) != null && pos >= 0) { 86 | if(pos == 0) { 87 | res = temp; 88 | } 89 | pos--; 90 | } 91 | } catch (Exception e) { 92 | e.printStackTrace(); 93 | } finally { 94 | try { 95 | br.close(); 96 | } catch (Exception e) { 97 | e.printStackTrace(); 98 | } 99 | } 100 | return res; 101 | } 102 | 103 | 104 | /** 105 | * 这个方法将读取指定文件的所有行的字符串,并按照顺序储存在String类型数组中返回(支持中文)。 106 | * @param fileName String类型, 文件名。 107 | * @exception Exception 输入流产生错误或者文件不存在时。 108 | * @return String[], 文件中的内容将按照行号依次储存在String数组中。 109 | */ 110 | public static String[] getAllLinesFromFile(String fileName) { 111 | int numLines = 0; 112 | BufferedReader br = null; 113 | ArrayList strList = new ArrayList(); 114 | try { 115 | br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); 116 | 117 | String temp = null; 118 | while((temp = br.readLine()) != null){ 119 | strList.add(temp); 120 | } 121 | numLines = strList.size(); 122 | } catch (Exception e) { 123 | e.printStackTrace(); 124 | } finally { 125 | try { 126 | br.close(); 127 | } catch (Exception e) { 128 | e.printStackTrace(); 129 | } 130 | } 131 | String[] res = new String[numLines]; 132 | for(int i = 0; i < numLines; i++) { 133 | res[i] = strList.get(i); 134 | } 135 | return res; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/allclasses-noframe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/constant-values.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 常量字段值 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

常量字段值

72 |

目录

73 |
74 | 75 |
76 | 77 | 78 | 79 | 80 | 81 | 82 | 90 |
91 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/deprecated-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 已过时的列表 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

已过时的 API

72 |

目录

73 |
74 | 75 |
76 | 77 | 78 | 79 | 80 | 81 | 82 | 90 |
91 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 生成的文档 (无标题) 7 | 60 | 61 | 62 | 63 | 64 | 65 | <noscript> 66 | <div>您的浏览器已禁用 JavaScript。</div> 67 | </noscript> 68 | <h2>框架预警</h2> 69 | <p>请使用框架功能查看此文档。如果看到此消息, 则表明您使用的是不支持框架的 Web 客户机。链接到<a href="FileIO.html">非框架版本</a>。</p> 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/overview-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

所有程序包的分层结构

72 |
73 |
74 |

类分层结构

75 |
    76 |
  • java.lang.Object 77 | 80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <Unnamed> 7 | 8 | 9 | 10 | 11 | 12 |

<Unnamed>

13 |
14 |

15 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/package-list: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/package-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | 30 |
31 | 58 | 59 |
60 |

程序包 <Unnamed>

61 |
62 |
63 |
    64 |
  • 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 77 | 78 | 79 |
    类概要 
    说明
    FileIO 75 |
    这个类用于文件读写,便于同学后续写作业的时候使用
    76 |
    80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/package-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | 41 |
42 | 69 | 70 |
71 |

程序包<Unnamed>的分层结构

72 |
73 |
74 |

类分层结构

75 |
    76 |
  • java.lang.Object 77 | 80 |
  • 81 |
82 |
83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 99 |
100 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /25-Spring/ref_source/FileIO/doc/script.js: -------------------------------------------------------------------------------- 1 | function show(type) 2 | { 3 | count = 0; 4 | for (var key in methods) { 5 | var row = document.getElementById(key); 6 | if ((methods[key] & type) != 0) { 7 | row.style.display = ''; 8 | row.className = (count++ % 2) ? rowColor : altColor; 9 | } 10 | else 11 | row.style.display = 'none'; 12 | } 13 | updateTabs(type); 14 | } 15 | 16 | function updateTabs(type) 17 | { 18 | for (var value in tabs) { 19 | var sNode = document.getElementById(tabs[value][0]); 20 | var spanNode = sNode.firstChild; 21 | if (value == type) { 22 | sNode.className = activeTableTab; 23 | spanNode.innerHTML = tabs[value][1]; 24 | } 25 | else { 26 | sNode.className = tableTab; 27 | spanNode.innerHTML = "" + tabs[value][1] + ""; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /25-Spring/ref_source/MyList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class StudentRecord{ 4 | public int studentID; 5 | public String name; 6 | public double gpa; 7 | public StudentRecord(int studentID, String name, double gpa) { 8 | this.studentID = studentID; 9 | this.name = name; 10 | this.gpa = gpa; 11 | } 12 | public void printData() { 13 | System.out.println("student ID:" + studentID); 14 | System.out.println("name:" + name); 15 | System.out.println("gpa:" + gpa); 16 | } 17 | } 18 | 19 | class Node{ 20 | StudentRecord data; 21 | Node next; 22 | public Node(StudentRecord data) { 23 | setData(data); 24 | setNext(null); 25 | } 26 | public void setData(StudentRecord data) { 27 | this.data = data; 28 | } 29 | public void setNext(Node next){ 30 | this.next = next; 31 | } 32 | 33 | public void printNode() { 34 | data.printData(); 35 | } 36 | } 37 | 38 | public class MyList{ 39 | private Node head; 40 | 41 | public MyList() { 42 | setHead(new Node(null)); 43 | } 44 | 45 | private void setHead(Node head) { 46 | this.head = head; 47 | } 48 | 49 | private Node getHead() { 50 | return head; 51 | } 52 | 53 | public void insert(Node node) { 54 | Node moveNode = head; 55 | while(moveNode.next != null) { 56 | moveNode = moveNode.next; 57 | } 58 | moveNode.next = node; 59 | node.next = null; 60 | } 61 | 62 | public void insert(Node node, int pos) { 63 | Node moveNode = head; 64 | for(int i = 0; i < pos; i++) { 65 | moveNode = moveNode.next; 66 | } 67 | node.next = moveNode.next; 68 | moveNode.next = node; 69 | } 70 | 71 | // write it yourself, pos 0 means delete the first node of list. 72 | public void delete(int pos) { 73 | // 74 | 75 | } 76 | 77 | public void printList() { 78 | Node moveNode = head; 79 | 80 | while(moveNode.next != null) { 81 | moveNode = moveNode.next; 82 | moveNode.printNode(); 83 | } 84 | 85 | } 86 | 87 | 88 | public static void main(String args[]) { 89 | MyList myList = new MyList(); 90 | 91 | 92 | 93 | 94 | Node n1 = new Node(new StudentRecord(101221302, "Bob", 3.5)); 95 | myList.insert(n1); 96 | 97 | Node n2 = new Node(new StudentRecord(101221303, "Mary", 3.7)); 98 | myList.insert(n2); 99 | 100 | Node n3 = new Node(new StudentRecord(777, "Jack", 1.0)); 101 | myList.insert(n3, 0); 102 | 103 | Node n4 = new Node(new StudentRecord(1788, "Tom", 10.0)); 104 | myList.insert(n4, 2); 105 | 106 | myList.printList(); 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /25-Spring/ref_source/StringDemo.java: -------------------------------------------------------------------------------- 1 | import java.io.* ; 2 | 3 | public class StringDemo{ 4 | public static void main(String[] args) { 5 | // example 1 6 | // String str; 7 | // str = new String("Hello World!"); 8 | // System.out.println(str); 9 | // str = "Hello World!"; 10 | // System.out.println(str); 11 | 12 | // example 2 13 | 14 | // String s1 = "abc"; 15 | // String s2 = "abc"; 16 | // String s3 = s1; 17 | // System.out.println(s1 == s2); 18 | // System.out.println(s3 == s2); 19 | // System.out.println(s1.equals(s2)); 20 | // System.out.println(s2.equals(s3)); 21 | 22 | // String s4 = new String("abc"); 23 | // String s5 = new String("abc"); 24 | // System.out.println(s4 == s5); 25 | // System.out.println(s4.equals(s5)); 26 | 27 | // ==============10 basic methods==================== 28 | String str1 = " I love Java! "; 29 | String str2 = " Java DON'T LOVE ME...."; 30 | System.out.println("==== str1 and str2 ===="); 31 | System.out.println(str1); 32 | System.out.println(str2); 33 | 34 | // 返回字符串的长度 35 | System.out.println("==== int length() ===="); 36 | System.out.println(str1.length()); 37 | System.out.println(str2.length()); 38 | 39 | //将串中字符变成大写 40 | System.out.println("==== String toUpperCase() ===="); 41 | System.out.println(str1.toUpperCase()); 42 | System.out.println(str2.toUpperCase()); 43 | 44 | //将串中字符变成小写 45 | System.out.println("==== String toLowerCase() ===="); 46 | System.out.println(str1.toLowerCase()); 47 | System.out.println(str2.toLowerCase()); 48 | 49 | //返回位置i处的字符 50 | System.out.println("==== char charAt(int i) ===="); 51 | System.out.println(str1.charAt(3)); 52 | System.out.println(str1.charAt(10)); 53 | 54 | // 返回从位置s到e的字符子串[s,e) 55 | System.out.println("==== String substring(int s,int e) ===="); 56 | System.out.println(str1.substring(3,7)); 57 | System.out.println(str2.substring(6, 12)); 58 | 59 | // 返回从位置s到末尾的字符子串 60 | System.out.println("==== String substring(int s) ===="); 61 | System.out.println(str1.substring(3)); 62 | System.out.println(str2.substring(6)); 63 | 64 | // 返回首次出现字符串s的位置 65 | System.out.println("==== int indexOf(String s) ===="); 66 | System.out.println(str1.indexOf("a")); 67 | System.out.println(str1.indexOf("kkk")); 68 | 69 | // 返回在位置i之后首次出现s的位置 70 | System.out.println("==== int indexOf(String s,int i) ===="); 71 | System.out.println(str1.indexOf("a", 11)); 72 | 73 | // 返回一个新串,去除前后空白字符 74 | System.out.println("==== String trim() ===="); 75 | System.out.println(str1.trim()); 76 | System.out.println(str2.trim()); 77 | 78 | // 返回一个新串,将a替换为b 79 | System.out.println("==== String replace(String a,String b) ===="); 80 | System.out.println(str1.replace("Java", "Python")); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /25-Spring/slides/Lab1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/Lab1.pdf -------------------------------------------------------------------------------- /25-Spring/slides/Lab2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/Lab2.pdf -------------------------------------------------------------------------------- /25-Spring/slides/Lab3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/Lab3.pdf -------------------------------------------------------------------------------- /25-Spring/slides/Lab4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/Lab4.pdf -------------------------------------------------------------------------------- /25-Spring/slides/course_review.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/course_review.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture1.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture10.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture11.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture12.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture13.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture2.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture3.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture4.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture5.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture6.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture7.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture8.pdf -------------------------------------------------------------------------------- /25-Spring/slides/lecture9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/25-Spring/slides/lecture9.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to the ecnu Java course 2 | 3 | ## :snake: [Spring 2025](25-Spring/README.md) 4 | ## :dragon: [Spring 2024](24-Spring/README.md) 5 | ## :rabbit: [Spring 2023](23-Spring/README.md) 6 | -------------------------------------------------------------------------------- /projects/build_data_types/README.md: -------------------------------------------------------------------------------- 1 | # 创建 Java 类 2 | 3 | ## 概述 4 | 5 | 类是 Java 以及其他面向对象程序设计的核心. 6 | 作为对实际问题的抽象, 我们可以从两个角度来观察一个类: 7 | 从类的内部来看, 类需要提供简洁而易用的接口 (API), 8 | 同时需要对内部细节做合适的屏蔽. 9 | 从类的外部来看, 不同类之间关系同样应简洁清晰: 10 | 类A的实现应仅仅依赖类B的接口, 而不依赖于类B内部被隐藏的细节. 11 | 所谓"高内聚, 低耦合"是创建类的一条设计的原则. 12 | 我们会使用的另外一条原则是: **完成任务的情况下设计应尽量简单** 13 | ([奥卡姆剃刀原则 (Occam's Razor)](https://en.wikipedia.org/wiki/Occam%27s_razor)). 14 | 简单的设计更容易测试, 维护和扩展. 15 | 16 | 在本次作业中, 我们主要关注对单个类的设计. 你将练习定义一些简单的类. 17 | 有些类的接口相对较为明确 (Project a, b), 另外的一些类需要从问题中归纳出 (Project c). 18 | 我们希望能够帮助你熟悉创建类的过程. 19 | 同时, 单个类的创建是解决复杂问题(需要多个类相互协作)的第一步. 20 | 21 | ## 任务 22 | 23 | [a 长整数](big_integer.md) 24 | 25 | [b 复数](complex.md) 26 | 27 | [c 分形](fractal.md) 28 | -------------------------------------------------------------------------------- /projects/build_data_types/big_integer.md: -------------------------------------------------------------------------------- 1 | # 长整数 2 | 3 | ## 简介 4 | Java 内置的整数类型 `int` 为32 bit. 5 | 它可以表示的整数范围为 $-2^{31}$ 到 $2^{31}-1$ (约40亿个数). 6 | 这些整数足够应对普通的任务. 但一些特殊任务, 可能需要能够支持更大范围的整数. 7 | 例如在加密算法(如 [RSA](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) )中, 往往需要用到很大的素数. 素数越大, 加密算法越安全. 8 | 又如, 在一些模拟程序中, 为了达到足够的精度, 需要能够支持更大的整数. 9 | 长整数以对它们的操作是 10 | [高精度计算](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) 11 | 的一个基本组成部分. 12 | 13 | Java 已经包含一个长整数类 14 | [BigInteger](http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html). 15 | 本次Project需要你自己动手实现一个类似的类. 16 | 17 | 18 | ## 实验内容 19 | 20 | 1. 实现长整数类BigInt, 支持任意精度的整数及其运算. 包含以下 API : 21 | 22 | | 方法 | 说明| 23 | |--- | ---| 24 | |BigInt() | 默认构造函数, 初始化为0 | 25 | |BigInt(String s) | 使用字符串构造长整数 | 26 | |BigInt add(BigInt x) | 返回与 x 的和 | 27 | |BigInt subtract(BigInt x) | 返回与 x 的差 | 28 | |BigInt multiply(BigInt x) | 返回与 x 的积 | 29 | |BigInt divide(BigInt x) | 返回除以 x 后的商 | 30 | |BigInt mod(BigInt x) | 返回除以 x 后的余数 | 31 | |int compare(BigInt x) | 返回1 如果大于 x, -1如果小于 x, 0如果相等 | 32 | |boolean equals(BigInt x) |是否等于x | 33 | |String toString() |返回字符串表示| 34 | 35 | 2. 使用 BigInt 计算阶乘函数 $n!$ 及指数函数 $2^n$. 36 | 37 | 3. 比较 BigInt 与 int 的性能差异 (例如做多次运算时间上的区别). 并尝试分析原因. 38 | 39 | 4. 比较 BigInt 与 Java 自中 BigInteger 的性能差异, 并尝试分析原因. 40 | 41 | 3. 除了已经使用的方法外, 你能否想到其他方法来实现 BigInt ? 42 | 43 | -------------------------------------------------------------------------------- /projects/build_data_types/complex.md: -------------------------------------------------------------------------------- 1 | # 复数 2 | 3 | ## 简介 4 | 5 | [复数 (complex number)](https://en.wikipedia.org/wiki/Complex_number) 6 | 定义为 $z = x + yi$, 其中 $i^2 = -1$ 称为虚数单位, 7 | $x, y\in \mathbf{R}$ 为实数, 分别称为 $z$ 的实部 (real part) 和虚部 (imaginary part). 8 | 复数的全体记为 $\mathbf{C} = \\{x + yi|x, y \in R\\}$. 9 | 显然实数是一类特殊的复数 $\mathbf{R} = \\{z\in \mathbf{C}|y=0\\}$. 10 | 11 | ### 复数的运算 12 | 13 | 考虑两个复数 $z_1 = x_1 + y_1 i$ 和 $z_2 = x_2 + y_2 i$ . 14 | $z_1=z_2$ 当且仅当 $x_1 = x_2, y_1 = y_2$. 15 | 复数的加法和乘法运算规则如下 16 | 17 | * 加法 (addition): 18 | 19 | $$\begin{eqnarray*} 20 | z_1 + z_2 = (x_1+x_2) + (y_1+y_2)i 21 | \end{eqnarray*}$$ 22 | 23 | * 乘法 (multiplication): 24 | 25 | $$\begin{eqnarray*} 26 | z_1z_2 = (x_1x_2-y_1y_2) + (x_1y_2 + x_2y_1)i 27 | \end{eqnarray*}$$ 28 | 29 | 通过加法和乘法, 可以得到减法: 30 | 31 | * 减法 (subtraction): 32 | 33 | $$\begin{eqnarray*} 34 | z_1 - z_2 = z_1 + (-1) z_2 = (x_1-x_2) + (y_1-y_2)i 35 | \end{eqnarray*}$$ 36 | 37 | 为了定义复数的除法, 首先定义复数 $z$ 的模长和共轭. 38 | 39 | * 模长 (magnitude): 40 | 41 | $$\begin{eqnarray*} 42 | |z| = \sqrt{x^2 + y^2} 43 | \end{eqnarray*}$$ 44 | 45 | * 共轭 (conjungate): 46 | 47 | $$\begin{eqnarray*} 48 | \bar{z} = x - yi 49 | \end{eqnarray*}$$ 50 | 51 | 由共轭的定义可以得到 $z\bar{z} = |z|^2$, 由此可以定义 $z$ 的倒数: 52 | 53 | * 倒数 (reciprocal): 54 | 55 | $$\begin{eqnarray*} 56 | \frac{1}{z} = \frac{\bar{z}}{|z|^2} 57 | \end{eqnarray*}$$ 58 | 59 | 由此可以定义复数的除法: 60 | 61 | * 除法 (division): 62 | 63 | $$\begin{eqnarray*} 64 | \frac{z_1}{z_2} = z_1 \frac{1}{z_2} = z_1 \frac{\bar{z_2}}{|z_2|^2} 65 | =\frac{(x_1x_2 + y_1y_2) - (x_1y_2 - x_2y_1)i}{x_2^2 + y_2^2} 66 | \end{eqnarray*}$$ 67 | 68 | 69 | 70 | ### 极坐标形式 (Polar Form) 71 | 72 | 根据定义, 复数 $z$ 可以看成复平面上的一个点, 并使用极坐标表示: 73 | 74 | $$\begin{eqnarray*} 75 | z = r(\cos\varphi + i\sin\varphi), \ \ \text{ 其中 } \ r \in [0, \infty), \varphi \in (-\pi, \pi] 76 | \end{eqnarray*}$$ 77 | 78 | ![](images/complex_polar.png) 79 | 80 | 由欧拉公式 $e^{i\varphi} = \cos\varphi + i\sin\varphi$, 可得 81 | 82 | $$\begin{eqnarray*} 83 | z=re^{i\varphi}. 84 | \end{eqnarray*}$$ 85 | 86 | 在极坐标下复数的加法和减法可以看成复平面上向量的加法和减法, 87 | 乘法和除法也有清晰的几何意义. 88 | 89 | $$\begin{eqnarray*} 90 | z_1z_2 & = & r_1r_2e^{i(\varphi_1 + \varphi_2)} = r_1r_2(\cos(\varphi_1+\varphi_2) + i\sin(\varphi_1+\varphi_2)) \\ 91 | \frac{z_1}{z_2} & = &\frac{r_1}{r_2}e^{i(\varphi_1 - \varphi_2)} = \frac{r_1}{r_2}(\cos(\varphi_1 - \varphi_2) + i \sin(\varphi_1 - \varphi_2)) 92 | \end{eqnarray*}$$ 93 | 94 | 在极坐标下可以方便的定义复数的指数函数, 对数函数和幂函数: 95 | 96 | $$\begin{eqnarray*} 97 | e^{z} &=& e^{r(\cos\varphi + i\sin\varphi)} = e^{r\cos\varphi}(\cos(r\sin\varphi) + i\sin(r\sin\varphi)) \\ 98 | \ln z &=& \ln(re^{i\varphi}) = \\{\ln r + (\varphi + 2k\pi)i | k为整数\\} \\ 99 | z^a &=& e^{\ln z^a} = e^{a\ln z} = \\{e^{a (\ln r +(\varphi + 2k\pi)i)}| k为整数\\} 100 | \end{eqnarray*}$$ 101 | 102 | ## 实验内容 103 | 104 | 1. 定义 ComplexCart 类, 它使用笛卡尔坐标系来代表复数, 包含以下 API 且为不可变的 (immutable). 105 | 106 | |方法 | 说明 | 107 | |--- | --- | 108 | |ComplexCart(double real, double imag) |构造函数| 109 | |ComplexCart add(ComplexCart x) |返回与x的和| 110 | |ComplexCart subtract(ComplexCart x) |返回与x的差| 111 | |ComplexCart multiply(ComplexCart x) | 返回与x的积| 112 | |ComplexCart divide(ComplexCart x) | 返回与x的商| 113 | |ComplexCart reciprocal() | 返回倒数| 114 | |ComplexCart conjugate() | 返回共轭| 115 | |double abs() | 返回模长| 116 | |double getRealPart() | 返回实部| 117 | |double getImaginaryPart() | 返回虚部| 118 | |boolean equals(ComplexCart x) | 是否等于x| 119 | |String toString() | 字符串表示| 120 | 121 | 122 | 2. 思考并回答以下问题: 123 | * 在 API 中加入方法 `ComplexCart add(ComplexCart x, ComplexCart y)` (返回三个复数的和)是否合适? 124 | * 在 API 中加入方法 `ComplexCart scale(double f)` (返回与一个实数的乘积) 是否合适? 125 | * 在 API 中加入方法 `boolean isReal()` (返回是否是实数) 是否合适? 126 | * 在 API 中加入方法 `void setRealPart(double a)` (将实部修改为a) 是否合适? 127 | * 在 API 中去掉方法 `double abs()` 是否合适? (由定义, 模长可以通过乘法计算得到) 128 | * 在 API 中去掉除法 `ComplexCart divide(ComplexCart x)` 是否合适? (由定义, 除法可以由倒数和乘法得到) 129 | * 在 API 中去掉方法 `double getRealPart()` 是否合适? (直接访问实部) 130 | * 以下两种使用方法哪一种更好? 为什么? 131 | 132 | ```java 133 | ComplexCard a = new ComplexCart(1, 2); 134 | ComplexCard b = new ComplexCart(3, 4); 135 | 136 | double real = a.getRealPart() + b.getRealPart(); 137 | double imag = a.getImaginaryPart() + b.getImaginaryPart(); 138 | ComplexCard c = new ComplexCart(real, imag); 139 | ``` 140 | 141 | ```java 142 | ComplexCard a = new ComplexCart(1, 2); 143 | ComplexCard b = new ComplexCart(3, 4); 144 | ComplexCard c = a.add(b); 145 | ``` 146 | 147 | 3. 使用极坐标实现复数类 ComplexPolar , 包含相同的 API . 148 | 149 | 150 | 4. 为ComplexCart类增加方法 `ComplexPolar toPolar()` 返回它的极坐标表示. 同样, 为 ComplexPolar 增加方法 `ComplexCart toCartesian()` 返回它的笛卡尔坐标表示. 151 | 152 | 5. 为 ComplexPolar 类增加下列 API. 153 | 154 | |方法 | 说明| 155 | | --- | --- | 156 | |ComplexPolar exp() |返回 $e^z$ | 157 | |ComplexPolar log() |返回 $\ln z$ (取k=0)| 158 | |ComplexPolar pow(double a) |返回 $z^a$ (取k=0)| 159 | 160 | 6. 为 ComplexCart 类增加以上 API. 161 | 162 | 7. 给定二元一次方程, $ax^2 + bx +c = 0$, 输出它的根. 163 | 164 | -------------------------------------------------------------------------------- /projects/build_data_types/fractal.md: -------------------------------------------------------------------------------- 1 | # 分形 2 | 3 | [分形 (Fractal) ](https://en.wikipedia.org/wiki/Fractal) 4 | 是一类几何形状. 它们的特点是在任意小的尺度上都有精细的结构. 5 | 分形通常可以由一些简单结构通过不断组合, 分裂形成, 即所谓的自相似性 6 | (self-similar): 任意的局部都有和整体相似的形状. 7 | 它们与传统的几何(点, 线, 多边形, 多面体等)有很大的不同, 8 | 有许多令人吃惊的性质. 例如可以通过分形构造出封闭的几何图形, 9 | 它们的面积是有限的, 但周长无限. 又如, 可以通过分形构造出连续但几乎处处不可微的曲线. 10 | 分形是自然界中常见的现象: 11 | [岛屿的海岸线](https://en.wikipedia.org/wiki/How_Long_Is_the_Coast_of_Britain%3F_Statistical_Self-Similarity_and_Fractional_Dimension), 12 | 破碎的玻璃边缘等都体现出分形结构. 13 | 同时, 分形在[艺术创作](http://fractalarts.com/ASF/Fractal_Art_Galleries.html), 14 | 工艺品设计, 建筑中也有用武之地. 15 | 16 | 在这个 Project 中我们首先以 [Koch 曲线](https://en.wikipedia.org/wiki/Koch_snowflake) 17 | 为例, 介绍分形的特征. 随后简单介绍一个画图包. 在实验内容中, 你需要设计并定义一个类. 18 | 通过这个类提供的接口, 用户可以完成一些分形的绘制. 通过这个实验, 19 | 我们希望帮助你体会使用面向对象方法解决实际问题的基本流程: 20 | 21 | * 分析所需要解决的问题 22 | * 问题是否相似? 如何划分子问题? 子问题之间的联系是什么? 23 | * 提出恰当的抽象 24 | * 不同的问题是否用到同样一组方法? 哪部分代码可以重用? 有哪些类? 哪些部分应该被隐藏, 哪些部分应该作为 API ? 25 | * 编码设计 26 | * 测试 27 | * 重复迭代以上步骤 28 | 29 | ## 简介 30 | 31 | ### Koch 曲线 (Koch curves) 32 | 33 | Koch 曲线是一种分形曲线. 以下分别是0阶, 1阶, 2阶及3阶 Koch 曲线. 34 | 35 | ![0阶](images/koch0.png) ![1阶](images/koch1.png) ![2阶](images/koch2.png) ![3阶](images/koch3.png) 36 | 37 | 38 | 绘制 n 阶 Koch 曲线可以通过以下算法实现: 39 | 40 | * 绘制 n-1 阶 Koch 曲线 41 | * 逆时针旋转 60 度 42 | * 绘制 n-1 阶 Koch 曲线 43 | * 顺时针旋转 120 度 44 | * 绘制 n-1 阶 Koch 曲线 45 | * 逆时针旋转 60 度 46 | * 绘制 n-1 阶 Koch 曲线 47 | 48 | 49 | ### Koch 雪花 (Koch snowflake) 50 | 51 | Koch 雪花是二维平面上由 Koch 曲线组合成的封闭图形. 52 | 以下分别是0阶, 1阶, 2阶及3阶 Koch 雪花. 53 | 54 | ![](images/koch_snow0.png) ![](images/koch_snow1.png) ![](images/koch_snow2.png) ![](images/koch_snow3.png) 55 | 56 | 绘制 n 阶 Koch 雪花可以通过以下算法实现: 57 | 58 | * 绘制 n 阶 Koch 曲线 59 | * 顺时针旋转 120 度 60 | * 绘制 n 阶 Koch 曲线 61 | * 顺时针旋转 120 度 62 | * 绘制 n 阶 Koch 曲线 63 | 64 | 问题: Koch 雪花的面积是有限的, 但周长是无限. 你能否写出周长和面积的表达式? 65 | 66 | ### StdDraw 画图包 67 | 68 | [StdDraw 包](./src/fractal/StdDraw.java)封装了一些基本的画图方法. 69 | 可以利用它来绘制简单的集合图形包括点, 线, 矩形, 圆等. 70 | 详细的说明可以参见它的[文档](./docs/StdDraw.html). 71 | 以下列出我们可能会用到的接口: 72 | 73 | |方法 | 说明| 74 | |--- | --- | 75 | |StdDraw.line(double x1, double y1, double x2, double y2) | 画一条从 (x1, y1) 到 (x2, y2) 的直线 . 76 | |StdDraw.setXscale(double minx, double maxx) | 画纸 X 轴最左边坐标为 minx, 最右边坐标为 maxx, 默认情况分别为0和1 | 77 | |StdDraw.setYscale(double miny, double maxy) | 画纸 Y 轴最下边坐标为 miny, 最上边坐标为 maxy, 默认情况分别为0和1 | 78 | 79 | 80 | ### Math 包 81 | 82 | 在绘制分形时, 往往需要计算长度和角度. 83 | 可以使用 [Java Math](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html) 包来完成. 84 | 我们可能会用到的函数包括: 85 | 86 | * 三角函数: Math.sin, Math.cos, Math.toRadians 87 | * 幂函数: Math.pow 88 | * 随机函数: Math.random 89 | 90 | 91 | ## 实验内容 92 | 93 | 1. 利用 StdDraw 设计一个类 Turtle, 并使用 Turtle 类画出以下三类图片 (提示: Turtle类包含数据成员(x, y)表示当前所在的坐标点, 数据成员angle表示当前画笔前进的角度, 方法turnLeft(delta)表示把当前画笔角度左转delta度, goForward(step)表示画笔从当前位置沿当前角度画一条长度为step的线). 94 | * 单位圆的内接正 N 边形 95 | 96 | ![](images/ngon3.png) ![](images/ngon4.png) ![](images/ngon5.png) ![](images/ngon6.png) 97 | 98 | * 螺旋线 (Spiral): 单位圆内接正 N 边形的边长随时间 t 衰减得到的曲线. 接收三个参数, N: 正 N 边形, T: 螺旋线盘旋的次数(即, 穿过第一象限的次数), D: 边长衰减的倍数. 下面分别为T=10, D=1.1, N为3, 4, 5, 6所得到的图形. 99 | 100 | ![](images/spiral3.png) ![](images/spiral4.png) ![](images/spiral5.png) ![](images/spiral6.png) 101 | 102 | * [布朗运动 (Brownian Motion)](https://en.wikipedia.org/wiki/Brownian_motion). 粒子在平面上随机选择方向移动. 接收两个参数, T: 移动次数, S: 每次移动的长度. 以下是T=1000, S=0.1时得到的四个图形. 103 | 104 | ![](images/brown1.png) ![](images/brown2.png) ![](images/brown3.png) ![](images/brown4.png) 105 | 106 | 107 | 2. 绘制 Koch 雪花. 接收命令行参数 N, 表示图形的阶数 (以下实验中, 参数均通过命令行给出. 提示:使用 Turtle 类). 108 | 109 | 3. 在绘制 Koch 曲线时, 将其中的顺时针替换成逆时针, 逆时针替换成顺时针. 用修改后的 Koch 曲线绘制 Koch 雪花. 110 | 111 | 4. 绘制以下分形(任选三个, 提示:使用 Turtle 类). 112 | 113 | * Minkowski sausage 114 | 115 | ![](images/sausage0.png) ![](images/sausage1.png) ![](images/sausage2.png) ![](images/sausage3.png) ![](images/sausage4.png) 116 | 117 | * Gosper island 118 | 119 | ![](images/gosper0.png) ![](images/gosper1.png) ![](images/gosper2.png) ![](images/gosper3.png) ![](images/gosper4.png) 120 | 121 | 122 | * Fudge flake 123 | 124 | ![](images/fudgeflake0.png) ![](images/fudgeflake1.png) ![](images/fudgeflake2.png) ![](images/fudgeflake3.png) ![](images/fudgeflake4.png) 125 | 126 | * Levy tapestry 127 | 128 | ![](images/levy0.png) ![](images/levy1.png) ![](images/levy2.png) ![](images/levy3.png) ![](images/levy4.png) 129 | ![](images/levy5.png) ![](images/levy6.png) ![](images/levy7.png) ![](images/levy8.png) ![](images/levy9.png) 130 | 131 | * Hilbert curve 132 | 133 | ![](images/hilbert0.png) ![](images/hilbert1.png) ![](images/hilbert2.png) ![](images/hilbert3.png) ![](images/hilbert4.png) 134 | 135 | 136 | * Sierpinski curve 137 | 138 | ![](images/sierpinski-curve0.png) ![](images/sierpinski-curve1.png) ![](images/sierpinski-curve2.png) ![](images/sierpinski-curve3.png) ![](images/sierpinski-curve4.png) 139 | 140 | * Dragon curve 141 | 142 | ![](images/dragon0.png) ![](images/dragon1.png) ![](images/dragon2.png) ![](images/dragon3.png) ![](images/dragon4.png) 143 | 144 | ![](images/dragon5.png) ![](images/dragon6.png) ![](images/dragon7.png) ![](images/dragon8.png) ![](images/dragon9.png) 145 | 146 | 147 | 5. 你能否设计出自己的分形? 148 | -------------------------------------------------------------------------------- /projects/build_data_types/images/brown1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/brown1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/brown2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/brown2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/brown3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/brown3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/brown4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/brown4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/complex_polar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/complex_polar.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon5.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon6.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon7.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon8.png -------------------------------------------------------------------------------- /projects/build_data_types/images/dragon9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/dragon9.png -------------------------------------------------------------------------------- /projects/build_data_types/images/fudgeflake0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/fudgeflake0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/fudgeflake1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/fudgeflake1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/fudgeflake2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/fudgeflake2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/fudgeflake3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/fudgeflake3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/fudgeflake4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/fudgeflake4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/gosper0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/gosper0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/gosper1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/gosper1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/gosper2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/gosper2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/gosper3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/gosper3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/gosper4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/gosper4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/hilbert0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/hilbert0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/hilbert1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/hilbert1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/hilbert2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/hilbert2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/hilbert3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/hilbert3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/hilbert4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/hilbert4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch_snow0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch_snow0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch_snow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch_snow1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch_snow2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch_snow2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/koch_snow3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/koch_snow3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/kochrainbow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/kochrainbow1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy10.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy5.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy6.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy7.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy8.png -------------------------------------------------------------------------------- /projects/build_data_types/images/levy9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/levy9.png -------------------------------------------------------------------------------- /projects/build_data_types/images/ngon3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/ngon3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/ngon4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/ngon4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/ngon5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/ngon5.png -------------------------------------------------------------------------------- /projects/build_data_types/images/ngon6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/ngon6.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sausage0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sausage0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sausage1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sausage1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sausage2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sausage2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sausage3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sausage3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sausage4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sausage4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sierpinski-curve0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sierpinski-curve0.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sierpinski-curve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sierpinski-curve1.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sierpinski-curve2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sierpinski-curve2.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sierpinski-curve3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sierpinski-curve3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/sierpinski-curve4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/sierpinski-curve4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/spiral3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/spiral3.png -------------------------------------------------------------------------------- /projects/build_data_types/images/spiral4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/spiral4.png -------------------------------------------------------------------------------- /projects/build_data_types/images/spiral5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/spiral5.png -------------------------------------------------------------------------------- /projects/build_data_types/images/spiral6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/build_data_types/images/spiral6.png -------------------------------------------------------------------------------- /projects/container_io/README.md: -------------------------------------------------------------------------------- 1 | # 容器和I/O 2 | 3 | ## 概述 4 | 5 | 本次作业将练习容器的使用和I/O操作. 首先我们将使用 java 容器进行基本的文本处理工作. 随后我们将利用队列实现一个较为复杂的调度模型. 最后对与 I/O 操作, 我们将通过阅读两段简单的 Java 的源码, 理解两个装饰器的实现原理. 6 | 7 | ## 任务 8 | 9 | [a 容器](container.md) 10 | 11 | [b I/O](io.md) 12 | 13 | 18 | -------------------------------------------------------------------------------- /projects/container_io/container.md: -------------------------------------------------------------------------------- 1 | # 容器 2 | 3 | 请利用 java 容器完成以下问题. 4 | 5 | ## 实验内容 6 | 7 | 1. 给定文本文件, 将文本中所有的单词读入一个 List 中. 8 | 2. 给定文本文件, 统计文本中有多少不同的单词. 9 | 3. 给定文本文件, 统计文本中不同单词的出现次数. 10 | 4. 定义稀疏矩阵类 SparseMatrix. 稀疏矩阵与普通矩阵相比, 矩阵中含有大量的0元素. 对于这样的矩阵, 用二维数组存储会造成存储空间的浪费 (特别是矩阵的行数列数很大时). 请选择合适容器来实现稀疏矩阵. 同时实现矩阵的加法和乘法. 11 | 12 | -------------------------------------------------------------------------------- /projects/container_io/images/queue.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/container_io/images/queue.jpg -------------------------------------------------------------------------------- /projects/container_io/images/queue.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/container_io/images/queue.odp -------------------------------------------------------------------------------- /projects/container_io/io.md: -------------------------------------------------------------------------------- 1 | # I/O 2 | 3 | ## 实验内容 4 | 5 | 1. 给定目录名, 找出该目录下的所有文件和子目录, 并递归的列出所有子目录的内容. 按照以下方式输出 6 | 7 | ``` 8 | parent_dir1: 9 | |- a.txt 10 | |- b.txt 11 | |- sub_dir1: 12 | |- e.txt 13 | |- f.txt 14 | |- c.txt 15 | |- d.txt 16 | parent_dir2: 17 | | g.txt 18 | ... 19 | ``` 20 | 21 | 22 | 2. 请**通过编写程序**, 比较带缓冲的流和不带缓冲的流在读写性能上的差距. 请给出实验程序源码, 并说明实验比较步骤和实验结果. 23 | 24 | 28 | 29 | 3. 请阅读 [DataInputStream](http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/e146e0fb6dee/src/share/classes/java/io/DataInputStream.java) 的源代码 (openjdk), 请观察它的继承关系, 尝试理解每个方法的具体实现, 提交readBoolean(), readInt() 两个方法的逐行注释及算法说明。请提交注释后的源文件(可以在这里下载[DataInputStream.java](http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/raw-file/e146e0fb6dee/src/share/classes/java/io/DataInputStream.java)). 30 | 31 | -------------------------------------------------------------------------------- /projects/container_io/printer.md: -------------------------------------------------------------------------------- 1 | # 打印机服务程序 2 | 3 | ## 背景 4 | 5 | 假设一台打印机服务器为许多用户提供打印服务. 该打印机同一时间只能打印一份文档. 因此, 对于不同时刻送达的打印任务, 我们需要设计调度策略来安排各个任务何时被执行. 6 | 7 | 最简单的调度策略为"先来先服务" (First in, First Serve (FIFS)), 即, 按照任务到来的先后顺序安排打印, 先来的任务先被执行. FIFS 的优点为简单, 公平(只要排队, 任何任务都会被执行). 但缺点在于对于一些耗时较小的打印任务(比如, 页数比较少的文档)会等待较长的时间 (设想你去超市买一瓶水, 发现排在你前面人买了一车食物准备冬眠). 8 | 9 | 为了解决 FIFS 的问题, 我们可以给打印任务分配不同的优先级 (打印页数越少, 优先级越高), 不同优先级的任务放入对应优先级的队列中. 10 | 假设服务器有 $n$ 个队列 $q_0, q_1, ..., q_{n-1}$, 其中 $q_0$ 的优先级最高, 其次为 $q_1$, 而 $q_{n-1}$ 为最低优先级. 11 | 在调度任务时, 首先从优先级最高的 $q_0$ 队列中选择任务, 若 $q_0$ 为空, 则从次高优先级的 $q_1$ 队列中选择. 以此类推. 12 | 这样能够让耗时较小的任务优先执行, 从而提高了单位时间内能够服务的用户数量. 但"优先级"策略也带来了"饥饿"效应: 耗时较长的任务可能会永远得不到执行. 因此, 以上方案仍然需要修改. 13 | 14 | 最终的策略为"动态优先级", 即, 打印任务的优先级可以动态的更新. 如果低优先级的任务长时间没有得到执行, 15 | 我们将提高它的优先级, 并把它加入较高优先级的队列中. 具体来说, 当一个队列 $q_i$ 被访问超过一个预先设定的次数 $k$ , 16 | 则优先级低于 $i$ 的队列就可能存在饥饿. 因此, 此时我们会将一个低优先级队列中的任务移到队列 $q_i$ 中来提升它的优先级. 17 | 18 | ![](images/queue.jpg) 19 | 20 | 以下为"动态优先级"调度策略的详细描述. 21 | 22 | * 任何时刻, 用户都可以向服务器发送打印请求 23 | - 每个请求包含打印文件的文件名字, 页数. 24 | * 服务器根据文件页数初始化该请求的优先级, 并将该打印请求放入对应的队列中. 25 | - 具体的初始化优先级算法见后续描述 26 | - 如果打印机空闲, 则从优先级最高的非空队列中选择请求进行打印 27 | - 如果打印机忙, 则让其继续打印 28 | - 当一个打印任务完成, 服务器将进行以下操作 29 | - 如果所有队列为空, 则等待新的任务到来. 30 | - 如果存在队列非空, 则从优先级最高的非空队列中选择一个请求. 同时, 如果一个队列 $q_i$ 已经被访问过 $k$ 次, 则选择优先级最高非空队列 $q_j, j > i$, 从 $q_j$ 中选择一个任务将其放入 $q_i$ 中, 并清空 $q_i$的访问次数. 31 | 32 | 33 | 我们有三种策略初始化打印请求的优先级 34 | 35 | - A. 所有请求赋予相同优先级, 放入 $q_0$ 中. 等价于只有一个队列的 FIFS. 36 | - B. 使用 n 个队列, 根据文档页数分配优先级 37 | - 页数在 1 到 10 之间, 则进入队列 $q_0$ 38 | - 页数在 11 到 20, 则进入队列 $q_1$ 39 | - ... 40 | - 页数在 10(n-2)+1 到 10(n-1) 之间, 则进入队列 $q_{n-2}$ 41 | - 页数超过 10(n-1), 则进入队列 $q_{n-1}$ 42 | - C. 使用 n 个队列, 若文件页数为 t, 则进入队列 $q_{t\\%n}$ 43 | 44 | 最后, 做如下假设: 有一个代表时钟的变量 `int clock`. clock 每增加1代表时钟过了1秒. 每打印一页耗时1秒. 45 | 46 | ## 实验内容 47 | 48 | 1. 定义 PrintRequest 类表示打印请求. 需要包含: 文件的名称, 页数以及发出该请求时的时间. 49 | 50 | 2. 定义 Printer 类表示打印机. 至少包含以下方法 51 | - `boolean printerIdle()`: 如果打印机空闲返回 true, 否则返回 false 52 | - `boolean printFile(PrintRequest r)`: 如果打印机空闲, 则开始处理打印任务 r, 并返回 true. 否则忽略改请求, 返回 false. 53 | - `PrintRequest processForOneUnit()`, 如果打印机空闲, 或者当前正在打印的文档还剩余超过1页则返回 null. 如果当前文档已完成, 则返回当前的打印任务对象. 54 | 55 | 3. 定义 PrintQueue 类表示一个队列 (提示: 参考 Queue 接口. 主要的操作是"进队列"和"出队列". 同时 队列可能需要记录当前已经被访问了多少次). 56 | 57 | 4. 定义 PrintDispatcher 类为一个队列. 它包含 n 个 PrintQueue 对象, 而相应的"进队列", "出队列"操作则完成上述"动态优先级"策略. 包含 toString() 方法可以显示逐个队列的状态. 58 | 59 | 5. 定义 PrintSimulation 类, 模拟打印场景. 60 | - 从命令行接收4个参数, 分别 61 | - p: 当前这1秒钟有打印请求到来的概率 62 | - n: 使用多少个队列 63 | - k: 当某个队列被访问了 k 次后, 将提升优先级 64 | - a: 选择哪种初始化优先级的策略 (A, B, C) 65 | - 模拟每一秒系统的行为. 可能出现的情况为 66 | - 如果打印机当前正忙, 则打印当前任务的一页. 67 | - 同时, 以概率 p 产生一个打印请求. 该请求的文档页数为 1 到 100 间的随机数, 发出请求的时间为当前 clock 变量的值. 该打印请求被放入 PrintDispatcher 中 (通过调用enqueue() 方法). 68 | - 如果打印机空闲, 且有请求正在等待, 则通过 PrintDispatcher 选择一个任务交给 Printer (通过调用 dequeue() 方法). 同时, 通过该任务的发出时间, 计算该任务的总共的等待时间. 69 | - 当以上过程进行了 1000 秒后 (clock = 0, 1, ..., 999), 不再产生新的请求. 但过程将继续下去, 直到所有没有完成任务被完成. 70 | - 输出所有任务中, 最长的等待时间和平均等待时间. 71 | - 请绘制直方图, 横坐标表示页数 (10, 20, ..., 10(n-1), >10(n-1)), 纵坐标表示页数落在对应横坐标范围内的所有请求的平均等待时间. 72 | 73 | 6. 请**根据程序运行结果**分析不同参数对系统的影响. 74 | 75 | -------------------------------------------------------------------------------- /projects/familiar_with_toolkits/README.md: -------------------------------------------------------------------------------- 1 | # 熟悉数组,字符串以及FileIO 2 | 3 | 在实验过程中我们将频繁使用数组,字符串和文件操作。希望通过本次实验大家能够熟悉这些内容。 4 | 5 | ## 字符串 6 | 7 | 1. 逐个运行**StringDemo.java**中的代码。(自己运行熟悉,不用提交) 8 | 11 | 12 | ## 数组 13 | 14 | 1. 逐个运行**ArrayDemo.java**中的代码。(自己运行熟悉,不用提交) 15 | 18 | 19 | ## 文件IO 20 | 21 | 1. 学会调用 **FileIO.java** 中的函数。 22 | 23 | - 调用4次writeStringToFile(),逐行写入以下内容到test.txt中(总共四行) 24 | 25 | ``` 26 | 《老人与海》这本小说是根据真人真事写的。第一次世界大战结束后,海明威移居古巴,认识了老渔民格雷戈里奥·富恩特斯。 27 | 1930年,海明威乘的船在暴风雨中沉没,富恩特斯搭救了海明威。从此,海明威与富恩特斯结下了深厚的友谊,并经常一起出海捕鱼。 28 | The novel The Old Man and the Sea is based on a real story. After the end of World War I, Hemingway moved to Cuba, where he met an old fisherman, Gregorio Fuentes. 29 | In 1930, Hemingway was rescued by Fuentes when his boat sank in a storm. From then on, Hemingway and Fuentes formed a deep friendship, and often went fishing together. 30 | ``` 31 | 32 | - 针对test.txt文件,调用getCharFromFile(),返回第5个字符,并打印出来。 33 | - 针对test.txt文件,调用getLineFromFile(),返回第3行,并打印出来。 34 | - 针对test.txt文件,调用getAllLinesFromFile(),返回所有行,并打印出来。 35 | -------------------------------------------------------------------------------- /projects/inheritance/README.md: -------------------------------------------------------------------------------- 1 | # 继承与组合 2 | 3 | ## 概述 4 | 5 | 继承与组合是两种主要的代码重用方式. 通过编程语言显式的语法支持, 面向对象语言能够不断扩展所支持的数据类型, 帮助程序员更快的对实际问题建模和求解. 本次作业将复习继承与组合的基本概念, 同时将重用思想应用到一个简单的场景. 6 | 7 | ## 任务 8 | 9 | [a 继承与组合基础](inheritance_basic.md) 10 | 11 | [b 电路网络](circuits.md) 12 | 13 | 14 | -------------------------------------------------------------------------------- /projects/inheritance/circuits.md: -------------------------------------------------------------------------------- 1 | # 电阻网络 2 | 3 | ## 背景 4 | 5 | 物体的电阻 (resistance) 用来衡量当电流通过物体时会收到"阻碍". 6 | 电阻越大, 电流同过该物体时受到的阻碍就越大. 欧姆定律刻画了电阻, 电流和电压之间的关系. 7 | 8 | $$\begin{eqnarray*} 9 | I = \frac{V}{R} 10 | \end{eqnarray*}$$ 11 | 12 | 其中, V 表示物体两端的电压(电势差), R 为该物体的电阻, I 为通过物体的电流.电阻可以通过串联和并联的方式组合成为复杂的电阻网络: 13 | 14 | * 串联 (Series): 将两个电阻首尾相连. 其等效电阻等于两个电阻的和. 15 | * 并联 (Parallel): 将两个电阻并行排列. 其等效电阻等于倒数和的倒数. 16 | 17 | ![](images/circuits/seriesparallel.png) 18 | 19 | 例如, 以下网络由两个串联电阻与第三个电阻并联形成. 它的等效电阻为 $\frac{1}{\frac{1}{R_1} + \frac{1}{R_2 + R_3}}$ 20 | 21 | ![](images/circuits/combination.png) 22 | 23 | 24 | ## 如何表示网络 25 | 26 | 一个通过串联和并联搭建的电阻网络可以表示成为字符串. 定义 27 | 28 | * $R$ 表示单个电阻器 29 | * $a$ 为正数, 表示单个电阻器的取值 30 | * $N$ 为电阻网络 31 | * $(-, N, N)$ 表示两个电阻网络串联后的得到的网络 32 | * $(/, N, N)$ 表示两个电阻网络并联后的得到的网络 33 | 34 | 电阻网络可以按照以下规则表示 35 | 36 | 1. $N = (-, N, N)|(/, N, N)|R$ 37 | 2. $R = a$ 38 | 39 | 其中"|"表示或者. 规则1 表示一个电路网络要么由两个网络串联而成, 要么由两个网络并联而成, 要么是单个电阻器. 规则2 表示一个电阻器的电阻取值是多少. 40 | 41 | 42 | 下面是几个例子: 43 | 44 | 1. 两个电阻为3和5的电阻器串联可以表示为: "(-, 3, 5)" 45 | 2. 两个电阻为3和5的电阻器并联可以表示为: "(/, 3, 5)" 46 | 3. 以下网络可以表示成为: "(-, (/, (/, 4, 8), 5), 3)" 47 | 48 | ![](images/circuits/parallel3.png) 49 | 50 | 4. 以下网络可以表示成为: "(/, (-, (/, 6, 3), 2), (-, 3, 3))" 51 | 52 | ![](images/circuits/circuit2.png) 53 | 54 | 这样的表示方式称为 [上下文无关文法 (Context Free Gramma)](https://zh.wikipedia.org/wiki/%E4%B8%8A%E4%B8%8B%E6%96%87%E6%97%A0%E5%85%B3%E6%96%87%E6%B3%95), 它是在程序语言设计, 人工智能等方向有广泛应用. 55 | 56 | ## 实验内容 57 | 58 | 1. 写出下列网络的字符串表示 59 | 60 | ![](images/circuits/series3.png) 61 | 62 | ![](images/circuits/circuit.png) 63 | 64 | ![](images/circuits/bigcircuit.png) 65 | 66 | ![](images/circuits/bigcircuit2.png) 67 | 68 | 2. 定义 Circuit 类用来表示电路网络. 69 | 70 | ```java 71 | public abstract class Circuit { 72 | public abstract double resistance(); 73 | } 74 | ``` 75 | resistance() 返回该网络的等效电阻值. 给定网络的字符串表示, 构造相应的 Circuit 对象, 并输出它们的等效电阻值. 76 | 77 | 3. 修改 Circuit 类, 使得给定电源电压, 能够计算任意子网络两端的电势差以及流经该网络的电流. 78 | 79 | ![](images/circuits/circuit2withbattery.png) 80 | 81 | 82 | -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/bigcircuit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/bigcircuit.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/bigcircuit2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/bigcircuit2.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/circuit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/circuit.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/circuit2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/circuit2.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/circuit2withbattery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/circuit2withbattery.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/combination.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/combination.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/parallel3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/parallel3.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/series3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/series3.png -------------------------------------------------------------------------------- /projects/inheritance/images/circuits/seriesparallel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/inheritance/images/circuits/seriesparallel.png -------------------------------------------------------------------------------- /projects/inheritance/inheritance_basic.md: -------------------------------------------------------------------------------- 1 | # 继承与组合基础 2 | 3 | ## 实验内容 4 | 5 | 1. 定义 Name 类包含以下成员, 其中数据成员是私有的, 方法是公开的(一些方法的参数列表并不完整, 请补充完整). 6 | 7 | |数据成员 |说明| 8 | |---|---| 9 | |firstName |名| 10 | |lastName |姓| 11 | 12 | |方法 | 说明| 13 | |---|---| 14 | |Name() |构造函数| 15 | |Name(f, l) |构造函数, 其中f为名, l为姓| 16 | |getFirstName() |返回名| 17 | |setFirstName(f) |设置名| 18 | |getLastName() |返回姓| 19 | |setLastName(f) |设置姓| 20 | |toString() |转化为String| 21 | 22 | 2. 定义 Person 类, 包含以下成员(所有方法都是公开的, 其中一些方法的参数列表并不完整, 请补充完整). 23 | 24 | |数据成员 |说明| 25 | |---|---| 26 | |name |姓名( Name 对象, 公开的)| 27 | |age |年龄(常量, 公开的)| 28 | |gender |性别(私有的)| 29 | 30 | |方法 | 说明| 31 | |---|---| 32 | |Person() |构造函数| 33 | |Person(a, g, n) |构造函数, 其中 a 为年龄, g 为性别, n 为姓名| 34 | |getGender() |返回性别| 35 | |setGender() |设置性别| 36 | |talk() |输出"Hi, how is it going"| 37 | |talk(s) |输出字符串对象s| 38 | |chatWith(p, s) |p 为 Person 对象, s 为字符串对象. 输出"A to B: %s", 其中 A 是调用者的姓名, B 是 p 的姓名| 39 | 40 | 3. 请通过试验回答以下问题并说明原因 41 | * 在 Name 类的主函数中是否能访问 Name 对象的数据成员 42 | * 在 Person 类中(例如 chatWith 方法中)能否直接访问 name 对象的数据成员 43 | * 如果将 Name 的数据成员变为 package access, 上一个问题的答案会发生改变吗 44 | * 如果将 Person 类与 Name 类分别放入两个不同的包, 则以上问题的答案会发生改变吗 45 | 46 | 4. 定义 Student 类为 Person 类的子类. `talk()` 函数输出"Hi, how is your homework going?" 47 | 5. 定义 Teacher 类为 Person 类的子类. `talk()` 函数输出"Hi, how is your paper going?" 48 | 6. 请编写程序验证 Teacher, Student 类中talk()方法是动态绑定的. 49 | 7. 定义 PersonFactory 类, 包含方法 `Person next()`, 随机生成一个 Teacher 或者 Student 对象 50 | 8. 利用 PersonFactory 类, 定义 Discussion 类, 其中包含若干 Teacher 对象和若干 Student 对象. 同时包含方法 broadcast(): 随机选择一个 Teacher 或者 Student 对象, 并对其余对象调用 chatWith() 方法. 51 | 9. 定义 ElderTeacher 类为 Teacher 类的子类, 要求 ElderTeacher 类最多只能有一个实例 52 | 10. 假设类 Sup 包含方法 f(), 返回值为类型A; 类 Sub 为 Sup 的子类, 包含方法 f(), 返回值为类型B. 通过程序试验回答以下问题. 53 | * 若 A, B 类型相同, 运行程序会出现什么情况? 54 | * 若 A 为数组, B为 int, 会出现什么情况? 55 | * 若 B 是 A 的子类, 会出现什么情况? 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /projects/interface/README.md: -------------------------------------------------------------------------------- 1 | # 接口 2 | 3 | ## 概述 4 | 5 | 与继承相比, 接口强调多态而摒弃了代码重用. 由于一个类可以实现多个接口, 我们可以将一个类 upcasting 到不同的类. 本次作业将首先复习接口和抽象类的基本概念, 随后通过两个具体的例子了解多接口, 接口的扩展, 内部类等概念. 6 | 7 | ## 任务 8 | 9 | [a 接口与抽象类基础](interface_basic.md) 10 | 11 | [b 数值计算](function.md) 12 | 13 | [c 序列与迭代器](list.md) 14 | -------------------------------------------------------------------------------- /projects/interface/function.md: -------------------------------------------------------------------------------- 1 | # 数值计算 2 | 3 | ## 背景 4 | 5 | ### 数值积分 (Numerical Integration) 6 | 7 | 微积分基本定理 (Newton-Leibniz 公式) $\int_a^b f(x)\mathrm{d}x = F(b) - F(a)$ 是求解定积分的重要方法. 但在实际应用中我们发现仅使用 N-L 公式是不够的. 一方面, 大部分可积函数的原函数不是初等函数(例如 $\frac{\sin(x)}{x}$ ), 无法使用 N-L 公式. 另一方面, 许多函数只是通过实验, 采样等方法获得若干自变量对应的函数值, 而无法确定函数的表达式. 针对这些问题, 我们需要近似算法来估计定积分的值, 其中数值积分是一类重要方法. 8 | 9 | 为了求解 $\int_a^b f(x)\mathrm{d}x$, 我们希望通过一个可积函数 $p(x)$ 来代替 $f(x)$. 一方面 $p(x)$ 的原函数比较容易求出. 另一方面, $p(x)$ 能够充分逼近 $f(x)$, 使得 $\int_a^b f(x) \approx \int_a^b p(x)$. 在这里, 我们将 $p(x)$ 取为n阶多项式函数 $p_n(x)$. 10 | 11 | 将积分区间 $[a, b]$ 以步长 $h=\frac{b-a}{n}$ 划分为 $n$ 等分. 以分点 $x_i = a+ih, i = 0, 1, ..., n$ 为结点, 做 $f(x)$ 的 [Lagrange 插值多项式](https://en.wikipedia.org/wiki/Lagrange_polynomial) $p_n(x)$ , 12 | 13 | $$\begin{eqnarray*} 14 | f(x) \approx p_n(x) = \sum_{i=0}^n\left(\prod_{j=0, j\neq i}^n\frac{x-x_j}{x_i-x_j}\right)f(x_i) 15 | \end{eqnarray*}$$ 16 | 17 | 容易验证, $p_n(x_i) = f(x_i)$, 即在分点 $x_1, x_2, ..., x_n$ 处 $p_n(x) = f(x)$. 进一步有, 18 | 19 | $$\begin{eqnarray*} 20 | \int_a^bf(x)\mathrm{d}x \approx \int_a^b p_n(x)\mathrm{d}x = (b-a)\sum_{i=0}^nC_i^{(n)}f(x_i) 21 | \end{eqnarray*}$$ 22 | 23 | 其中, 24 | 25 | $$\begin{eqnarray*} 26 | C_i^{(n)} = \frac{1}{b-a} \int_a^b\prod_{j=0, j\neq i}^n\frac{x-x_j}{x_i-x_j}\mathrm{d}x 27 | \end{eqnarray*}$$ 28 | 29 | 令 $x = a+th$, 30 | 31 | $$\begin{eqnarray*} 32 | C_i^{(n)} = \frac{h}{b-a} \int_0^n\prod_{j=0, j\neq i}^n\frac{t-j}{i-j}\mathrm{d}t 33 | = \frac{1}{n}\frac{(-1)^{n-i}}{i!(n-i)!} \int_0^n\prod_{j=0, j\neq i}^n(t-j)\mathrm{d}t 34 | \end{eqnarray*}$$ 35 | 36 | 这称为 *Newton-Cotes 积分公式*. 通过N-C公式计算定积分, 需要给定 $n+1$ 个分点处的函数值 $f(x_0), f(x_2), ..., f(x_n)$. $C_i^{(n)}$ 称为 Cotes 系数, 它与被积区间与被积函数 $f(x)$ 无关, 可以事先计算好. 37 | 38 | 容易看出 $C_i^{(n)}$ 有如下性质 39 | 40 | - $C_i^{(n)} = C_{n-i}^{(n)}, \forall i = 0, 1, ..., n$ 41 | - 由于对 $f(x)\equiv 1$, Newton-Cotes 公式精确成立, 因此 42 | 43 | $$\begin{eqnarray*} 44 | & \int_a^b 1\cdot\mathrm{d}x = (b-a)\sum_{i=0}^n C_i^{(n)} \\ 45 | \Rightarrow & \sum_{i=0}^n C_i^{(n)} = 1 46 | \end{eqnarray*}$$ 47 | 48 | ### 梯形公式 49 | 50 | 在 Newton-Cotes 公式中取 $n=1$ (即使用过点 $(a, f(a)), (b, f(b))$ 的线性函数来拟合 $f(x)$ ), 由 Cotes 系数的性质可得 $C_0^{(1)} = C_1^{(1)} = \frac{1}{2}$, 因此 51 | 52 | $$\begin{eqnarray*} 53 | \int_a^bf(x)\mathrm{d}x \approx \frac{b-a}{2}\left(f(a) + f(b)\right) 54 | \end{eqnarray*}$$ 55 | 56 | 几何意义是, 通过梯形的面积来近似函数 $f(x)$ 所围成的面积. 57 | 58 | ### Simpson 公式 59 | 60 | 在 Newton-Cotes 公式中取 $n=2$, 61 | 62 | $$\begin{eqnarray*} 63 | & C_0^{(2)} = \frac{1}{4}\int_0^2(t-1)(t-2)\mathrm{d}t = \frac{1}{6} = C_2^{(2)}\\ 64 | & C_1^{(2)} = 1- C_0^{(2)} - C_2^{(2)}= \frac{4}{6} 65 | \end{eqnarray*}$$ 66 | 67 | 因此得到 Simpson 公式: 68 | 69 | $$\begin{eqnarray*} 70 | \int_a^bf(x)\mathrm{d}x \approx \frac{b-a}{6}\left(f(a) + 4f\left(\frac{a+b}{2}\right)+ f(b)\right) 71 | \end{eqnarray*}$$ 72 | 73 | ### 复化的梯形公式 74 | 75 | 对积分区间直接使用梯形公式得到的近似往往比较粗略. 我们可以将积分区间 $[a, b]$ 划分为更小的区间, 通过在每一个小的区间上使用梯形公式得到更精确的结果. 这也称为复化的梯形公式. 76 | 77 | 78 | 将$[a, b]$ 以步长 $h=\frac{b-a}{m}$ 划分为 $m$ 等分. $x_i = a+ih, i = 0, 1, ..., m$. 在每一个小区间 $[x_{i-1}, x_i]$ 上使用梯形公式 79 | 80 | $$\begin{eqnarray*} 81 | \int_a^bf(x)\mathrm{d}x = \sum_{i=1}^m \int_{x_{i-1}}^{x_i} f(x)\mathrm{d}x \approx \sum_{i=1}^m \frac{h}{2}\left(f(x_{i-1}) + f(x_i)\right) = T_m^{(1)} 82 | \end{eqnarray*}$$ 83 | 84 | 其中 85 | 86 | $$\begin{eqnarray*} 87 | T_m^{(1)} = \frac{h}{2}\left(f(a) + f(b) + 2\sum_{i=1}^{m-1}f(x_i)\right) 88 | \end{eqnarray*}$$ 89 | 90 | ### 复化的 Simpson 公式 91 | 92 | 我们可以用同样的方法在小区间中使用 Simpson 公式. 93 | 令 $x_{i-\frac{1}{2}}$ 为$[x_{i-1}, x_i]$ 的中点 94 | 95 | $$\begin{eqnarray*} 96 | \int_a^bf(x)\mathrm{d}x = \sum_{i=1}^m \int_{x_{i-1}}^{x_i} f(x)\mathrm{d}x \approx T_m^{(2)} 97 | \end{eqnarray*}$$ 98 | 99 | 其中 100 | 101 | $$\begin{eqnarray*} 102 | T_m^{(2)} = \frac{h}{6}\left(f(a) + f(b) + 2\sum_{i=1}^{m-1}f(x_i) + 4\sum_{i=1}^mf(x_{i-\frac{1}{2}})\right) 103 | \end{eqnarray*}$$ 104 | 105 | 106 | ## 实验内容 107 | 108 | 1. 定义接口 Function, 包含 eval 方法, 返回函数在自变量 x 处的取值. 109 | 110 | ```java 111 | double eval(double x) 112 | ``` 113 | 114 | 2. 定义 接口 DifferentiableFunction, 扩展 Function 接口表示可微函数, 包含 diff 方法, 返回导函数在变量 x 处的取值. 115 | 116 | ```java 117 | double diff(double x) 118 | ``` 119 | 120 | 3. 通过实现 DifferentiableFunction 接口, 实现以下类 121 | - Linear 类. 代表函数 $kx+b$, 其中 $k, b$ 为参数 122 | - Quadartic 类. 代表函数 $ax^2 + bx + c$ 其中, $a, b, c$ 为参数 123 | - Sin 类. 代表函数 $\sin(\omega x + \varphi)$, 其中 $\omega, \varphi$ 为参数 124 | - NormalPDF 类. 代表函数 $e^{-\frac{(x-\mu)^2}{2\sigma^2}}$, 其中 $\mu, \sigma$ 为参数. 125 | 126 | 4. 定义类 NewtonRoot , 包含方法 `findRoot`. 使用牛顿法寻找函数 f 的根 (见 Project 2 第6题). 并对以上四个函数类进行测试. 127 | 128 | ```java 129 | double findRoot(DifferentiableFunction f) 130 | ``` 131 | 132 | 5. 定义类 NewtonCatos, 包含方法 `Trapozoidal` 使用梯形公式计算 f 在 $[a, b]$ 上的积分. 同时包含方法 `Simpson`, 使用 Simpson 公式计算定积分. 对以上四个函数类进行测试. 133 | 134 | ```java 135 | double Trapozoidal(Function f, double a, double b) 136 | double Simpson(Function f, double a, double b) 137 | ``` 138 | 139 | 140 | -------------------------------------------------------------------------------- /projects/interface/interface_basic.md: -------------------------------------------------------------------------------- 1 | # 接口与抽象类基础 2 | 3 | ## 实验内容 4 | 5 | 1. 定义并实现以下商品相关的类结构. 6 | 7 | - Item 抽象类. 8 | 9 | |数据成员| 说明| 10 | |---|---| 11 | |name (String, protected, final) |商品名称| 12 | |cost (double, protected, final) |价格| 13 | 14 | |方法 |说明| 15 | |---|---| 16 | |getCost() |返回价格| 17 | |getName() |返回名称| 18 | |costPerUnit() |单价, 抽象方法| 19 | |toString() |转化为 String| 20 | 21 | - Produce 类 (生鲜商品). 为 Item 的子类. 22 | 23 | |数据成员| 说明| 24 | |---|---| 25 | |pounds (double, private) |商品重量| 26 | |category (String, private) |种类 (例如: Vegetable, fruit)| 27 | 28 | |方法 |说明| 29 | |---|---| 30 | |costPerUnit() |单价| 31 | |getPounds() |返回重量(Kg)| 32 | |setPounds() |设置重量| 33 | |getCategory() |返回种类| 34 | |setCategory() |设置种类| 35 | |toString() |转化为 String| 36 | 37 | - Beverage 类 (饮品). 为 Item 的子类 38 | 39 | |数据成员| 说明| 40 | |---|---| 41 | |volume (double, private) |商品体积(L)| 42 | |containerDeposit (double, private) |回收费用| 43 | 44 | |方法 |说明| 45 | |---|---| 46 | |costPerUnit() |单价 | 47 | |getCost() |重写返回价格函数, 需回收费用| 48 | |getVolume() |返回体积| 49 | |setVolume() |设置体积| 50 | |getContainerDeposit() |返回回收费用| 51 | |setContainerDeposit() |设置回收费用| 52 | |toString() |转化为 String| 53 | 54 | 55 | - Package 类(包装在盒子中的商品). 为 Item 的子类 56 | 57 | |数据成员| 说明| 58 | |---|---| 59 | |length (double, private) |包装盒的长度| 60 | |width (double, private) |包装的宽度| 61 | |height (double, private) |包装的高度| 62 | 63 | |方法 |说明| 64 | |---|---| 65 | |costPerUnit() |单价 | 66 | |getSize() |返回体积| 67 | |toString() |转化为 String| 68 | 69 | - ShoppingCart 类 (购物车). 70 | 71 | |数据成员| 说明| 72 | |---|---| 73 | |cart (array, private) |放置 Item 的数组| 74 | |maxSize (int) |cart数组长度| 75 | |currentSize (int) |购物车当前有多少商品| 76 | 77 | |方法 |说明| 78 | |---|---| 79 | |addItem() |添加商品| 80 | |display() |输出商品信息| 81 | |totalCost() |输出购物车内商品总价| 82 | |numberInCart(s) |给定商品名称s, 查看购物车中有多少该商品| 83 | 84 | 2. 农场中有许多动物, 动物表示为 Animal 接口, 它包含方法 85 | 86 | |方法 |说明| 87 | |---|---| 88 | |String getType() |返回动物的类型| 89 | |String getSound() |返回动物的叫声(比如"moo" (牛), "cheep"(鸡), "oink" (猪))| 90 | 91 | - 编写Cow, Hen, Pig 类, 实现 Animal 接口. 92 | - 定义 Farm 类, 包含以下方法 93 | 94 | |数据成员| 说明| 95 | |---|---| 96 | |animals (array, private) |包含农场中的动物| 97 | 98 | |方法 |说明| 99 | |---|---| 100 | |animalSound() |打印农场中所有动物的叫声| 101 | 102 | - 另外有接口 MilkProvider, EggProvider, 分别包含方法 `getMilk()`, `getEgg()`. 修改 Cow, Hen 类的定义, 在实现 Animal 类基础上, 分别实现 MilkProvider 接口 和 EggProvider 接口. 103 | - 定义 Farmer 类, 包含方法 `fetchMilk(MilkProvider a), fetchEgg(EggProvider a)`. 104 | - 在 Farm 类中添加数据成员 `Farmer farmer`, `MilkProvider []mp`, `EggProvider []ep`. 数组 `mp, ep` 分别包含 animal 数组中的 MilkProvider 和 EggProvider. 105 | - 在 Farm 类中添加新方法 `produce()`, 对`mp, ep`数组中的对象, 调用 Farmer 对象的 `fetchMilk, fetchEgg` 方法. 106 | 107 | 108 | -------------------------------------------------------------------------------- /projects/interface/list.md: -------------------------------------------------------------------------------- 1 | # 序列与迭代器 2 | 3 | ## 实验内容 4 | 5 | 1. 定义 SequenceItem 类, 包含一个字符串作为数据, 并提供以下方法 6 | 7 | |方法 |说明| 8 | |---|---| 9 | |getData() |返回所包含的字符串| 10 | |setData() |设置所包含的字符串| 11 | 12 | 2. 定义 Sequence 接口, 包括以下方法 13 | 14 | |方法 |说明| 15 | |---|---| 16 | |add(item) |将 SequenceItem 类型的对象 item 加入序列| 17 | |get(i) |将第 i 个SequenceItem 对象返回| 18 | |remove(item) |删除 item 对象| 19 | |contains(item) |返回是否包含 item| 20 | |size() |返回序列长度| 21 | |isEmpty() |返回序列是否为空| 22 | |iterator() |返回一个迭代器对象, 用于顺序访问序列元素 (见 SeqIterator 接口描述)| 23 | |reverseIterator() |返回一个迭代器对象, 用于倒序访问序列元素| 24 | |biIterator() |返回一个迭代器对象, 可以双向访问序列元素 (见 SeqBiIterator 接口描述)| 25 | |toArray() |返回序列的数组表示| 26 | |equals(seq) |返回该序列是否与 seq 序列相同| 27 | |toString() |转化为 String| 28 | 29 | 3. 定义 SeqIterator 接口, 包括以下方法 30 | 31 | |方法 |说明| 32 | |---|---| 33 | |hasNext() |返回是否能取下一个 SequenceItem 对象| 34 | |next() |取下一个 SequenceItem 对象| 35 | |remove() |删除前一个被返回的 SequenceItem 对象| 36 | 37 | 4. 定义 SeqBiIterator 接口, 扩展 SeqIterator 接口, 包含以下方法 38 | 39 | |方法 |说明| 40 | |---|---| 41 | |hasPrevious() |返回是否能取前一个 SequenceItem 对象| 42 | |previous() |取前一个 SequenceItem 对象| 43 | 44 | 45 | 5. 编写类 ArraySequence 用可变长度数组实现 Sequence 接口, 使用内部类实现 iterator(), reverseIterator(), biIterator(). (提示: 使用数组存储 SequenceItem 对象, 当数组容量不够时, 申请一个两倍长度的数组. 当数组元素数量小于容量的1/4时, 缩减数组长度为1/2). 46 | 47 | 6. 编写类 IteratorTest, 包含静态方法 `display(Iterator i)` 顺序输出 Sequence 的内容. 48 | 49 | 7. (选做) 编写类 LinkedSequence 用[链表](https://en.wanweibaike.com/wiki-linked%20list)实现 Sequence 接口, 使用内部类实现 iterator(), reverseIterator(), biIterator(). 50 | 51 | 8. (选做) 请比较 LinkedSequence 与 ArraySequence 在插入, 删除, 访问(例如, 顺序访问, 倒序访问, 随机访问) 等操作的性能. 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /projects/use_data_types/README.md: -------------------------------------------------------------------------------- 1 | # 使用 Java 包 2 | 3 | ## 概述 4 | 5 | 相比于C语言来说, Java 或许更加面向实际问题. 6 | 它为程序员提供了丰富功能, 7 | 这些功能要么是由 Java 语言本身提供, 8 | 要么是由其他的开发者发布. 9 | 它们以包(package)的形式提供给用户. 而一个包由许多相关的类 (class) 构成. 10 | 在本次作业中, 你将学习如何使用现成的 Java 包来完成一些较为复杂的任务. 11 | 希望通过学习如何使用包, 能帮助你更进一步了解实际应用中的"对象"是如何被设计, 封装和调用. 12 | 这将会为今后学习如何编写自己的包提供一些启发. 13 | 14 | 15 | 16 | ## 任务 17 | 18 | [a 使用String类](string.md) 19 | 20 | [b 图像处理](image_processing.md) 21 | 22 | 23 | -------------------------------------------------------------------------------- /projects/use_data_types/images/baboon-grayscale.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/use_data_types/images/baboon-grayscale.jpg -------------------------------------------------------------------------------- /projects/use_data_types/images/baboon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/use_data_types/images/baboon.jpg -------------------------------------------------------------------------------- /projects/use_data_types/images/darwin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/use_data_types/images/darwin.jpg -------------------------------------------------------------------------------- /projects/use_data_types/images/mandrill.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/use_data_types/images/mandrill.jpg -------------------------------------------------------------------------------- /projects/use_data_types/images/mandrill200x200.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/use_data_types/images/mandrill200x200.jpg -------------------------------------------------------------------------------- /projects/use_data_types/images/mandrill200x400.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecnu-oop-java/ecnu-oop-java-up/429cf90fb8b22b3afdbdb9277d4e83506967a247/projects/use_data_types/images/mandrill200x400.jpg -------------------------------------------------------------------------------- /projects/use_data_types/string.md: -------------------------------------------------------------------------------- 1 | 2 | # 使用String 3 | 4 | 在这个Project中, 你将熟悉 Java String 类的使用. 5 | 几乎每一个 Java 程序都会使用到 String 对象. 6 | 回忆一下, 在 C 语言中, 字符串的处理需要调用 `string.h` 中的字符串函数. 7 | 例如 `strcmp, strlen` 等. 而从面向对象编程的视角来看, 8 | 它们可以看成每一个字符串提供给外界的访问接口. 9 | 外部程序通过调用这些接口可以获得字符串的信息. 10 | 而相比起 C 语言中仅将有限的字符串操作作为标准库函数, 11 | String 类作为 Java 语言的一个部分(你不用 import 任何包就能使用 String), 12 | 提供了更丰富的接口. 13 | 希望通过 String 你可以熟悉 Java 中类和对象的基本使用方法. 14 | 15 | ## String 简介 16 | 17 | 关于 String 类的详细信息, 18 | 可以参考 [Java 文档](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html). 19 | 也可以阅读Thinking in Java 中的 String 一章. 下面列出一些 String 类的主要方法: 20 | 21 | |方法 | 说明| 22 | |---|---| 23 | |String(char []s) |构造函数, 从字符串数组构造 String| 24 | |String(String s) |构造函数, 用String s 构造| 25 | |int length() |返回字符串长度| 26 | |char charAt(int i) |返回位置 i 处的字符| 27 | |String substring(int s, int e) |返回从位置 s 到位置 e 的子串| 28 | |String substring(int s) |返回从位置 s 开始的子串| 29 | |String toUpperCase() |返回一个全大写的字符串| 30 | |String toLowerCase() |返回一个全小写的字符串| 31 | |boolean startsWith(String s) |是否以字符串 s 开头| 32 | |boolean endsWith(String s) |是否以字符串 s 结尾| 33 | |int indexOf(String s) |返回首次出现字符串 s 的位置| 34 | |int indexOf(String s, int i) |返回位置 i 之后首次出现字符串 s 的位置| 35 | |int lastIndexOf(String s) |返回最后一次出现字符串 s 的位置| 36 | |String trim() |返回一个新的字符串, 去除了原字符串前后的空白字符| 37 | |String replace(String a, String b) |返回一个新的字符串, 将原字符串中 a 替换为 b| 38 | |int compareTo(String a) |与字符串 a 通过词典序比较大小| 39 | 40 | 41 | 注意, Java 中 String 对象是**不可变的 (Immutable)**. 直观的说, 42 | 一旦创建了一个 String 对象, 就不能修改它. 43 | 比如 `s.toUpperCase()` 并不是将 s 中的每个字符修改为对应的大写字符, 44 | 而是创建一个新的 String 对象, 这个对象中每个字符字符为原来字符的大写. 45 | 46 | ## 实验内容 47 | 48 | 1. 给定一个字符串 (通过命令行参数形式, 以下各题相同), 判断它是否是一个网址 (假设所有网址以"http:" 开头). 49 | 2. 给定一个网址字符串, 根据 "." 将它分解成字串. 例如 http://www.ecnu.edu.cn 分解成为 http://www, ecnu, edu, cn. (提示: 使用 split() 方法) 50 | 3. 给定一个代表文件的字符串, 输出它的扩展名. 例如: a.jpg 输出 jpg. 51 | 4. 给定一个代表 Unix 文件路径的字符串, 输出它的路径名与文件名. 例如 /home/tom/documents/a.jpg, 路径名为 /home/tom/documents/, 文件名为 a.jpg 52 | 5. 从命令行中读入一串字符, 将其中的单引号替换成双引号, 输出到标准输出. 53 | 6. 编写程序 Cat.java, 它可以有任意多的命令行参数, 每个参数为一个文件名. Cat.java 将文件按照参数的顺序合并成一个文件, 并将合并后的文件内容输出到标准输出. 54 | 7. 给定一个字符串, 代表一个16进制数. 将其转换成10进制整数, 输出到标准输出. 55 | 8. 给定一个文件, 其中每行一个词. 输出包含字符最多的词. 如果存在多个这样的词(假设不超过10个), 则将它们都输出. 56 | 9. 给定一个字符串, 将其倒序输出. 要求不使用循环语句. 57 | 10. 给定一个文件, 统计其中26个英文小写字母出现的频率. 58 | 11. 编写程序 Print.java, 它有以下命令行选项, 根据不同的选项得到不同的运行结果. 59 | 60 | |选项|用法举例 |说明| 61 | |---|---|---| 62 | |-t |`java Print -t type` |若 type=n 则输出0到9的数字, type=a 则输出a到z的字母, 默认 type=n (即不带 -t 选项执行 `java Print` 将输出数字)| 63 | |-o |`java Print -o out.txt` |输出到文件out.txt. 默认输出到标准输出| 64 | |-h |`java Print -h` |输出帮助信息到标准输出, 不输出其他信息| 65 | 66 | 例如 `java Print -t a -o a.txt` 将输出 a 到 z 到文件 a.txt. `java Print -o b.txt` 输出数字0到9到 b.txt. `java Print -t a`, 将输出 a 到 z 到标准输出. `java Print -h` 输出的帮助信息为 67 | 68 | ```shell 69 | usage: % java Print [OPTIONS] 70 | -t type if type=n print 0-9, if type=a print a-z. Default: type=n 71 | -o out.txt outputs to out.txt, Default: standard out 72 | -h print this help informantion 73 | ``` 74 | 75 | 除了以上列出的三种选项, 如果输入其他的选项将输出错误信息 "Wrong options", 随后打印帮助信息并退出. 76 | 77 | 12. 给定一个字符串, 判断它能否作为一个安全的密码. 安全密码的要求如下: 78 | * 至少8个字符 79 | * 包含至少1个数字 80 | * 至少包含一个大写字母 81 | * 至少包含一个非字母字符 82 | 83 | 84 | -------------------------------------------------------------------------------- /projects/use_data_types/svg/2dDCnouYkM.svg: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------