1、第一章 实验一 package ch01; import java.text.SimpleDateFormat; import java.util.Date; class Timer extends Thread { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); public void run() { while (true) { System.out.print("\r现在时间是:");
2、 Date now = new Date(); System.out.print(sdf.format(now)); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Clock { public static void main(
3、String[] args) { Timer timer = new Timer(); timer.start(); } } 实验二 package ch01; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; public class MagicButton extends
4、MouseAdapter { JFrame win; JButton button = new JButton("你点不到我"); Random rand = new Random(); void initUI() { win = new JFrame(); win.setLayout(null); button.setSize(100, 40); button.addMouseListener(this); win.add(button);
5、 win.setSize(400, 300); win.setResizable(false); win.setLocationRelativeTo(null); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } public static void main(String[] args) { MagicButton demo = new MagicButton();
6、 demo.initUI(); } public void mouseEntered(MouseEvent e) { int mouseX = button.getX() + e.getX(); int mouseY = button.getY() + e.getY(); while (true) { int buttonX = rand.nextInt(win.getWidth() - button.getWidth()); int buttonY
7、 rand.nextInt(win.getHeight() - button.getHeight()); button.setLocation(buttonX, buttonY); if (!button.getBounds().contains(mouseX, mouseY)) { break; } } } } 第二章 实验一 /********************************* 2.
8、 交换两个变量的值(不允许使用中间变量)。 **********************************/ package ch03; public class Exp2_2 { public static void main(String[] args) { int a = 2, b = 3; int s = a * b; a = s / a; b = s / a; System.out.println("a=" + a + ", b=" + b); } } 实验二 /******************************
9、 3. 逆序输出一个7位整数,如8639427输出为7249368(不允许使用循环语句)。 **********************************/ package ch03; public class Exp2_3 { public static void main(String[] args) { long a = 8639427; System.out.print(a % 10); System.out.print(a / 10 % 10); System.out.print(a / 100 % 10); System
10、out.print(a / 1000 % 10); System.out.print(a / 10000 % 10); System.out.print(a / 100000 % 10); System.out.print(a / 1000000 % 10); } } 实验三 /********************************* 4. 对于int型变量a,以最快的速度计算34×a的值。 **********************************/ package ch03; public class Exp2_4 {
11、public static void main(String[] args) { int a = 3; int b = (a << 5) + (a << 1); System.out.println(a + "*34=" + b); } } 实验四 /********************************* 5. 字符型变量ch中存放着一个大小写未知的英文字母,判断其大小写后,将ch的值转为小写或大写字母(不允许使用加减运算符和if语句)。 **********************************/ package ch03;
12、public class Exp2_5 { public static void main(String[] args) { char ch = 'E'; ch = (char) ((ch & 32) == 0 ? ch | 32 : ch & (Integer.MAX_VALUE - 32)); System.out.println("ch1=" + ch); } } 实验5 /********************************* 6. 使用嵌套的条件运算符,求a、b、c中的最大者。 ***************************
13、/ package ch03; public class Exp2_6 { public static void main(String[] args) { int a = 2, b = 4, c = 3; int max = (a > b ? a : b) > c ? (a > b ? a : b) : c; System.out.println("max=" + max); } } 第三章 实验一 /********************************* 2. 使用
14、循环结构逆序输出任意位数的整数。 **********************************/ package ch04; import java.util.Scanner; public class Exp3_2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("输入整数:"); long n = s.nextLong(); while (n > 0) { System.out.pr
15、int(n % 10); n /= 10; } } } 实验二 /********************************* 3. 输出以下由数字组成的菱形(要求将输出行数存放于变量中以便随时更改)。 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1 **********************************/ package
16、ch04; import java.util.Scanner; public class Exp3_3 { public static void main(String[] args) { int rows; Scanner s = new Scanner(System.in); System.out.print("输入行数:"); rows = s.nextInt(); for (int i = -rows / 2; i <= rows / 2; i++) { System.out.printf("%-" + (3 * Math.ab
17、s(i) + 1) + "s", ""); for (int j = Math.abs(i) - rows / 2; j <= rows / 2 - Math.abs(i); j++) { System.out.printf("%-3d", rows / 2 + 1 - Math.abs(i) - Math.abs(j)); } System.out.println(); } } } 实验三 /********************************* 4. 输出以上由数字组成的三角形(要求将输出行数存放于变量中以便随时更改)。
18、 1 3 6 10 15 21 2 5 9 14 20 4 8 13 19 7 12 18 11 17 16 **********************************/ package ch04; import java.util.Scanner; public class Exp3_4 { public static void main(String[] args) { int rows; Scanner s = new Scanner(System.in);
19、 System.out.print("输入行数:"); rows = s.nextInt(); int firstNumOfRow = 1, nextNumOfRow; for (int i = 1; i <= rows; i++) { firstNumOfRow += i - 1; int firstStepOfRow = i + 1; nextNumOfRow = firstNumOfRow; for (int j = 1; j <= rows + 1 - i; j++) { System.out.printf("%-4
20、d", nextNumOfRow); nextNumOfRow += firstStepOfRow++; } System.out.println(); } } } 实验四 /********************************* 5. 计算多项式8+88+888+8888+88888+... 的前8项之和。 输出结果: 98765424 **********************************/ package ch04; public class Exp3_5 { public static vo
21、id main(String[] args) { long sum = 0; for (int i = 1; i <= 8; i++) { long num = 0; for (int j = 1; j <= i; j++) { num = num * 10 + 8; } sum += num; } System.out.println(sum); } } 第四章 实验一 /****************************
22、 1. 产生10个100以内的随机整数以填充一维数组,实现以下功能。 ① 找出最大以及最小值。 ② 查找给定整数a在数组中最后一次出现的位置,若不存在则提示。 ③ 判断数组是否呈非递减排列。 ④ 将数组元素翻转存放。 **********************************/ package ch05; import java.util.Random; import java.util.Scanner; public class Exp4_1 { int[] init() { int[] a = new int[10]; R
23、andom r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(100); } return a; } void print(int[] a) { for (int i = 0; i < a.length; i++) { System.out.printf("%-5d", a[i]); } System.out.println(); } int findMax(int[] a) { int max = a[0]; f
24、or (int i = 1; i < a.length; i++) { if (max < a[i]) { max = a[i]; } } return max; } int findMin(int[] a) { int min = a[0]; for (int i = 1; i < a.length; i++) { if (min > a[i]) { min = a[i]; } } return min; } int findLastLocation(int[] a, int x)
25、 { for (int i = a.length - 1; i >= 0; i--) { if (a[i] == x) { return i; } } return -1; } boolean isAsc(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } void reverse(int[] a) { for (i
26、nt i = 0; i < a.length / 2; i++) { int temp = a[i]; a[i] = a[a.length - i - 1]; a[a.length - i - 1] = temp; } } public static void main(String[] args) { Exp4_1 t = new Exp4_1(); int[] a = t.init(); t.print(a); System.out.println("max=" + t.findMax(a)); System.
27、out.println("min=" + t.findMin(a)); System.out.print("输入要查找的数:"); Scanner s = new Scanner(System.in); int x = s.nextInt(); int i = t.findLastLocation(a, x); if (i == -1) { System.out.println(x + " 在数组中不存在。"); } else { System.out.printf("Last location of %d: %d。\n", x, i);
28、 } if (t.isAsc(a)) { System.out.println("数组是非递减排列!"); } else { System.out.println("数组不是非递减排列!"); } t.reverse(a); System.out.println("翻转后的数组:"); t.print(a); } } 实验二 /********************************* 2. 将a插入到一个长度不小于10且元素呈递增排列的一维数组中,并保证插入之后的数组依然递增(若a在插入前的数组中存在,则输出
29、提示并忽略)。 **********************************/ package ch05; import java.util.Scanner; public class Exp4_2 { int a[] = { 2, 4, 5, 7, 9, 11, 15, 17, 20, 22, Integer.MAX_VALUE }; void print(boolean isAfterInsert) { int end = isAfterInsert ? a.length : a.length - 1; for (int i = 0; i
30、< end; i++) { System.out.printf("%-5d", a[i]); } System.out.println(); } int findInsertLocation(int x) { int i = 0; for (; i < a.length - 1; i++) { if (a[i] == x) { return -1; } else if (a[i] > x) { return i; } } return i; } void insert(int i,
31、int x) { for (int j = a.length - 2; j >= i; j--) { a[j + 1] = a[j]; } a[i] = x; } public static void main(String[] args) { Exp4_2 t = new Exp4_2(); t.print(false); System.out.print("输入要插入的数:"); Scanner s = new Scanner(System.in); int x = s.nextInt(); int i =
32、t.findInsertLocation(x); if (i == -1) { System.out.println(x + " 在数组中已经存在,放弃插入!"); } else { t.insert(i, x); t.print(true); } } } 实验三 /********************************* 3. 找出阶数不小于8的方阵的鞍点值及位置(鞍点值在该行上最大、该列上最小),若无鞍点则提示。 **********************************/ package ch05;
33、 import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; /** * 鞍点对象类 */ class AnDian { private int row; // 鞍点所在行下标 private int col; // 鞍点所在列下标 private int value; // 鞍点值 // 完全构造方法 public AnDian(int row, int col, int value) { thi
34、s.row = row; this.col = col; this.value = value; } // getters and setters public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getCol() { return col; } public void setCol(int col) { this.col = col; } public i
35、nt getValue() { return value; } public void setValue(int value) { this.value = value; } } /** * 测试类(整体上是若干个并列的2重循环,时间复杂度较3重循环低) */ public class Exp4_3 { int[][] a; // 矩阵 int maxOfRows[]; // 存放每行的最大值 int minOfCols[]; // 存放每列的最小值 final int LIMIT = 3; // 矩阵元素值的上限(为测试方便此处写
36、死,也可在运行时由用户输入) // 初始化矩阵 void initArray() { Scanner scanner = new Scanner(System.in); System.out.print("输入矩阵行数:"); int m = scanner.nextInt(); // 矩阵行数 System.out.print("输入矩阵列数:"); int n = scanner.nextInt(); // 矩阵列数 // 构造各数组 a = new int[m][n]; maxOfRows = new int[m];
37、minOfCols = new int[n]; // 以随机数填充矩阵 Random random = new Random(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = random.nextInt(LIMIT); } } // 记录每行的最大值 int max; for (int i = 0; i < a.length; i++) { max = a[i][0]; f
38、or (int j = 1; j < a[i].length; j++) { if (max < a[i][j]) { max = a[i][j]; } } maxOfRows[i] = max; } // 记录每列的最小值 int min; for (int j = 0; j < a[0].length; j++) { min = a[0][j]; for (int i = 1; i < a.length; i++) { if (min > a[i][j]) { min = a[
39、i][j]; } } minOfCols[j] = min; } } // 打印矩阵 void printArray() { System.out.println("得到的矩阵为:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%-8d", a[i][j]); } System.out.println(); } } // 找鞍点(可能
40、有多个),返回类型为线性表类型(List),尖括号语法为泛型,表示线性表的元素为AnDian对象,具体看教材
List
41、 a[i][j] == minOfCols[j]) { AnDian p = new AnDian(i, j, a[i][j]); // 构造AnDian对象 anDians.add(p); // 加入AnDian对象到线性表 } } } return anDians; // 返回线性表 } // 测试入口 public static void main(String[] args) { Exp4_3 o = new Exp4_3(); o.initArray(); o.printArray();
42、List
43、8d\n", ++i, e.getRow(), e.getCol(), e.getValue()); } } } } 实验四 /********************************* 4. 编写如下图所示的程序以模拟命令行的copy命令。 **********************************/ package ch05; public class Exp4_4 { public static void main(String[] args) { if (args.length != 2) { System
44、err.println("命令语法不正确,使用格式为:java Exp4_4 要复制的文件 复制到的路径"); return; } System.out.println("成功将\"" + args[0] + "\" 复制到 \"" + args[1] + "\"。"); } } 实验五/********************************* 5. 输出如上图所示的循环移位方阵(第一行存于一维数组,循环右移该行元素一个位置以产生下一行,以此类推)。 7 4 8 9 1 5 5 7 4
45、 8 9 1 1 5 7 4 8 9 9 1 5 7 4 8 8 9 1 5 7 4 4 8 9 1 5 7 **********************************/ package ch05; public class Exp4_5 { void shift(int[] a) { int last = a[a.length - 1]; for (int i = a.leng
46、th - 2; i >= 0; i--) { a[i + 1] = a[i]; } a[0] = last; } void print(int[] a) { for (int i = 0; i < a.length; i++) { System.out.printf("%-5d", a[i]); } System.out.println(); } public static void main(String[] args) { Exp4_5 t = new Exp4_5(); int[] a = { 7, 4,
47、8, 9, 1, 5 }; t.print(a); for (int i = 0; i < a.length - 1; i++) { t.shift(a); t.print(a); } } } 第五章 实验一 /********************************* 2. 在例6.17的基础上,将draw方法改为计算形状自身面积的calcArea方法并在测试类中测试。 提示:可根据需要在Shape的每个子类中增加用以计算面积所必须的字段
48、如为Square类增加边长 字段、Circle类增加半径字段等,然后编写含相应字段的构造方法以构造具体的形状对象。 输出结果: s1的面积:4.0 s2的面积:6.0 s3的面积:12.566370614359172 **********************************/ package ch06; class Shape { public double calcArea() { return 0; } } class Square extends Shape { float width; public Square
49、int width) { this.width = width; } public double calcArea() { return width * width; } } class Triangle extends Shape { float a, b, c; public Triangle(float a, float b, float c) { this.a = a; this.b = b; this.c = c; } public double calcArea() { float s = (a
50、 b + c) / 2; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } } class Circle extends Shape { float radius; public Circle(float radius) { this.radius = radius; } public double calcArea() { return Math.PI * radius * radius; } } public class Exp5_2 { public stati






