ImageVerifierCode 换一换
格式:DOC , 页数:50 ,大小:148.50KB ,
资源ID:534597      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/534597.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(《Java编程语言:原理与范例》课后实验源代码.doc)为本站上传会员【Fis****915】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

《Java编程语言:原理与范例》课后实验源代码.doc

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 findAnDian() { List anDians = new ArrayList(); // 构造线性表对象 for (int i = 0; i < a.length; i++) { // 扫描矩阵中的每个元素 for (int j = 0; j < a[i].length; j++) { // 是当前行最大且当前列最小 if (a[i][j] == maxOfRows[i] &

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 anDians = o.findAnDian(); System.out.println("------------------------"); if (anDians.size() == 0) { // 返回的线性表元素个数为0 System.out.println("没有鞍点。"); } else { int i = 0; for (AnDian e : anDians) { // 迭代性for循环的语法也可用于线性表类型 System.out.printf("鞍点%-4d:a[%-3d][%-3d] =

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

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服