用户工具

站点工具


zh:courses:java2025:ch05

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
zh:courses:java2025:ch05 [2025/03/23 20:35]
pzczxs [上机作业]
zh:courses:java2025:ch05 [2025/04/04 11:26] (当前版本)
pzczxs 讨论状态变化了
行 373: 行 373:
 ===== 上机作业 ===== ===== 上机作业 =====
   * 完善Matrix类,补充矩阵转置方法,以及提取特定行与特定列的方法;   * 完善Matrix类,补充矩阵转置方法,以及提取特定行与特定列的方法;
 +【参考答案】
 +<file java Matrix.java>​
 +package cn.edu.bjut.chapter5;​
 +
 +public class Matrix {
 + private double[][] values;
 + private int nrows, ncols;
 +
 + public Matrix(int nrows, int ncols) {
 + this.nrows = nrows;
 + this.ncols = ncols;
 + this.values = new double[nrows][ncols];​
 + }
 +
 + public Matrix(double[][] values) {
 + this.nrows = values.length;​
 + this.ncols = values[0].length;​
 + this.values = values;
 + }
 +
 + public Matrix add(Matrix mat) {
 + if (nrows != mat.nrows || ncols != mat.ncols) {
 + return null;
 + }
 +
 + double[][] newValues = new double[nrows][ncols];​
 + for (int i = 0; i < nrows; i++) {
 + for (int j = 0; j < ncols; j++) {
 + newValues[i][j] = values[i][j] + mat.values[i][j];​
 + }
 + }
 +
 + return (new Matrix(newValues));​
 + }
 +
 + public Matrix substract(Matrix mat) {
 + if (nrows != mat.nrows || ncols != mat.ncols) {
 + return null;
 + }
 +
 + double[][] newValues = new double[nrows][ncols];​
 + for (int i = 0; i < nrows; i++) {
 + for (int j = 0; j < ncols; j++) {
 + newValues[i][j] = values[i][j] - mat.values[i][j];​
 + }
 + }
 +
 + return (new Matrix(newValues));​
 + }
 +
 + public Matrix multiplicate(Matrix mat) {
 + if (ncols != mat.nrows) {
 + return null;
 + }
 +
 + double[][] newValues = new double[nrows][mat.ncols];​
 + for (int i = 0; i < nrows; i++) {
 + for (int j = 0; j < mat.ncols; j++) {
 + double sum = 0;
 + for (int k = 0; k < ncols; k++) {
 + sum += values[i][k] * mat.values[k][j];​
 + }
 + newValues[i][j] = sum;
 + }
 + }
 +
 + return (new Matrix(newValues));​
 + }
 +
 + public Matrix transpose() {
 + double[][] newValues = new double[ncols][nrows];​
 +
 + for (int i = 0; i < nrows; i++) {
 + for (int j = 0; j < ncols; j++) {
 + newValues[j][i] = values[i][j]; ​
 + }
 + }
 +
 + return (new Matrix(newValues)); ​
 + }
 +
 + public Matrix getColumn(int idx) {
 + if (idx < 0 || idx >= ncols) {
 + return null; 
 + }
 +
 + double[][] colVector = new double[nrows][1];​
 + for (int i = 0; i < nrows; i++) {
 + colVector[i][0] = values[i][idx]; ​
 + }
 +
 + return new Matrix(colVector); ​
 + }
 +
 + public Matrix getRow(int idx) {
 + if (idx < 0 || idx >= nrows) {
 + return null; 
 + }
 +
 + double[][] rowVector = new double[1][ncols];​
 + for (int j = 0; j < ncols; j++) {
 + rowVector[0][j] = values[idx][j]; ​
 + }
 +
 + return new Matrix(rowVector); ​
 + }
 +
 + @Override
 + public String toString() {
 + StringBuilder sb = new StringBuilder();​
 + for (int i = 0; i < nrows; i++) {
 + for (int j = 0; j < ncols; j++) {
 + sb.append("​\t"​ + values[i][j]);​
 + }
 + sb.append("​\n"​);​
 + }
 +
 + return sb.toString();​
 + }
 +}
 +</​file>​
   * 写一个类CharCounter,当给定一个字符串时,可以统计其中英文字母及数字出现的频次,并且可以查询特定字符出现在的频次;   * 写一个类CharCounter,当给定一个字符串时,可以统计其中英文字母及数字出现的频次,并且可以查询特定字符出现在的频次;
 【参考答案】 【参考答案】
