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

加入VIP,免费下载
 

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

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

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(《Java编程语言原理与范例》课后实验源代码.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

1、Java编程语言原理与范例课后实验源代码第一章实验一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.f

2、ormat(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;import javax.

3、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(button); win.setSi

4、ze(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 mouseX = button.getX(

5、) + 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) 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(Strin

7、g 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. 对于int型变量a,以最快的速度计算34a的值。 */package ch03

8、;public class Exp2_4 public static 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 Sca

9、nner(System.in); System.out.println(输入整数:); 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 ar

10、gs) 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.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 - Ma

11、th.abs(i) - Math.abs(j); System.out.println(); 实验三/*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.pr

12、int(输入行数:); 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 += firstStepOfRow+; System.ou

13、t.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.println(sum); 第四章实验一/*1. 产生10个100以

14、内的随机整数以填充一维数组,实现以下功能。 找出最大以及最小值。 查找给定整数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 = r.nextInt(100); return a; void print

15、(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 ai) min = ai; return min; int findLastLocation(int a, int x

16、) 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 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 a

17、rgs) 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.print(输入要查找的数:); Scanner s = new Scanner(System.in); int x = s.nextInt(); int i = t.findLastLocation(a, x); if (i = -1) System.out.println(x + 在数组中不存

18、在。); else System.out.printf(Last location of %d: %d。n, x, i); if (t.isAsc(a) System.out.println(数组是非递减排列!); else System.out.println(数组不是非递减排列!); t.reverse(a); System.out.println(翻转后的数组:); t.print(a); 实验二/*2. 将a插入到一个长度不小于10且元素呈递增排列的一维数组中,并保证插入之后的数组依然递增(若a在插入前的数组中存在,则输出提示并忽略)。 */package ch05;import ja

19、va.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 end; i+) System.out.printf(%-5d, ai); System.out.println(); int findInsertLocation(int x) int i =

20、0; for (; i 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(); int i = t.findIns

21、ertLocation(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.Scanner;/* * 鞍点对象类 */class AnDian priv

22、ate 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 = row; public int getCol() ret

23、urn 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 LIMIT = 3; / 矩阵元素值的上限(为测试方便此处写死,也可在运行时由用户输入) / 初始化矩阵 void initArray() Scanner scanner = new Scanner(System.in); System.out.print(输入矩阵行数:); int m = scann

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1