




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
x x x x x x x x 大大 学学 计计 算算 机机 与与 信信 息息 技技 术术 学学院院 实实验验报报告告 姓 名学 号专业班级 课程名称 Java 实验实验日期 成 绩指导教师批改日期 实验名 称实验 3 3 方法和方法和数组 实验目的 实验目的 1 掌握一维数组和二维数组的定义 初始化方法 2 了解和初步应用 java lang Math 类的 random 方法处理实际问题 3 了解增强 for 循环 并使用增强 for 循环顺序访问数组元素 4 掌握 String 类中 split 方法 charAt 方法以及 length 方法的使用 5 掌握 Double Integer 等数据包装类的 parseDouble parseInt 等方法 6 掌握数组的 length 属性的应用 实验内容 实验内容 要求把源程序和运行结果图都粘贴到实验报告中 1 分别用一维数组 例子数组如下 7 4 3 9 0 6 实现冒泡排序 选择排序和插入排序中的两种 排序算法 程序中要求加注释 程序代码 1 选择排序 import java util public class LX3 1 public static void main String args double text 7 4 3 9 0 6 SelectionSort text public static void SelectionSort double list for int i 0 i list length 1 i 第一层循环 控制拍第x数 double currentMin list i currentMin存储现有的最小值 int currentMinIndex i 最小值的位置 for int j i 1 jlist j 如果有比他小的 currentMin list j 吧此时的值放入currentMin currentMinIndex j 记下此时的位置 if currentMinIndex i list currentMinIndex list i list i currentMin 交换位置 System out println 排序结果是 for int i 0 i list length i System out print list i 输出已排序数组 运行结果贴图 2 插入排序 import java util public class LX3 2 public static void main String args double text 7 4 3 9 0 6 insertionSort text public static void insertionSort double list for int i 1 i 0k list k 1 list k 移动出插入位置 list k 1 currentElement 放入插入数据 System out println 排序结果是 for int i 0 i list length i System out print list i 输出已排序数组 运行结果贴图 3 冒泡排序 import java util public class LX3 3 public static void main String args double text 7 4 3 9 0 6 Sort text public static void Sort double list for int i 0 ii j length 1 length超出范围 if list j list j 1 double t list j list j list j 1 list j 1 t System out println 排序结果是 for int i 0 i list length i System out print list i 输出已排序数组 运行结果贴图 2 编写程序实现两个矩阵的相加 相乘 要求程序运行结果形如如下显示 Array c 1 2 3 4 5 6 7 8 9 Array d 2 2 2 1 1 1 3 3 3 Array c d 3 4 5 5 6 7 10 11 12 Array c d 13 13 13 31 31 31 49 49 49 程序代码 public class LX3 4 param args public static void main String args int c 1 2 3 4 5 6 7 8 9 int d 2 2 2 1 1 1 3 3 3 System out println Array c shuChu c System out println Array d shuChu d System out println Array c d Add c d System out println Array c d Cheng c d TODO Auto generated method stub public static void shuChu int shuChu for int i 0 i 3 i for int j 0 j 3 j System out printf 4d shuChu i j System out println public static void Add int add1 int add2 int Answer new int 3 3 for int i 0 i 3 i for int j 0 j 3 j Answer i j add1 i j add2 i j shuChu Answer public static void Cheng int cheng1 int cheng2 int Answer new int 3 3 for int i 0 i 3 i for int j 0 j 3 j Answer i j cheng1 i 0 cheng2 0 j cheng1 i 1 cheng2 1 j cheng1 i 2 cheng2 2 j shuChu Answer 运行结果贴图 3 将用 和 分割的包含数字字符的字符串 23 21 3 33 34 2 1 9 2 1 3 3 1 3 4 4 9 中 的数据解析出来放在一个 double 类型的二维数组中 以分号分割二维数组的每一行 以逗号分割每 行中的各个元素 利用 String 的 split 方法 程序代码 import javax swing plaf basic BasicBorders SplitPaneBorder public class LX3 5 param args public static void main String args String a 23 21 3 33 34 2 1 9 2 1 3 3 1 3 4 4 9 split String b new String 3 for int i 0 i a length i b i a i split 第i行的值 且split仅能放入Stringl类型 double c new double 3 8 第二个必须有范围 否则无法运行 for int i 0 i 3 i for int j 0 j b i length j c i j Double parseDouble b i j System out printf 8 1f c i j System out println TODO Auto generated method stub 运行结果贴图 4 查看帮助 编写例子 利用 System 类中的 arraycopy 方法复制数组 分别用 Arrays 类中的 sort 方法和 binarySearch 方法实现数组的排序和折半查找 程序代码 1 复制数组 public class LX3 6 public static void main String args int old 1 2 3 4 5 6 7 int copy new int 7 System arraycopy old 0 copy 0 old length TODO Auto generated method stub for int i 0 i copy length i System out println copy i 运行结果贴图 2 数组排序 import java util public class LX3 7 public static void main String args double te 7 4 3 9 0 6 Arrays sort te 多种方法重载 for int i 0 i te length i System out println te i 运行结果贴图 3 折半查找 import java util public class LX3 8 public static void main String args double te 1 9 21 31 44 56 78 99 System out println 请输入要查找的数 Scanner input new Scanner System in int i input nextInt System out println Arrays binarySearch te i 运行结果贴图 5 随机生成 100 个小写字母 统计每个字母出现的次数 并显示出来 利用 Math random 方法随机产生 程序代码 public class LX3 9 public static void main String args char number new char 100 for int i 0 i number length i number i char a Math random z a 1 CountNumber number public static void CountNumber char Count int counts new int 26 for int i 0 i Count length i counts Count i a count字符减字符存到相应的位置 SystemOut counts public static void SystemOut int syso for int i 0 i syso length i System out println char i a 出现的次数是 syso i i对应 a z 运行结果贴图 6 建立一个不规则的二维数组如下 并在控制台显示 数组如下 1 3 5 2 4 6 8 1 9 16 25 36 10 20 30 1 2 3 4 5 6 程序代码 public class LX3 10 public static void main String args int new1 1 3 5 2 4 6 8 1 9 16 25 36 10 20 30 1 2 3 4 5 6 for int i 0 i 5 i for int j 0 j new1 i length j System out printf 4d new1 i j System out println 运行结果贴图 7 编写两个重载的方法分别交换两个整型变量 和整型数组的第一个和第二个元素 运行并分析 结果 程序代码 public class LX3 11 public static void main String args int a 2 b 5 int c 2 5 Switch a b Switch c public static void Switch int a int b int t t a a b b t System out println 交换以后 System out println a a b b public static void Switch int a int t t a 0 a 0 a 1 a 1 t System out println a 0 a 0 a 1 a 1 运行结果贴图 课后作业题 P153 5 16 5 23 5 25 P184 6 13 P186 6 26 P202 7 1 7 2 7 6 7 23 5 16 程序代码 public class LX3 12 public static void main String args for int i 2000 i 2010 i System out println i 年的天数是 numberOfDaysInYear i public static int numberOfDaysInYear int year if year 400 0 year 4 0 else return 365 运行结果贴图 5 23 程序代码 public class LX3 13 public static void main String args System out println 100个大写字母 SysOut A Z System out println 100个一位数 SysOut 0 9 public static char getRandomCharacter char ch1 char ch2 return char ch1 Math random ch2 ch1 1 public static void SysOut char zf1 char zf2 for int i 0 i 10 i for int j 0 j 10 j System out printf 4c getRandomCharacter zf1 zf2 System out println 运行结果贴图 5 25 程序代码 import java util public class LX3 14 public static void main String args Scanner input new Scanner System in System out println 请输入毫秒数 long i input nextLong System out println convertMillis i public static String convertMillis long mills long a mills 3600000 long b mills a 3600000 60000 long c mills a 3600000 b 60000 1000 return a b c 转换为String类型 1 i 空或字符串 2 String valueOf i 运行结果贴图 6 13 程序代码 public class LX3 15 public static void main String args int a 1 4 13 51 System out println getRandom a public static int getRandom int numbers int s int 1 Math random 54 for int i 0 i numbers length i int a numbers i s if a 0 s getRandom numbers return s 运行结果贴图 6 26 程序代码 import java util public class LX3 16 public static void main String args System out println Enter list1 int list1 creatArray System out println Enter list2 int list2 creatArray compare list1 list2 public static int creatArray int length 0 Scanner input new Scanner System in length input nextInt int a new int length for int i 0 i a length i a i input nextInt return a public static void compare int ch1 int ch2 int s 0 if ch1 length ch2 length for int i 0 i ch1 length i s ch1 i ch2 i if s 0 System out println Two lists are not strictly identical System exit 0 System out println Two lists are strictly identical else System out println Two lists are not strictly identical 运行结果贴图 7 1 程序代码 import java util public class LX3 17 param args public static void main String args System out println Enter a 4 by 4 matrix now by row System out println Sun of the matrix is add creatArrays public static int creatArrays int array1 new int 4 4 Scanner input new Scanner System in for int i 0 i 4 i for int j 0 j 4 j array1 i j input nextInt return array1 public static int add int list int sum 0 for int i 0 i 4 i for int j 0 j 4 j sum sum list i j return sum 运行结果贴图 7 2 程序代码 import java util public class LX3 18 public static void main String args System out println Enter a 4 by 4 matrix now by row System out println Sun of the elements in the major diagonal is add creatArrays public static int creatArrays int array1 new int 4 4 Scanner input new Scanner System in for int i 0 i 4 i for int j 0 j 4 j array1 i j input nextInt return array1 public static int add int list int sum 0 for int i 0 i 4 i for int j 0 j 4 j if i j sum sum list i j return sum 运行结果贴图 7 6 程序代码 import java util Scanner public class LX3 19 param args public static void main String args System out print Enter matrix1 double array1 creatArrays System out print Enter matrix2 double array2 creatArrays double array3 Cheng array1 array2 System out println The matrices are multiplied as follows sysOut array1 array2 array3 TODO 自动生成的方法存根 public static void sysOut double m1 double m2 double m3 for int i 0 i m1 length i for int j 0 j m1 i length j System out printf 2 1f m1 i j if i m1 length 2 System out print else System out print for int j 0 j m2 i length j System out printf 2 1f m2 i j if i m1 length 2 System out print else System out print for int j 0 j m3 i length j System out printf 3 1f m3 i j System out println public static double creatArrays double array1 new double 3 3 Scanner input new Scanner System in for int i 0 i 3 i for int j 0 j 3 j array1 i j input nextDouble return array1 public static double Cheng double cheng1 double cheng2 double Answer new double 3 3 for int i 0 i 3 i for int j 0 j 3 j Answer i j cheng1 i 0 cheng2 0 j cheng1 i 1 cheng2 1 j cheng1 i 2 cheng2 2 j return Answer 运行结果贴图 7 23 程序代码 import java util public class LX3 20 public static void main String args double arrayText creatArrays double su 1 count1 arrayText double arrayText2 array2 arrayText double arrayText3 array3Count arrayText2 su print arrayText3 public static double creatArrays System out println Enter all a12 a13 a21 a22 a23 a31 a32 a33 double array new double 3 3 Scanner input new Scanner System in for int i 0 i 3 i for int j 0 j 3 j array i j input nextDouble
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初中数学教学设计每课件
- 电信冬季安全知识培训课件
- 电仪维修基础知识培训课件
- 2025年餐饮连锁企业人力资源总监招聘面试模拟题及解析
- 2025年物业高级管理面试热点解析模拟题及应对策略
- 甲状腺穿刺术课件
- 甲状腺切除术手术课件
- 低段识字教学试讲课件
- 用色彩表达情感课件
- 2024年江苏省徐州市行政管理、人事管理等管理人员综合技能知识考试库【基础题】
- 2025年职测e类试题及答案
- 消防车辆安全行驶课件
- 偏瘫患者穿衣健康宣教
- 无废工厂宣传课件
- 酒店预算培训课件
- 关于财富的课件
- 2025-2030中国汽车工程服务外包(ESO)行业现状调查与前景趋势研究报告
- 2025至2030全球及中国实验室PH电极行业发展趋势分析与未来投资战略咨询研究报告
- 儿科血小板减少的护理查房
- 林下生态养鸡技术课件
- 高中语文课程标准测试题答案
评论
0/150
提交评论