
一.实验目的及要求
1.掌握JDK1.6软件包、eclipse IDE工具的下载、安装
2.掌握设置Java程序运行环境的方法
3.掌握编写与运行Java程序的方法
4.掌握Java基本数据类型、运算符与表达式、数组的使用方法
5.理解Java程序语法结构,掌握顺序结构、选择结构和循环结构语法的程序设计方法
6.掌握运行Java程序的步骤
二.实验内容及步骤
1.从sun公司网站(http://java.sun.com/javase/downloads/index.jsp)下载jdk1.6程序包java jdk1.6.0.02.exe并将jdk安装到C:\\program files\\Java\\jdk1.6(公共jre可以不安装),从apache jarkart公司主页下载tomcat6.0,程序包apache-tomcat-6.0.18.zip,并解压到C:\\ apache-tomcat-6.0.18,从相关网站下载java IDE环境,程序包MyEclipse_6.0.1GA_Eclipse3.3.1_Full.exe,下载安装editplus3.0作为java源程序编辑器,并设置相应的系统环境变量。
1)新建或设置系统环境变量值(path变量名已经存在)如下:
java_home=C:\\Program Files\\Java\\jdk1.6
catalina_home=C:\\apache-tomcat-6.0.18
classpath=.;%java_home%\\lib\\dt.jar;%java_home%\\lib\ools.jar --类路径很重要
path=%path%;%java_home%\\bin;%java_home%\\lib;%catalina_home%\\bin;%catalina_home%\\lib
之后,可以进DOS窗口分别输入javac命令,set命令进行检查。
2)针对editplus编辑器,依次单击Tools―>Preferences…->Files,将default encoding改成UTF-8,将UTF-8 signature改成always remove UTF-8 signature,保存即可。
3)针对Eclipse3.3.1 IDE环境,进行如下设置:
3.1) 添加jdk路径
Menu(主菜单)->Window(窗口)->Preferences(首选项)->java->Installed JREs(已安装的jre),点Add(添加)。
3.2)设置Server
菜单栏中选择“窗口(window)”--“首选项...(preference)” ——“MyEclipse”--“Servers”--“Tomcat”——Tomcat6.x,将Tomcat Server设置为“Enable”,并将“Tomcat Home Directory”设置为Tomcat6.0的安装目录,其他目录选项将会自动生成, 接着选择Tomcat6.x ——JDK,在Tomcat JDK name那选择之前添加的jdk。
3.3)配置默认编码
仍然是菜单栏中的“窗口(window)”--“首选项...(preference)”——“常规(General)”--“内容类型(Content Types)”--“文本(text)”。然后在“缺省编码(Default encoding)”中输入“UTF-8”,点击“更新(Update)”,将“Java类文件(Java Properties File)”及其他字符编码均“更新(Update)”为“UTF-8”。
3.4)设置编辑器字体/输出内容字体
依次进“窗口(window)”--“首选项...(preference)”——“常规(General)”--"皮肤(Appearance)"
Java 实验内容及报告要求
--“颜色和字体(colors and fonts)”--"Java"--“java editor text font(set to default text font)”,再单击“change...”,在上述列表框中选择"part title font"后,可修改编辑器下方的输出面板的标题字体。
依次进“窗口(window)”--“首选项...(preference)”——“常规(General)”--"皮肤(Appearance)"--“颜色和字体(colors and fonts)”--"Debug"--"console font (overrides default: text font)可修改控制台输出内容的字体。
4)在editplus中新建一个简单的java源程序,编辑,文件保存为firstJava.java。然后分别执行如下操作。
4.1)打开一个DOS控制台窗口,进入firstJava.java文件所在目录,然后依次输入命令,
..>javac firstJava.java
..>java firstJava
观察编译是否通过,运行是否通过。
4.2)运行myeclipse6.0.1,之后新建一个java工程,工程名可为chp1,按照向导进行逐级选择,最后单击“完成”。可看到自动生成chp1,src,bin等目录,从文件夹窗口中将4.1)中已建好的firstJava.java文件复制一份到src目录下,切换到myeclipse主界面,右单击chp1,选择“刷新”,可以看到firstJava.java程序添加到工程中了,然后编译、运行这个java程序,直到正确为止。
5)编写一个java源程序,文件名为chp2Basic.java,功能包括建立基本类型和数组类型的变量,对它们进行赋值(包括字符串变量赋值),运算(包括逻辑与&&,&,位与&,常量和变量的无符号右移)以及输出变量值。
6)编写一个java源程序,文件名为chp2Loop.java,功能包括建立一个5×5×5的整型数组,用元素所在的位置序号(从1开始)对所有数组元素进行赋值,然后使用三层循环只对那些任一个下标值对2求余不为0时的数组单元值加1,其余不变,要求使用continue label语法,可以加上break label语法。
三.实验小结
实验二 面向对象编程
一.实验目的及要求
1. 掌握如何创建类和对象
2. 掌握构造方法的执行过程与对象的创建
3. 掌握成员变量的定义,隐藏规则
4. 掌握成员方法的定义,重载以及覆盖规则
5. 掌握java中引入包命名机制和带包名的java程序的运行
6. 掌握抽象类/接口的定义,抽象方法的声明
7. 掌握java中异常处理机制和异常处理方法
二.实验内容及步骤
1. 创建一个飞行器抽象类,名称为absAircraft,并以它为父类分别创建飞机(airplane),鸟(bird),以及超人(superman)三个子类,所有的飞行器类具有不同的尺寸,长L,宽W,高H;不同的体重(weight),速度(speed),体积(volume)。要求以不同的长宽高三个参数构造相应的飞行器对象,将计算飞行器体积的方法在抽象类中实现并返回默认值0.0。它可以起飞,飞行,着陆,将这三个功能在一个接口中声明,接口名为infAircraft,方法名分别为takeoff,fly,landfall,上述三个子类实现这个接口;还可以发出不同的声音,将发出不同声音的方法在抽象类中申明为抽象方法。另外,鸟和超人还会吃(eat)。所有这些功能要求使用类的继承,方法重载和方法覆盖,接口声明和实现予以实现,编写一个主类,名称为oopDemo.java,在main函数中创建三个子对象,并以子类引用变量或父类引用变量方式调用这些方法,观察在各种情形下程序能否正常运行。
要求编写符合java编程规范的源程序以及对程序作必要的注释。
package sn.exp;
public abstract class AbsAircraft {
protected double width = 0.0D;
protected double height = 0.0D;
protected double length = 0.0D;
protected double speed = 0.0D;
protected double weight = 0.0D;
protected double volume = 0.0D;
abstract public void sound();
public AbsAircraft(double width, double height, double length) {
this.width = width;
this.height = height;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getVolume() {
return volume;
}
public void setVolume(double volume) {
this.volume = volume;
}
}
package sn.exp;
import sn.exp.inf.InfAircraft;
public class Airplane extends AbsAircraft implements InfAircraft{
public Airplane(double width, double height, double length) {
super(width, height, length);
}
@Override
public void sound() {
System.out.println("Airplane sound");
}
public void fly() {
System.out.println("Airplane fly");
}
public void landfall() {
System.out.println("Airplane landfall");
}
public void takeoff() {
System.out.println("Airplane takeoff");
}
@Override
public double getVolume() {
return width * height * length;
}
}
package sn.exp;
import sn.exp.inf.InfAircraft;
public class Bird extends AbsAircraft implements InfAircraft{
public Bird(double width, double height, double length) {
super(width, height, length);
}
@Override
public void sound() {
System.out.println("Bird sound");
}
public void fly() {
System.out.println("Bird fly");
}
public void landfall() {
System.out.println("Bird landfall");
}
public void takeoff() {
System.out.println("Bird takeoff");
}
public void eat(){
System.out.println("Bird eat");
}
public double getVolume() {
return width + height + length;
}
}
package sn.exp;
import sn.exp.inf.InfAircraft;
public class Superman extends AbsAircraft implements InfAircraft{
public Superman(double width, double height, double length) {
super(width, height, length);
}
@Override
public void sound() {
System.out.println("Superman sound");
}
public void fly() {
System.out.println("Superman fly");
}
public void landfall() {
System.out.println("Superman landfall");
}
public void takeoff() {
System.out.println("Superman takeoff");
}
public void eat(){
System.out.println("Superman eat");
}
public double getVolume() {
return width + height - length;
}
}
package sn.exp;
import sn.exp.inf.InfAircraft;
public class OopDemo {
public static void main(String[] args) {
Bird bird = new Bird(1.0D, 2.0D, 3.0D);
Airplane airplane = new Airplane(4.0D, 5.0D, 6.0D);
Superman superman = new Superman(7.0D, 8.0D, 9.0D);
AbsAircraft [] absAircrafts = new AbsAircraft[]{bird, airplane, superman};
for(AbsAircraft aircraft : absAircrafts){
System.out.println(aircraft.getVolume());
}
InfAircraft [] infAircrafts = new InfAircraft[]{bird, airplane, superman};
for(InfAircraft infaircraft : infAircrafts){
infaircraft.takeoff();
infaircraft.fly();
infaircraft.landfall();
}
bird.eat();
superman.eat();
}
}
package sn.exp.inf;
public interface InfAircraft {
public void takeoff();
public void fly();
public void landfall();
}
2. 编写一个带包名的java源程序,包名为chp3.test,三个类名分别为类cAccess,cAccess的子类subAccess,以及非子类anotherAccess,且用public修饰类cAccess,注意此时的源程序名必须为cAccess.java。定义在类cAccess中的成员变量a,b,c,d分别用private,default(不修饰),protected和public修饰。在cAccess类和subAccess类中的公共方法access中要求访问这些变量的值,在anotherAccess类的access方法中以新建一个cAccess对象的形式调用它的access方法,最后在anotherAccess类的main方法中新建三个类的对象,并分别调用它们的access方法,注意观察在各种情形下的访问是否成功。另外,在DOS控制台窗口下运行这个带包名的java程序,想想此时待运行的程序名是什么?
package Chp3.test;
public class CAccess {
private double a;
double b;
protected double c;
public double d;
public CAccess(double a, double b, double c, double d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public void access() {
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println();
}
}
package Chp3.test;
public class SubAccess extends CAccess {
public SubAccess(double a, double b, double c, double d) {
super(a, b, c, d);
}
}
package Chp3.test;
public class AnotherAccess {
public static void main(String[] args) {
CAccess ca=new CAccess(1.0,2.0,3.0,4.0);
SubAccess sa=new SubAccess(5.0,6.0,7.0,8.0);
AnotherAccess anotheraccess =new AnotherAccess();
ca.access();
sa.access();
anotheraccess.access();
}
public void access(){
CAccess ca=new CAccess(9.0,10.0,11.0,12.0);
ca.access();
}
}
3. 编写一个包含try-catch-finally语句块的java源程序,文件名为testExceptionProc.java,类名为testExceptionProc,其中方法process接收一个整型参数,该参数对应的实参值分别是1,2,3,在传入值1时,没有任何异常出现;传入值2时,执行一个除法运算,当除数为0时,则抛出ArithmeticException,要捕捉此异常;在传入3时,要求能够捕捉数组下标越界异常,即ArrayIndexOutOfBoundsException,请设计此时的语句块,最后还可以捕捉Exception异常。另外,将上述process方法申明抛出ArithmeticException,ArrayIndexOutOfBoundsException,并由此方法的调用者对这些异常进行处理,请设计实现上述两种情形下的异常处理程序。
public class TestExceptionProc{
public void process(int i) {
int tem;
if(i==1)
tem=1+2;
if(i==2)
tem=2/0;
if(i==3){
int []a=new int[2];
a[4]=4;
}
}
}
public class Main {
public static void main(String[] args) {
TestExceptionProc tep=new TestExceptionProc();
tep.process(1);
try{
tep.process(2);
}catch(ArithmeticException e){
System.out.println("除数为0");
}
try{
tep.process(3);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界");
}
}
}
三.实验小结
实验三 常用系统类的使用,Java多线程编程
一.实验目的及要求
1. 掌握字符串类的使用方法
2. 掌握不同类型的输入输出流类,标准数据流、文件流、数据输入输出流、对象流等
3. 理解多线程机制,掌握线程对象的创建方法
4. 掌握线程调度方法,包括线程优先级,线程等待、睡眠、唤醒、中断、终止方法
5. 理解多线程之间的互斥和同步
二.实验内容及步骤
1. 创建一个基本类型以及对象类型应用的源程序basicType.java,要求使用String类的变量先后指向两个相同的字符串常量,观察这个变量的值和所指向串的值,使用equals方法判定两个串的内容是否相等。使用Scanner类的变量从键盘上接收若干个整型数据,计算其总和并输出,能够处理非数值的情形并允许再次输入下一个数值。使用FileReader类/FileWriter类结合File类的变量执行先将整型、长整型、char和String类型变量值保存到文件test.txt,再输出该文件中的内容,观察此时程序的输入输出。使用FileWriter类/BufferedWriter类向文件test2.txt中写入dobule类型,char类型,以及String类型的变量值,再使用FileReader类/BufferedReader类读出这些变量的值,观察此时程序的输入输出情况。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class BasicType {
public static final String CONSTANT_STRING = "Java String";
public static void main(String[] args) {
// testEqualsString();
// testScanner();
// testfile();
test2Buffered();
}
public static void testEqualsString(){
String stringA = CONSTANT_STRING;
String stringB = CONSTANT_STRING;
System.out.println("stringA == stringB :" + (stringA == stringB));
System.out.println("stringA.equals(stringB):" + (stringA.equals(stringB)));
}
public static void testScanner(){
Scanner s = new Scanner(System.in);
int count = 0;
int sum = 0;
int num = 0;
System.out.print("Input Integers num :" );
num = s.nextInt();
System.out.print("Input "+ num +" Integers;" );
for(count=0;count System.out.print("sum : " + sum); } public static void testfile(){ File fileTest1 = new File("test1.txt"); FileWriter fw = null; try{ fw = new FileWriter(fileTest1); fw.write(String.valueOf(1234)); fw.write(String.valueOf(12345670L)); fw.write('A'); fw.write("String"); } catch(IOException e){ System.out.println(e.getMessage()); } finally{ if(fw != null){ try{ fw.close(); } catch(IOException e){ System.out.println(e.getMessage()); } } } FileReader fr = null; try{ String fileText1 = null; fr = new FileReader(fileText1); int charread; while( (charread = fr.read()) != -1){ System.out.print((char)charread); } } catch(IOException e){ System.out.println(e.getMessage()); } finally{ if(fr != null){ try{ fr.close(); } catch(IOException e){ System.out.println(e.getMessage()); } } } } public static void test2Buffered(){ File filetest1=new File("test2.txt"); FileWriter fw=null; BufferedWriter bw=null; try{ fw =new FileWriter(filetest1); bw =new BufferedWriter(bw); bw.write(String.valueOf(123.4D)); bw.write('a' ); bw.write("string"); bw.flush(); } catch(IOException e){ System.out.println(e.getMessage()); } finally{ if(bw != null){ try{ bw.close(); } catch(IOException e){ System.out.println(e.getMessage()); } } } FileReader fr=null; BufferedReader br = null; try{ fr=new FileReader("filetest1"); br=new BufferedReader (fr); String line = br.readLine(); while( line != null){ System.out.println(line); line=br.readLine(); } } catch(IOException e){ System.out.println(e.getMessage()); } finally{ if(br != null){ try{ br.close(); } catch(IOException e){ System.out.println(e.getMessage()); } } } } } public class test { public static void main(String args[]){ outer: for(int i=1;i<10;i++){ for(int j=1;j<10;j++){ if(j>i){ System.out.println(); continue outer; } System.out.print(" " +(i*j)); } System.out.println(); } } } 2. 创建一个有关线程应用的源程序threadDemo.java,其中两个具有包访问权限的类thread1,thread2分别以两种不同的方式定义,且都重写线程的run方法,每个线程类的run方法中重复10次输出本线程对象的名称,并执行Thread.sleep方法。每个线程类的构造方法均要求带一个String类型的参数,用来指出当前线程对象的名称,然后在主类threadDemo的main方法中分别创建上述两种方式下的线程对象,对于同一种类型的线程类也创建两个不同名称的对象,将这些线程对象都启动,运行程序,观察程序的运行情况。讨论在什么情况下可以使用线程的stop方法,wait方法,yield方法,join方法,notify方法。 3. 有关临界资源的互斥访问问题。分析如下程序段,当两个线程对象在同时使用同一个stack上的对象时,能不能保证数据的有序入栈出栈?如果不行,应该如何修改?并写出修改的理由。 class stack{ int idx=0; char[ ] data = new char[6]; public void push(char c){ data[idx] = c; idx++; } public char pop(){ idx--; return data[idx]; } } 三.实验小结
