计科1309050158_JavaExp1,2_邓豪.doc_第1页
计科1309050158_JavaExp1,2_邓豪.doc_第2页
计科1309050158_JavaExp1,2_邓豪.doc_第3页
计科1309050158_JavaExp1,2_邓豪.doc_第4页
计科1309050158_JavaExp1,2_邓豪.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java语言程序设计实验报告实验序号:01实验项目名称:数据类型使用学号1309050158姓名邓豪专业、班计科1301实验地点文波指导教师韩志农时间2015.04.04一、实验目的及要求熟悉数据类型使用及转换。二、实验设备(环境)PC,eclipse.三、实验内容与步骤定义数据:Poly poly = new Poly();int init = 9;char charact = a;boolean bool = true;byte by = 1;short sh = 4;long lo = 336;double dou = 78.12d;float flo = 453.12f;String str = asd;Integer a = 25;1.运用、*、/、运算组合情况,测试结果的数据类型并显示在console中(用Poly.getType()方法)。/运用、*、/、运算组合情况,测试结果的数据类型并显示在console中(用Poly.getType()方法)。System.out.println(Poly.getType(init+charact);System.out.println(Poly.getType(init%sh);System.out.println(Poly.getType(init/lo);System.out.println(Poly.getType(init-dou);System.out.println(Poly.getType(flo+dou);System.out.println(Poly.getType(charact+str);System.out.println(Poly.getType(str+init);System.out.println(Poly.getType(lo%by);System.out.println(Poly.getType(lo-by);System.out.println(Poly.getType(flo%by);System.out.println();System.out.println();2. 设计至少6种数据被0和0.0除的情况, 观察运行结果,如果能显示在console中,则显示/设计至少6种数据被0和0.0除的情况, 观察运行结果,如果能显示在console中,则显示。/*System.out.println(init/0);*/* * 注释掉的代码出现算术异常 */System.out.println(init/0.0);System.out.println(flo/0);System.out.println(flo/0.0);System.out.println(dou/0);System.out.println(dou/0.0);/*System.out.println(by/0);*/System.out.println(by/0.0);/*System.out.println(sh/0);*/System.out.println(sh/0.0);/*System.out.println(lo/0);*/System.out.println(lo/0.0);/*System.out.println(a/0);*/System.out.println(a/0.0);/*System.out.println(charact/0);*/System.out.println(charact/0.0);3.设计至少5种char型数据和int型数据之间相互转换的情况,显示在console中。(每一句都要有明确的设计意图, 并用单行注释在其后标出。)/设计至少5种char型数据和int型数据之间相互转换的情况,显示在console中。/(每一句都要有明确的设计意图, 并用单行注释在其后标出。char a1 = 15;char a2 = B;int b1;System.out.println(a1);/1.整型数据转换为字符型并对应ASCII码表输出b1 = (int)a2;/2.使用强制类型转换将字符型转换为整型并输出System.out.println(b1);a2 = (char) b1;System.out.println(a2);/3.使用强制类型转换将整型转换为字符型输出b1 = a1 + a2;System.out.println(b1);/4.使两个字符型数据相加输出为整型 Character ch2 = 8; / char是基本数据类型,Character是其包装类型。int num2 = Integer.parseInt(ch2.toString();/5.利用包装类中的方法进行转换System.out.println(num2);四、实验结果与数据处理(1):(2):(3):五、分析与讨论(体会、感想、意见、建议)经过此次实验,理解了数据类型之间的差异以及如何转换,在Java中最常用的是强制类型转换,并且使用较为频繁。明白了一些算术异常出现的原因。看到书后面可以用try catch 语句消除异常。会使用了注释与消除注释的快捷键。以及alt+/。教师评语及成绩:签名:日期:实验序号:02实验项目名称:熟悉java结构,各种常用方法及语句的使用学号1309050158姓名邓豪专业、班计科1301实验地点文波机房指导教师韩志农时间2015.04.04一、实验目的及要求1、 熟悉程序结构2、 熟悉Java各种常用语句3、 熟悉几种Java常用方法的引用4、 培养好的源代码注释习惯5、 培养好的命名风格二、实验设备(环境)PC,eclipse.三、实验内容与步骤(1)static void dichotomySolvingEquations()/1.用对分法解方程 x(1/2)=cos(x) (x0)double y = 0,x = 0;double temp = (double) (Math.sqrt(x) - Math.cos(x);while(Math.abs(temp)0.0000000000000001)if(temp0)x = (x+y)/2; temp = (Math.sqrt(x) - Math.cos(x);System.out.println(x);System.out.println(Math.sqrt(x);System.out.println(Math.cos(x);(2)static void divisionAlgorithm()/2.用欧几里德辗转相除法求两个正整数的最大公约数int a,b;int commondivisor;Scanner in=new Scanner(System.in);System.out.println(Pleaseinputtwointegers,aandb:);a=in.nextInt(); b=in.nextInt(); if(a b) commondivisor = getCommond(a,b); else commondivisor = getCommond(b, a); System.out.println(commondivisor);/*static int getCommond(int a,int b)此处使用的是递归算法if(a%b = 0)return b;elsereturn getCommond(b,a%b);*/static int getCommond(int a,int b)此处使用的迭代算法int tempMod;if(a%b = 0)return b;elsewhile(a%b!=0)tempMod =a%b;a = b;b = tempMod;return b;(3)static void calculateCos()float x,value;Scanner in=new Scanner(System.in);System.out.println(Pleaseinputx);x=in.nextFloat();value = 1;for(int i = 1;i 50;i+)value = (float) (value+(Math.pow(-1, i)*Math.pow(x, 2*i)/factorial(2*i);System.out.println(cos(x)s value is +value);System.out.println(Math.cos(x);private static double factorial(int i) / TODO Auto-generated method stub double fatl =i;for(int k =1;ki;k+)fatl = fatl*(i-k);return fatl;(4)static void findPrime()/查找质数的算法一/4.编程计算1000以内的质数(输出格式:一行10个,按列右对齐)int i,j;int count = 0;while(true)for(i=2;i1001;i+)for(j=1;j=i)System.out.printf(%7d,i);count+;if(count%10 = 0)System.out.println();if(i1000)break;static void findPrime1()/查找质数的算法二int prime = 2;int count = 0;while(prime = 1000)boolean flag = true;for(int i = 2;i=0;j-)outputstring.append(inputstring.charAt(j);System.out.println(outputstring);(6)static void myTriangle()int ar=new int88;/-为1,+为0,利用位异或运算得出之后的值ar00=1;ar01=1;ar02=0;ar03=1;ar04=0;ar05=0;ar06=1;ar07=0;for(int i = 1;i8;i+)/从第二行开始for(int j = 1;j8;j+)arij=ari-1j-1ari-1j;for(int i=0;i8;i+)System.out.print( );for(int j=0;ji;j+)System.out.print( );for(int j=i;j8;j+)if(arij=1)System.out.print(-);else if(arij=0)System.out.print(+);System.out.println();(7)由于想不出数组如何实现进位,选择了大数运算static void countSum1()BigDecimal sum = new BigDecimal(0);/sum初植为0 int arr = new int20; for(int i = 1; i = 20; i+) arri-1 = i; for(int i = 0; i 20; i+) BigDecimal temp = new BigDecimal(arri);/将 int 类型的数据转换为 BigDecimal temp = temp.pow(arri); sum = sum.add(temp);/返回一个 BigDecimal,其值为 (this +temp)。 System.out.println(sum.toString();static void countSum()/int 最大2147483648 int arr = new int20

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论