行 445: 行 566:
 | 14711011 ​              | 2015-05-13 ​             | B32B  17/10, B32B  27/32                 | G06F   ​3/​485 ​                                              | L         | A      | | 14711011 ​              | 2015-05-13 ​             | B32B  17/10, B32B  27/32                 | G06F   ​3/​485 ​                                              | L         | A      |
 | 14515267 ​              | 2014-10-15 ​             | F16F  15/02, G09G   5/34, G06F   ​3/​0485 ​ | A01B  15/06, A01B  15/06                                   | L         | I      | | 14515267 ​              | 2014-10-15 ​             | F16F  15/02, G09G   5/34, G06F   ​3/​0485 ​ | A01B  15/06, A01B  15/06                                   | L         | I      |
 +
 +【参考答案】
 +<file java Classification.java>​
 +package cn.edu.bjut.chapter5;​
 +
 +public class Classification {
 + protected char section;
 + protected int mainClass;
 + protected char subclass;
 + protected int mainGroup;
 + protected int subgroup;
 +
 + public final static char SEPARATOR = '/';​
 +
 + public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
 + this.section = section;
 + this.mainClass = mainClass;
 + this.subclass = subclass;
 + this.mainGroup = mainGroup;
 + this.subgroup = subgroup;
 + }
 +
 + // example: G09F 17/30
 + public Classification(String symbol) {
 + this.section = symbol.charAt(0);​
 + this.mainClass = Integer.parseInt(symbol.substring(1,​ 3));
 + this.subclass = symbol.charAt(3);​
 +
 + int pos = symbol.indexOf(SEPARATOR);​
 + this.mainGroup = Integer.parseInt(symbol.substring(4,​ pos).trim());​
 + this.subgroup = Integer.parseInt(symbol.substring(pos + 1));
 + }
 +
 + public char getSection() {
 + return section;
 + }
 +
 + public void setSection(char section) {
 + this.section = section;
 + }
 +
 + public int getMainClass() {
 + return mainClass;
 + }
 +
 + public void setMainClass(int mainClass) {
 + this.mainClass = mainClass;
 + }
 +
 + public char getSubclass() {
 + return subclass;
 + }
 +
 + public void setSubclass(char subclass) {
 + this.subclass = subclass;
 + }
 +
 + public int getMainGroup() {
 + return mainGroup;
 + }
 +
 + public void setMainGroup(int mainGroup) {
 + this.mainGroup = mainGroup;
 + }
 +
 + public int getSubgroup() {
 + return subgroup;
 + }
 +
 + public void setSubgroup(int subgroup) {
 + this.subgroup = subgroup;
 + }
 +
 + @Override
 + public String toString() {
 + StringBuilder sb = new StringBuilder();​
 +
 + sb.append(section);​
 +
 + sb.append(String.format("​%2s",​ mainClass).replace('​ ', '​0'​));​
 +
 + sb.append(subclass);​
 +
 + sb.append(String.format("​%4s",​ mainGroup));​
 +
 + sb.append(SEPARATOR);​
 +
 + sb.append(subgroup);​
 +
 + return sb.toString();​
 + }
 +}
 +</​file>​
 +
 +<file java Ipc.java>​
 +package cn.edu.bjut.chapter5;​
 +
 +public class Ipc extends Classification {
 +
 + public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
 + super(section,​ mainClass, subclass, mainGroup, subgroup); ​
 + }
 +
 + public Ipc(String symbol) {
 + super(symbol); ​
 + }
 +
 + public static void main(String[] args) {
 + char section = '​A'; ​
 + int mainClass = 4; 
 + char subclass = '​B'; ​
 + int mainGroup = 55; 
 + int subgroup = 56; 
 +
 + Classification ipc = new Ipc(section,​ mainClass, subclass, mainGroup, subgroup); ​
 + System.out.println(ipc); ​
 +
 + String symbol = "G09F 17/​30"; ​
 + System.out.println(new Ipc(symbol)); ​
 + }
 +}
 +</​file>​
 +
 +<file java Cpc.java>​
 +package cn.edu.bjut.chapter5;​
 +
 +public class Cpc extends Classification {
 + private char position; ​
 + private char value; ​
 +
 + public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) {
 + super(section,​ mainClass, subclass, mainGroup, subgroup); ​
 +
 + this.position = position; ​
 + this.value = value; ​
 + }
 +
 + public Cpc(String symbol, char position, char value) {
 + super(symbol); ​
 +
 + this.position = position; ​
 + this.value = value; ​
 + }
 +
 + public char getPosition() {
 + return position;
 + }
 +
 + public void setPosition(char position) {
 + this.position = position;
 + }
 +
 + public char getValue() {
 + return value;
 + }
 +
 + public void setValue(char value) {
 + this.value = value;
 + }
 +}
 +</​file>​
 +
 +<file java Date.java>​
 +package cn.edu.bjut.chapter5;​
 +
 +public class Date {
 + private int year, month, day;
 +
 + public Date(int year, int month, int day) {
 + this.year = year;
 + this.month = month;
 + this.day = day;
 + }
 +
 + // format: YYYY-MM-DD
 + public Date(String str) {
 + String[] parts = str.trim().split("​-"​); ​
 + this.year = Integer.parseInt(parts[0]); ​
 + this.month = Integer.parseInt(parts[1]); ​
 + this.day = Integer.parseInt(parts[2]); ​
 + }
 +
 + public int getYear() {
 + return year;
 + }
 +
 + public void setYear(int year) {
 + this.year = year;
 + }
 +
 + public int getMonth() {
 + return month;
 + }
 +
 + public void setMonth(int month) {
 + this.month = month;
 + }
 +
 + public int getDay() {
 + return day;
 + }
 +
 + public void setDay(int day) {
 + this.day = day;
 + }
 +
 + @Override
 + public String toString() {
 + StringBuilder sb = new StringBuilder();​
 +
 + sb.append(year);​
 + if (month < 10) {
 + sb.append("​0"​ + month);
 + } else {
 + sb.append(month);​
 + }
 + if (day < 10) {
 + sb.append("​0"​ + day);
 + } else {
 + sb.append(day);​
 + }
 +
 + return sb.toString();​
 + }
 +}
 +</​file>​
 +
 +<file java Patent.java>​
 +package cn.edu.bjut.chapter5;​
 +
 +public class Patent {
 + private String applicationNo; ​
 + private Date applicationDate; ​
 + private Ipc[] ipcs; 
 + private Cpc[] cpcs; 
 +
 + private final static String SEPERATOR = ","; ​
 +
 + public Patent(String applicationNo,​ Date applicationDate,​ Ipc[] ipcs, Cpc[] cpcs) {
 + this.applicationNo = applicationNo; ​
 + this.applicationDate = applicationDate; ​
 + this.ipcs = ipcs; 
 + this.cpcs = cpcs; 
 + }
 +
 + public Patent(String applicationNo,​ String applicationDate,​ String multiIpcs, String multiCpcs, char position, char value) {
 + this.applicationNo = applicationNo; ​
 + this.applicationDate = new Date(applicationDate); ​
 +
 + String[] ipcSymbols = multiIpcs.split(SEPERATOR); ​
 + this.ipcs = new Ipc[ipcSymbols.length]; ​
 + for (int i = 0; i < ipcSymbols.length;​ i++) {
 + ipcs[i] = new Ipc(ipcSymbols[i].trim()); ​
 + }
 +
 + String[] cpcSymbols = multiCpcs.split(SEPERATOR); ​
 + this.cpcs = new Cpc[cpcSymbols.length]; ​
 + for (int i = 0; i < cpcSymbols.length;​ i++) {
 + cpcs[i] = new Cpc(cpcSymbols[i].trim(),​ position, value); ​
 + }
 + }
 +
 + public String getApplicationNo() {
 + return applicationNo;​
 + }
 +
 + public void setApplicationNo(String applicationNo) {
 + this.applicationNo = applicationNo;​
 + }
 +
 + public Date getApplicationDate() {
 + return applicationDate;​
 + }
 +
 + public void setApplicationDate(Date applicationDate) {
 + this.applicationDate = applicationDate;​
 + }
 +
 + public Ipc[] getIpcs() {
 + return ipcs;
 + }
 +
 + public void setIpcs(Ipc[] ipcs) {
 + this.ipcs = ipcs;
 + }
 +
 + public Cpc[] getCpcs() {
 + return cpcs;
 + }
 +
 + public void setCpcs(Cpc[] cpcs) {
 + this.cpcs = cpcs;
 + }
 +
 + @Override
 + public String toString() {
 + StringBuilder sb = new StringBuilder(); ​
 +
 + sb.append(applicationNo + "​\t"​ + applicationDate + "​\t"​); ​
 +
 + sb.append("​["​); ​
 + for (int i = 0; i < ipcs.length - 1; i++) {
 + sb.append(ipcs[i] + SEPERATOR); ​
 + }
 + sb.append(ipcs[ipcs.length - 1] + "​]\t"​); ​
 +
 + sb.append("​["​); ​
 + for (int i = 0; i < cpcs.length - 1; i++) {
 + sb.append(cpcs[i] + SEPERATOR); ​
 + }
 + sb.append(cpcs[cpcs.length - 1] + "​]"​); ​
 +
 + return sb.toString(); ​
 + }
 +
 + public static void main(String[] args) {
 + Patent[] patents = new Patent[5]; ​
 + patents[0] = new Patent("​14725838",​ "​2015-05-29",​ "A23B 5/04, A23B 5/​045",​ "A23B 5/04, A23B 5/45, A23L 15/​20",​ '​F',​ '​I'​); ​
 + patents[1] = new Patent("​14814205",​ "​2015-07-30",​ "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/​6042",​ '​F',​ '​I'​); ​
 + patents[2] = new Patent("​15189723",​ "​2016-06-22",​ "D04B 1/00", "​B65H2301/​44514,​ C01B 33/1071, F16F 15/2, H01L 27/​14641",​ '​L',​ '​A'​); ​
 + patents[3] = new Patent("​14711011",​ "​2015-05-13",​ "B32B 17/10, B32B 27/​32",​ "G06F 3/​485",​ '​L',​ '​A'​); ​
 + patents[4] = new Patent("​14515267",​ "​2014-10-15",​ "F16F 15/02, G09G 5/34, G06F 3/​0485",​ "A01B 15/06, A01B 15/​06",​ '​L',​ '​I'​); ​
 +
 + for (Patent patent: patents) {
 + System.out.println(patent); ​
 + }
 + }
 +}
 +</​file>​
  
 [[zh:​courses:​java2025:​index|返回Java课程页]] [[zh:​courses:​java2025:​index|返回Java课程页]]
  
-~~DISCUSSION~~+~~DISCUSSION:closed~~
zh/courses/java2025/ch05.1742733314.txt.gz · 最后更改: 2025/03/23 20:35 由 pzczxs