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现在时间是:); Date now = new Date(); System.out.print(sdf.format(now); try slee
2、p(1000); catch (InterruptedException e) e.printStackTrace(); public class Clock public static void main(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
3、 javax.swing.JFrame;public class MagicButton extends 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); win.setSize(400, 300); win.se
4、tResizable(false); win.setLocationRelativeTo(null); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); public static void main(String args) MagicButton demo = new MagicButton(); demo.initUI(); public void mouseEntered(MouseEvent e) int mouseX = button.getX() + e.getX(); int mo
5、useY = button.getY() + e.getY(); while (true) int buttonX = rand.nextInt(win.getWidth() - button.getWidth(); int buttonY = rand.nextInt(win.getHeight() - button.getHeight(); button.setLocation(buttonX, buttonY); if (!button.getBounds().contains(mouseX, mouseY) break; 第二章实验一/*2. 交换两个变量的值(不允许使用中间变量)。
6、*/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);实验二/*3. 逆序输出一个7位整数,如8639427输出为7249368(不允许使用循环语句)。 */package ch03;public class Exp2_3 public static void main(String args) long a = 8639427;
7、System.out.print(a % 10);System.out.print(a / 10 % 10);System.out.print(a / 100 % 10);System.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,以最快的速度计算34a的值。 */package ch03;public class Exp2_4 public stati
8、c void main(String args) int a = 3;int b = (a 5) + (a b ? a : b) c ? (a b ? a : b) : c;System.out.println(max= + max);第三章实验一/*2. 使用循环结构逆序输出任意位数的整数。 */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(输
9、入整数:);long n = s.nextLong();while (n 0) System.out.print(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 ch04;import java.util.Scanner;public class Exp3_3 public static void main(String args) int rows;Scanner s = new Scanner(Syst
10、em.in);System.out.print(输入行数:);rows = s.nextInt();for (int i = -rows / 2; i = rows / 2; i+) System.out.printf(%- + (3 * Math.abs(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();实
11、验三/*4. 输出以上由数字组成的三角形(要求将输出行数存放于变量中以便随时更改)。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);System.out.print(输入行数:);rows = s.nextInt();int firstNumOfRow =
12、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(%-4d, nextNumOfRow);nextNumOfRow += firstStepOfRow+;System.out.println();实验四/*5. 计算多项式8+88+888+8888+88888+. 的前8项之和。输出结果
13、:98765424 */package ch04;public class Exp3_5 public static void 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);第四章实验一/*1. 产生10个100以内的随机整数以填充一维数组,实现以下功能。 找出最大以及最小值。 查找给定整数a在数组中最后一次出现的位置,若不存在则提示。 判
14、断数组是否呈非递减排列。 将数组元素翻转存放。 */package ch05;import java.util.Random;import java.util.Scanner;public class Exp4_1 int init() int a = new int10;Random r = new Random();for (int i = 0; i a.length; i+) ai = r.nextInt(100);return a;void print(int a) for (int i = 0; i a.length; i+) System.out.printf(%-5d, ai);
15、System.out.println();int findMax(int a) int max = a0;for (int i = 1; i a.length; i+) if (max ai) max = ai;return max;int findMin(int a) int min = a0;for (int i = 1; i ai) min = ai;return min;int findLastLocation(int a, int x) for (int i = a.length - 1; i = 0; i-) if (ai = x) return i;return -1;boole
16、an isAsc(int a) for (int i = 0; i ai + 1) return false;return true;void reverse(int a) for (int i = 0; i a.length / 2; i+) int temp = ai;ai = aa.length - i - 1;aa.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= +
17、t.findMax(a);System.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);if (t.isAsc(a) System.out.print
18、ln(数组是非递减排列!); else System.out.println(数组不是非递减排列!);t.reverse(a);System.out.println(翻转后的数组:);t.print(a);实验二/*2. 将a插入到一个长度不小于10且元素呈递增排列的一维数组中,并保证插入之后的数组依然递增(若a在插入前的数组中存在,则输出提示并忽略)。 */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
19、;void print(boolean isAfterInsert) int end = isAfterInsert ? a.length : a.length - 1;for (int i = 0; i end; i+) System.out.printf(%-5d, ai);System.out.println();int findInsertLocation(int x) int i = 0;for (; i x) return i;return i;void insert(int i, int x) for (int j = a.length - 2; j = i; j-) aj +
20、1 = aj;ai = 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 = t.findInsertLocation(x);if (i = -1) System.out.println(x + 在数组中已经存在,放弃插入!); else t.insert(i, x);t.print(true);实验三/*3. 找出
21、阶数不小于8的方阵的鞍点值及位置(鞍点值在该行上最大、该列上最小),若无鞍点则提示。 */package ch05;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, in
22、t value) this.row = row;this.col = col;this.value = value;/ getters and setterspublic 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 int getValue() return value;public void setValue(int value) this.v
23、alue = value;/* * 测试类(整体上是若干个并列的2重循环,时间复杂度较3重循环低) */public class Exp4_3 int a; / 矩阵int maxOfRows; / 存放每行的最大值int minOfCols; / 存放每列的最小值final int LIMIT = 3; / 矩阵元素值的上限(为测试方便此处写死,也可在运行时由用户输入)/ 初始化矩阵void initArray() Scanner scanner = new Scanner(System.in);System.out.print(输入矩阵行数:);int m = scanner.nextIn
24、t(); / 矩阵行数System.out.print(输入矩阵列数:);int n = scanner.nextInt(); / 矩阵列数/ 构造各数组a = new intmn;maxOfRows = new intm;minOfCols = new intn;/ 以随机数填充矩阵Random random = new Random();for (int i = 0; i a.length; i+) for (int j = 0; j ai.length; j+) aij = random.nextInt(LIMIT);/ 记录每行的最大值int max;for (int i = 0; i
25、 a.length; i+) max = ai0;for (int j = 1; j ai.length; j+) if (max aij) max = aij;maxOfRowsi = max;/ 记录每列的最小值int min;for (int j = 0; j a0.length; j+) min = a0j;for (int i = 1; i aij) min = aij;minOfColsj = min;/ 打印矩阵void printArray() System.out.println(得到的矩阵为:);for (int i = 0; i a.length; i+) for (in
26、t j = 0; j ai.length; j+) System.out.printf(%-8d, aij);System.out.println();/ 找鞍点(可能有多个),返回类型为线性表类型(List),尖括号语法为泛型,表示线性表的元素为AnDian对象,具体看教材List findAnDian() List anDians = new ArrayList(); / 构造线性表对象for (int i = 0; i a.length; i+) / 扫描矩阵中的每个元素for (int j = 0; j ai.length; j+) / 是当前行最大且当前列最小if (aij = ma
27、xOfRowsi & aij = minOfColsj) AnDian p = new AnDian(i, j, aij); / 构造AnDian对象anDians.add(p); / 加入AnDian对象到线性表return anDians; / 返回线性表/ 测试入口public static void main(String args) Exp4_3 o = new Exp4_3();o.initArray();o.printArray();List anDians = o.findAnDian();System.out.println(-);if (anDians.size() = 0
28、) / 返回的线性表元素个数为0System.out.println(没有鞍点。); else int i = 0;for (AnDian e : anDians) / 迭代性for循环的语法也可用于线性表类型System.out.printf(鞍点%-4d:a%-3d%-3d = %-8dn, +i, e.getRow(), e.getCol(), e.getValue();实验四/*4. 编写如下图所示的程序以模拟命令行的copy命令。 */package ch05;public class Exp4_4 public static void main(String args) if (a
29、rgs.length != 2) System.err.println(命令语法不正确,使用格式为:java Exp4_4 要复制的文件 复制到的路径);return;System.out.println(成功将 + args0 + 复制到 + args1 + 。);实验五/*5. 输出如上图所示的循环移位方阵(第一行存于一维数组,循环右移该行元素一个位置以产生下一行,以此类推)。7 4 8 9 1 5 5 7 4 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 v
30、oid shift(int a) int last = aa.length - 1;for (int i = a.length - 2; i = 0; i-) ai + 1 = ai;a0 = last;void print(int a) for (int i = 0; i a.length; i+) System.out.printf(%-5d, ai);System.out.println();public static void main(String args) Exp4_5 t = new Exp4_5();int a = 7, 4, 8, 9, 1, 5 ;t.print(a);f
31、or (int i = 0; i a.length - 1; i+) t.shift(a);t.print(a);第五章实验一/*2. 在例6.17的基础上,将draw方法改为计算形状自身面积的calcArea方法并在测试类中测试。提示:可根据需要在Shape的每个子类中增加用以计算面积所必须的字段,如为Square类增加边长字段、Circle类增加半径字段等,然后编写含相应字段的构造方法以构造具体的形状对象。输出结果:s1的面积:4.0s2的面积:6.0s3的面积:12.566370614359172 */package ch06;class Shape public double calc
32、Area() return 0;class Square extends Shape float width;public Square(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 + 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