版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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.
2、print(sdf.format(now); try sleep(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;impo
3、rt javax.swing.JButton; import 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
4、(button); 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(); demo.initUI();public void mouseEntered(MouseEvent e) int mous
5、eX = button.getX() + e.getX(); int mouseY = 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) brea
6、k;第二章 实验一/*2. 交换两个变量的值(不允许使用中间变量) 。*/ 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 cl
7、ass 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.out.print(a / 1000 % 10); System.out.print(a / 10000 % 10); System.out.print(a / 100000 % 10); System.out.print(a / 1000000 % 10); 实验三 /*4
8、. 对于int型变量a,以最快的速度计算 34 x a的值。 */package ch03;public class Exp2_4 public static void main(String args) int a = 3;int b = (a << 5) + (a << 1);System.out.println(a + "*34=" + b);实验四/*ch 的值转为5. 字符型变量 ch 中存放着一个大小写未知的英文字母,判断其大小写后,将 小写或大写字母(不允许使用加减运算符和 if 语句)。*/ package ch03;public c
9、lass Exp2_5 public static void main(String args) char ch = 'E'ch = (char) (ch & 32) = 0 ? ch | 32 : ch & (Integer.MAX_V ALUE - 32); System.out.println("ch1=" + ch); 实验 5 /*6. 使用嵌套的条件运算符,求 a、b、c 中的最大者。 */package ch03;public class Exp2_6 public static void main(String args) i
10、nt a = 2, b = 4, c = 3;int max = (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.o
11、ut.println(" 输入整数: "); long n = s.nextLong();while (n > 0) System.out.print(n % 10);n /= 10; 实验二/*3. 输出以下由数字组成的菱形(要求将输出行数存放于变量中以便随时更改)12*/ package ch04;import java.util.Scanner;public class Exp3_3 public static void main(String args) int rows;Scanner s = new Scanner(System.in); System.o
12、ut.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 /
13、2 + 1 - Math.abs(i) - Math.abs(j);System.out.println(); 实验三/*4. 124输出以上由数字组成的三角形(要求将输出行数存放于变量中以便随时更改)3581269131810 15 2114 2019171116*/ 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("
14、输入行数 :"); 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("%-4d", nextNumOfRow); nextNumOfRow += first
15、StepOfRow+;System.out.println(); 实验四/*5. 计算多项式 8+88+888+8888+88888+. 的前 8 项之和。 输出结果: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.print
16、ln(sum);第四章 实验一/*1.产生 10个 100 以内的随机整数以填充一维数组,实现以下功能。 找出最大以及最小值。查找给定整数 a 在数组中最后一次出现的位置,若不存在则提示。 判断数组是否呈非递减排列。将数组元素翻转存放。*/ 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 =
17、r.nextInt(100); return a;void print(int a) for (int i = 0; i < a.length; i+) System.out.printf("%-5d", ai);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 <
18、; a.length; i+) if (min > 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;boolean isAsc(int a) for (int i = 0; i < a.length - 1; i+) if (ai > ai + 1) return false;return true;void reverse(int a) for (int i
19、 = 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=" + t.findMax(a); System.out.println("min=" + t.findMin(a); System.out.
20、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.println(" 数组是非递减排列!
21、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, 1
22、7, 20, 22, Integer.MAX_V ALUE ;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 < a.length - 1; i+) if (ai = X) return
23、-1; else if (ai > X) return i;return i;void insert(int i, int x) for (int j = a.length - 2; j >= i; j-) aj + 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
24、();int i = t.findInsertLocation(x); if (i = -1) System.out.println(x + " 在数组中已经存在,放弃插入! "); else t.insert(i, x); t.print(true); 实验三/*,若无3. 找出阶数不小于 8 的方阵的鞍点值及位置(鞍点值在该行上最大、该列上最小) 鞍点则提示。*/ package ch05;import java.util.ArrayList;import java.util.List;import java.util.Random; import java.util.
25、Scanner;/* 鞍点对象类*/class AnDian private int row; / 鞍点所在行下标 private int col; / 鞍点所在列下标 private int value; / 鞍点值/ 完全构造方法public AnDian(int row, int col, int value) this.row = row;this.col = col; this.value = value;/ getters and setters public int getRow() return row;public void setRow(int row) this.row
26、= 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.value = value;/* 测试类(整体上是若干个并列的 2 重循环,时间复杂度较 3 重循环低) */public class Exp4_3 int a; / 矩阵int maxOfRows; / 存放每行的最大值int minOfCols; / 存放每列的最小值final int LI
27、MIT = 3; / 矩阵元素值的上限(为测试方便此处写死,也可在运行时由用户 输入)/ 初始化矩阵void initArray() Scanner scanner = new Scanner(System.in);System.out.print(" 输入矩阵行数 :"); int m = scanner.nextInt(); / 矩阵行数 System.out.print(" 输入矩阵列数 :"); int n = scanner.nextInt(); / 矩阵列数/ 构造各数组 a = new intmn; maxOfRows = new intm
28、; 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 < a.length; i+) max = ai0;for (int j = 1; j < ai.length; j+) if (max < aij) max = aij;maxO
29、fRowsi = max;/ 记录每列的最小值int min;for (int j = 0; j < a0.length; j+) min = a0j;for (int i = 1; i < a.length; i+) if (min > aij) min = aij;minOfColsj = min;/ 打印矩阵 void printArray() System.out.println(" 得到的矩阵为: "); for (int i = 0; i < a.length; i+) for (int j = 0; j < ai.length;
30、j+) System.out.printf("%-8d", aij);System.out.println();/ 找鞍点(可能有多个) ,返回类型为线性表类型( List ),尖括号语法为泛型,表示线 性表的元素为 AnDian 对象,具体看教材List<AnDian> findAnDian() List<AnDian> anDians = new ArrayList<AnDian>(); /构造线性表对象for (int i = 0; i < a.length; i+) / 扫描矩阵中的每个元素for (int j = 0; j
31、 < ai.length; j+) / 是当前行最大且当前列最小 if (aij = maxOfRowsi && 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<An
32、Dian> anDians = o.findAnDian();System.out.println("if (anDians.size() = 0) / 返回的线性表元素个数为 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
33、(); 实验四/*4. 编写如下图所示的程序以模拟命令行的 copy 命令。*/ package ch05;public class Exp4_4 public static void main(String args) if (args.length != 2) System.err.println(" 命令语法不正确,使用格式为: java Exp4_4 要复制的文 件 复制到的路径 ");return; System.out.println(" 成功将 "" + args0 + "" 复制到 ""
34、+ args1 + "" 。 ");实验五 /*5. 输出如上图所示的循环移位方阵(第一行存于一维数组,循环右移该行元素一个位置以 产生下一行,以此类推)19847984757 5198*/ package ch05;public class Exp4_5 void 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.lengt
35、h; 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);for (int i = 0; i < a.length - 1; i+) t.shift(a); t.print(a);第五章 实验一/*2. 在例 6.17 的基础上,将 draw 方法改为计算形状自身面积的 calcArea 方法并在测试类中 测试。S
36、quare 类提示:可根据需要在 Shape 的每个子类中增加用以计算面积所必须的字段,如为 增加边长输出结果: s1 的面积: s2 的面积: s3 的面积:字段、 Circle 类增加半径字段等,然后编写含相应字段的构造方法以构造具体的形状对象。4.06.012.566370614359172*/ package ch06;class Shape public double calcArea() return 0;class Square extends Shape float width;public Square(int width) this.width = width;public
37、 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;p
38、ublic Circle(float radius) this.radius = radius;public double calcArea() return Math.PI * radius * radius; public class Exp5_2 public static void main(String args) Shape s1 = new Square(2); Shape s2 = new Triangle(3, 4, 5); Shape s3 = new Circle(2); System.out.println("s1 System.out.println(&qu
39、ot;s2 System.out.println("s3 实验二 /*3. 编写 TimeCounter 类,其静态方法 秒数 begin 在命令行显示倒计时(每隔 中调用 startCount 方法。的面积: 的面积: 的面积:" + s1.calcArea();" + s2.calcArea();" + s3.calcArea();startCount(int begin) 方法根据指定的初始1 秒刷新,时间为 0 时退出程序) ,然后在 main 方法*/ package ch06;public class Exp5_3 static void s
40、tartCount(int begin) while (begin > 0) System.out.println(begin); long init = System.currentTimeMillis(); while (true) long now = System.currentTimeMillis(); if (now - init >= 1000) begin-;break;System.out.println(" 时间到! ");public static void main(String args) startCount(5);实验三/*4.编写
41、 Complex 类表示数学上的复数概念,具体包括: real 和 image 字段,分别表示复数的实部和虚部。 读取和设置 real/image 字段的 get 和 set 方法。 根据实部和虚部参数构造复数对象的构造方法。 打印当前复数对象内容以及与另一复数相加的方法,原型为:void printInfo();Complex add(Complex anotherComplex); 重写父类 Object 的 equals 方法,相等逻辑为“若 2 个复数对象的实部和虚部分别对应相 等,则这 2 个复数相等” 。最后编写一个带 main 方法的测试类 ComplexTest ,分别测试 C
42、omplex 中的各个方法。 */package ch06;class Complex float real; float image;public float getReal() return real;public void setReal(float real) this.real = real;public float getImage() return image;public void setImage(float image) this.image = image;public Complex(float real, float image) this.real = real;
43、this.image = image;void printInfo() H:HSystem.out.println(real + (image > 0 ? "+" : "-") + Math.abs(image) + Complex add(Complex anotherComplex) Complex c = new Complex(0, 0); c.setReal(this.real + anotherComplex.getReal(); c.setImage(this.image + anotherComplex.getImage(); re
44、turn c;public boolean equals(Object obj) Complex c = (Complex) obj;return this.real = c.getReal() && this.image = c.getImage(); public class Exp5_4 public static void main(String args) Complex c1 = new Complex(1, -2); Complex c2 = new Complex(2, 6); Complex c3 = c1.add(c2); Complex c4 = c2.a
45、dd(c1);System.out.print("c1=");c1.printInfo();System.out.print("c2=");c2.printInfo();System.out.print("c3=");c3.printInfo();System.out.print("c4=");c4.printInfo();System.out.println(c1.equals(c2) ? "c1=c2" : "c1!=c2");System.out.println(c3.
46、equals(c4) ? "c3=c4" : "c3!=c4"); 实验四/*5. 查阅 java.util 包下的 GregorianCalendar 类的 API 文档,编写 MyDate 类继承该类,并实 现以下方法:MyDate(int year, int month, int day); / 根据指定的年月日构造日期对象/ 得到当前日期的年份/ 得到当前日期的月份/ 得到当前日期是本年的第几天/ 得到当前日期是本月的第几天/ 得到当前日期是本周的第几天(即星期几)int getYear();int getMonth();int getDayOf
47、Year();int getDayOfMonth();MyDate getBeforeDate(int beforeDays);/ 得到当前日期之前若干天对应的日期对象/ 得到当前日期之后若干天对应的日期对象 / 得到当前日期与指定日期 d 相隔多少天int getDayOfWeek();MyDate getAfterDate(int afterDays); int daysBetweenWith(MyDate d); 最后编写一个带 main 方法的测试类 MyDateTest ,分别测试 MyDate 中的各个方法。 提示:可通过父类GregorianCalendar相应方法的组合以实现上
48、述各方法。 注意父类中根据年月日创建日历对象的构造方法中,月份参数是从 0 开始的。 */package ch06;import java.util.GregorianCalendar;class MyDate extends GregorianCalendar MyDate(int year, int month, int day) super(year, month - 1, day);int getYear() return super.get(YEAR);int getMonth() return super.get(MONTH) + 1;int getDayOfYear() retu
49、rn super.get(DAY_OF_YEAR); int getDayOfMonth() return super.get(DAY_OF_MONTH);int getDayOfWeek() return super.get(DAY_OF_WEEK); MyDate getBeforeDate(int beforeDays) long time = super.getTimeInMillis() - beforeDays * 24L * 60 * 60 * 1000; GregorianCalendar gc = new GregorianCalendar();gc.setTimeInMil
50、lis(time);return new MyDate(gc.get(YEAR), gc.get(MONTH) + 1, gc.get(DAY_OF_MONTH); MyDate getAfterDate(int afterDays) long time = super.getTimeInMillis() + afterDays * 24L * 60 * 60 * 1000; GregorianCalendar gc = new GregorianCalendar();gc.setTimeInMillis(time);return new MyDate(gc.get(YEAR), gc.get
51、(MONTH) + 1, gc.get(DAY_OF_MONTH); int daysBetweenWith(MyDate d) long t1 = super.getTimeInMillis(); long t2 = d.getTimeInMillis(); return (int) (Math.abs(t1 - t2) / (24 * 60 * 60 * 1000);void print(String name) %d-%d-%dn", name, getYear(), getMonth(),System.out.printf("%-4s: getDayOfMonth(
52、);public class Exp5_5 public static void main(String args) MyDate d1 = new MyDate(2014, 3, 12);MyDate d2 = new MyDate(2012, 5, 5);MyDate d3 = new MyDate(2013, 3, 12);d1.print("d1");d2.print("d2");d3.print("d3");System.out.println("d3System.out.println("d3System.out.println("d3是该星期的第 " + d3.getDayOfWeek() + " 天"); 是该月的第 " + d3.getDayOfMonth() + " 天"); 是该年的第 " + d3.getD
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年人民版九年级生物下册阶段测试试卷含答案
- 2025年华师大新版选修4化学下册阶段测试试卷含答案
- 2025年沪科版第二册生物上册月考试卷
- 2025年人教新课标七年级生物下册阶段测试试卷含答案
- 2025年粤教沪科版八年级科学上册月考试卷含答案
- 2025年沪教版九年级历史上册阶段测试试卷含答案
- 2025年新世纪版七年级物理上册阶段测试试卷含答案
- 2025年华东师大版必修3历史上册月考试卷含答案
- 2025年度网络文字处理专家劳动合同4篇
- 2025年度智能门窗系统销售安装与升级合同4篇
- 2025年度版权授权协议:游戏角色形象设计与授权使用3篇
- 心肺复苏课件2024
- 《城镇燃气领域重大隐患判定指导手册》专题培训
- 湖南财政经济学院专升本管理学真题
- 全国身份证前六位、区号、邮编-编码大全
- 2024-2025学年福建省厦门市第一中学高一(上)适应性训练物理试卷(10月)(含答案)
- 《零售学第二版教学》课件
- 广东省珠海市香洲区2023-2024学年四年级下学期期末数学试卷
- 房地产行业职业生涯规划
- 江苏省建筑与装饰工程计价定额(2014)电子表格版
- MOOC 数字电路与系统-大连理工大学 中国大学慕课答案
评论
0/150
提交评论