您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 实验一程序设计基础及算法
实验报告一JAVA程序设计基础及算法设计一、实验目的:(1)掌握JAVA语言的语法,理解数组和对象的引用模型,理解类的封装、继承和多态(2)掌握类的设计方法(3)掌握异常处理方法和标准输出方法,了解标准输入方法(4)熟悉算法的描述方法、算法时间复杂度的分析和计算方法(5)理解数据和算法的基本概念二、实验内容:1、采用二维数据输出杨辉三角形,二维数据的结构如图1所示:012345matmat[0]mat[1]mat[2]mat[3]mat[4]mat[5]图1杨辉三角形的二维数组结构请粘贴源程序及运行测试结果:源程序:packageQ1;publicclassTest{publicstaticvoidmain(String[]args){int[][]y=newint[11][11];y[1][1]=y[2][1]=y[2][2]=1;for(inti=3;i=6;i++){y[i][1]=1;y[i][i]=1;}for(inti=3;i=6;i++)for(intj=2;ji;j++)y[i][j]=y[i-1][j-1]+y[i-1][j];for(inti=1;i=6;i++){for(intj=1;j=i;j++){System.out.printf(%5d,y[i][j]);}System.out.println();}}}运行结果:111121133114641151010512、找出一个二维数据的鞍点,即该位置上的元素在该行上最大,在该列中最小。一个二维数组可能没有鞍点,如果有,那么它只有一个鞍点。请粘贴源程序及运行测试结果:packageQ2;publicclassTest{publicstaticvoidfun_min(inta[][],intmax[]){intflag=0;intcols=max[2];intmin=a[0][cols];for(introws=0;rows5;rows++){if(a[rows][cols]max[0])flag=1;}if(flag==0){max[1]++;max[2]++;System.out.println(point=+max[0]+,rows=+max[1]+,cols=+max[2]);}}publicstaticvoidfun_max(inta[][]){intmax[]=newint[3];introws,cols;for(rows=0;rows5;rows++){max[0]=a[rows][0];max[1]=rows;max[2]=0;for(cols=0;cols5;cols++){if(a[rows][cols]max[0]){max[0]=a[rows][cols];max[1]=rows;max[2]=cols;}}fun_min(a,max);}}publicstaticint[][]init(){inta[][]=newint[5][5];for(inti=0;i5;i++)//createfor(intj=0;j5;j++){a[i][j]=(int)(Math.random()*101);}for(inti=0;i5;i++)//printallfor(intj=0;j5;j++){if(j!=4)System.out.printf(%5d,a[i][j]);else{System.out.printf(%5d,a[i][j]);System.out.println();}}returna;}publicstaticvoidmain(String[]args){inta[][]=init();fun_max(a);}}结果:3、设计复数类,成员变量包括实部和虚部,成员方法包括实现复数加法、减法、比较、转换成字符串等运算或操作。[测试数据](1)Z1=0,Z2=0;(2)Z1=4,Z2=3i;(3)Z1=3+1.5i,Z2=8-1.5i;(4)Z1=-4+3.4i,Z2=-6-8.1i;(5)Z1=-5.4+1.2i,Z2=5.4+3.2i;(6)Z1的共轭复数:packageQ3;publicclassComplex{privatedoubler;privatedoublei;publicComplex(){}publicComplex(doubler,doublei){this.r=r;this.i=i;}publicvoidsetRealPart(doubler){this.r=r;}publicdoublegetRealPart(){returnr;}publicvoidsetImaginaryPart(doublei){this.i=i;}publicdoublegetImaginaryPart(){returni;}publicComplexcomplexAdd(Complexc){doubler1=this.r;doublei1=this.i;doubler2=c.r;doublei2=c.i;Complexc1=newComplex();c1.setRealPart(r1+r2);c1.setImaginaryPart(i1+i2);returnc1;}publicComplexcomplexMinus(Complexc){doubler1=this.r;doublei1=this.i;doubler2=c.r;doublei2=c.i;Complexc1=newComplex();c1.setRealPart(r1-r2);c1.setImaginaryPart(i1-i2);returnc1;}publicvoidcomplexCompare(Complexc){doubler1=this.r;doublei1=this.i;doubler2=c.r;doublei2=c.i;if(Math.sqrt(r1*r1+i1*i1)Math.sqrt(r2*r2+i2*i2)){System.out.println(this.toString1()++c.toString1());}if(Math.sqrt(r1*r1+i1*i1)==Math.sqrt(r2*r2+i2*i2)){System.out.println(this.toString1()+=+c.toString1());}if(Math.sqrt(r1*r1+i1*i1)Math.sqrt(r2*r2+i2*i2)){System.out.println(this.toString1()++c.toString1());}}publicStringtoString(){if(this.i0)returnr++i+i;elsereturnr+++i+i;}publicStringtoString1(){if(this.i0)return(+r++i+i);elsereturn(+r+++i+i);}}packageQ3;publicclassTest{publicstaticvoidmain(String[]args){Complexc11=newComplex(0,0);Complexc12=newComplex(0,0);Complexc21=newComplex(4,0);Complexc22=newComplex(0,3);Complexc31=newComplex(3,1.5);Complexc32=newComplex(8,-1.5);Complexc41=newComplex(-4,3.4);Complexc42=newComplex(6,-8.1);Complexc51=newComplex(-5.4,1.2);Complexc52=newComplex(5.4,3.2);Complexc61=newComplex(3,2);Complexc62=newComplex(3,-2);System.out.println(c11.toString1()+-+c12.toString1()+=+c11.complexAdd(c12).toString());System.out.println(c11.toString1()+*+c12.toString1()+=+c11.complexMinus(c12).toString());c11.complexCompare(c12);System.out.println(***************************************************);System.out.println(c21.toString1()+-+c22.toString1()+=+c21.complexAdd(c22).toString());System.out.println(c21.toString1()+*+c22.toString1()+=+c21.complexMinus(c22).toString());c21.complexCompare(c22);System.out.println(***************************************************);System.out.println(c31.toString1()+-+c32.toString1()+=+c31.complexAdd(c32).toString());System.out.println(c31.toString1()+*+c32.toString1()+=+c31.complexMinus(c32).toString());c31.complexCompare(c32);System.out.println(***************************************************);System.out.println(c41.toString1()+-+c42.toString1()+=+c41.complexAdd(c42).toString());System.out.println(c41.toString1()+*+c42.toString1()+=+c41.complexMinus(c42).toString());c41.complexCompare(c42);System.out.println(***************************************************);System.out.println(c51.toString1()+-+c52.toString1()+=+c51.complexAdd(c52).toString());System.out.println(c51.toString1()+*+c52.toString1()+=+c51.complexMinus(c52).toString());c51.complexCompare(c52);System.out.println(***************************************************);System.out.println(c61.toString1()+-+c62.toString1()+=+c61.complexAdd(c62).toString());System.out.println(c61.toString1()+*+c62.toString1()+=+c61.complexMinus(c62).toString());c61.complexCompare(c62);System.out.println(***************************************************);}}[粘贴测试结果]4、数组逆置。将一个已知数组中所有元素的次序颠倒为相反次序,求算法的时间复杂度和空间
本文标题:实验一程序设计基础及算法
链接地址:https://www.777doc.com/doc-2457883 .html