手机版

实验一 程序设计基础及算法

时间:2025-07-03   来源:未知    
字号:

计算机科学与技术 ,数据结构java

实验报告一 JAVA程序设计基础及算法设计

一、 实验目的:

(1) 掌握JAVA语言的语法,理解数组和对象的引用模型,理解类的封装、继承和多态

(2) 掌握类的设计方法

(3) 掌握异常处理方法和标准输出方法,了解标准输入方法

(4) 熟悉算法的描述方法、算法时间复杂度的分析和计算方法

(5) 理解数据和算法的基本概念

二、 实验内容:

1、 采用二维数据输出杨辉三角形,二维数据的结构如图1所示:

mat mat[1] mat[2] mat[3] mat[4] mat[5]

图1 杨辉三角形的二维数组结构

请粘贴源程序及运行测试结果:

源程序:

package Q1;

public class Test {

public static void main(String[] args) {

int[][] y=new int [11][11];

y[1][1]=y[2][1]=y[2][2]=1;

for(int i=3;i<=6;i++)

{ y[i][1]=1;

y[i][i]=1;

}

for(int i=3;i<=6;i++)

for (int j=2;j<i;j++)

y[i][j]=y[i-1][j-1]+y[i-1][j];

for (int i=1;i<=6;i++){

for (int j=1;j<=i;j++){

System.out.printf("%5d",y[i][j]);}

System.out.println();

}

}

}

运行结果:

计算机科学与技术 ,数据结构java

2、 找出一个二维数据的鞍点,即该位置上的元素在该行上最大,在该列中最小。一个二维

数组可能没有鞍点,如果有,那么它只有一个鞍点。

请粘贴源程序及运行测试结果:

package Q2;

public class Test {

public static void fun_min(int a[][],int max[]){ System.out.println("point="+max[0]+" ,rows="+max[1]+" ,cols="+max[2]); } public static void fun_max(int a[][]){ } public static int[][] init(){ int a[][] =new int [5][5]; for(int i=0;i<5;i++) //create for(int j=0;j<5;j++){ } a[i][j]=(int)(Math.random()*101); int max[]=new int [3]; int rows,cols; for(rows=0;rows<5;rows++){ } max[0]=a[rows][0]; max[1]=rows; max[2]=0; for(cols=0;cols<5;cols++){ } fun_min(a,max); if(a[rows][cols]>max[0]){ } max[0]=a[rows][cols]; max[1]=rows; max[2]=cols; } int flag=0; int cols=max[2]; intfor(int rows=0;rows<5;rows++){ if(a[rows][cols]<max[0]) } max[1]++;max[2]++; flag=1; if(flag==0){ for(int i=0;i<5;i++) //print all

计算机科学与技术 ,数据结构java

} } for(int j=0;j<5;j++){ } if(j!=4) } System.out.printf("%5d",a[i][j]); System.out.printf("%5d",a[i][j]); System.out.println(); else{ return a; public static void main(String[] args) { } int a[][]=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的共轭复数:

package Q3;

public class Complex {

public void setRealPart(double r) { public Complex(double r, double i) { } this.r = r; this.i = i; public Complex() { } private double r; private double i;

计算机科学与技术 ,数据结构java

} public double getRealPart() { } public void setImaginaryPart(double i) { } public double getImaginaryPart() { } public Complex complexAdd(Complex c) { } public Complex complexMinus(Complex c) { } public void complexCompare(Complex c){ double r1 = this.r; double i1 = this.i; double r2 = c.r; double i2 = c.i; if(Math.sqrt(r1*r1+i1*i1)>Math.sqrt(r2*r2+i2*i2)){ } System.out.println(this.toString1()+" > "+c.toString1()); double r1 = this.r; double i1 = this.i; double r2 = c.r; double i2 = c.i; Complex c1 = new Complex(); c1.setRealPart(r1 - r2); c1.setImaginaryPart(i1 - i2); return c1; double r1 = this.r; double i1 = this.i; double r2 = c.r; double i2 = c.i; Complex c1 = new Complex(); c1.setRealPart(r1 + r2); c1.setImaginaryPart(i1 + i2); return c1; return i; this.i = i; return r;

计算机科学与技术 ,数据结构java

} } } if(Math.sqrt(r1*r1+i1*i1)<Math.sqrt(r2*r2+i2*i2)){ } System.out.println(this.toString1()+" < "+c.toString1()); System.out.println(this.toString1()+" = "+c.toString1()); public String toString() { } public String toString1() { } if(this.i<0) return "(" + r + "" + i + "i)"; else return "(" + r + "+" + i + "i)"; if(this.i<0) return r + "" + i + "i"; else return r + "+" + i + "i";

package Q3;

public class Test {

System.out.println("*************************************************** System.out.println(c21.toString1() + "-" + c22.toString1() + " = " + http://plexAdd(c22).toString()); System.out.println(c21.toString1() + "*" + c22.toString1() + " = " "); public static void main(String[] args) { Complex c11 = new Complex(0, 0); Complex c12 = new Complex(0, 0); Complex c21 = new Complex(4, 0); Complex c22 = new Complex(0, 3); Complex c31 = new Complex(3, 1.5); Complex c32 = new Complex(8, -1.5); Complex c41 = new Complex(-4, 3.4); Complex c42 = new Complex(6, -8.1); Complex c51 = new Complex(-5.4, 1.2); Complex c52 = new Complex(5.4, 3.2); Complex c61 = new Complex(3, 2); Complex c62 = new Complex(3, -2); System.out.println(c11.toString1() + "-" + c12.toString1() + " = " + http://plexAdd(c12) …… 此处隐藏:4496字,全部文档内容请下载后查看。喜欢就下载吧 ……

实验一 程序设计基础及算法.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
    ×
    二维码
    × 游客快捷下载通道(下载后可以自由复制和排版)
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
    × 常见问题(客服时间:周一到周五 9:30-18:00)