java实验报告汇总

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第一章实验报告1、安装与调试JDK

第二章实验报告1、运行Welcome.java

2、运行ImageView.java

第三章实验报告

1、一个简单的java应用程序:

2、关于java基本程序设计结构的实验:

3、常量:

4、字符串操作:

5、构建字符串:

6、输入输出:

7、条件语句

用for、while、和do while编写计算n!的小程序。输入欲求其阶乘的正整数n,返回n!,

如下:

8、字符串

以上是为foreach循环。

第四章实验报告

1、类、对象、访问器方法、修改器方法、静态实例域、静态方法等概念的一个小程序。以宿舍四位室友的学号和假定收入为依据。代码如下:

package p;

public class R

{

/**

*This program demonstrates static methods.

*@version 1.012013-05-08

*@author SCC

*/

public static void main(String[] args)

{

//fill the roommates with 4 objects

RoomMate[] roommates = new RoomMate[4];

roommates[0] = new RoomMate("SLK",10000);

roommates[1] = new RoomMate("SCC",9000);

roommates[2] = new RoomMate("WHL",8000);

roommates[3] = new RoomMate("MJP",7000);

//print out information about all roommates

for (RoomMate any:roommates)

{

any.setId();

System.out.println("name="+any.getName()+",id="+any.getId()+",sal ary="+any.getSalary());

}

int n = RoomMate.getNextId();//calls static method

System.out.println("Next avaliable id = "+n);

}

}

class RoomMate

{

public RoomMate(String n,double s)

{

name = n;

salary = s;

id = 0;

}

public String getName()

{

return name;

}

public double getSalary()

{

return salary;

}

public int getId()

{

return id;

}

public void setId()

{

id = nextId;//set id to next available id

nextId++;

}

public static int getNextId()

{

return nextId;

}

public static void main(String[] args)

{

RoomMate any = new RoomMate("SCC",5000);

System.out.println(any.getName()+" "+any.getSalary());

}

private String name;

private double salary;

private int id;

private static int nextId = 2011051526;

}

运行结果如下:

2、论证一个方法不能改变值参数的值,但可以通过对对象参数的拷贝修改所引用的对象的状态,以及java的参数引用是值调用而不是引用调用。代码如下:

package p;

public class R

{

/**

*This program demonstrates parameter passing in java.

*@version 1.012013-05-08

*@author SCC

*/

public static void main(String[] args)

{

/*

* Test 1: Methods can not modify numeric parameters.

*/

System.out.println("Test tripleValue:");

double a = 10;

System.out.println("Before: a="+a);

tripleValue(a);

System.out.println("After: a="+a);

/*

* Test 2:Methods can change the state of object parameters.

*/

System.out.println("\nTest tripleSalary:");

RoomMate mjp = new RoomMate("MJP",7000);

System.out.println("Before: salary="+mjp.getSalary());

tripleSalary(mjp);

System.out.println("After: salary= "+mjp.getSalary());

/*

* Test 3: Methods can not attach new object to object parameters.

*/

System.out.println("\nTest swap");

RoomMate slk = new RoomMate("SLK",9000);

RoomMate whl = new RoomMate("WHL",8000);

System.out.println("Before: slk="+slk.getName());

System.out.println("Before: whl="+whl.getName());

swap(slk,whl);

System.out.println("After: slk="+slk.getName());

System.out.println("After: whl="+whl.getName());

}

public static void tripleValue(double x)

{

x = 3*x;

相关文档
最新文档