java第四次实验报告

更新时间:2023-10-03 10:39:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

南京信息工程大学 实验(实习)报告

实验课程 java程序设计 实验名称 第四次实验 实验日期 2016-4-25 指导老师 专业 年级 姓名 学号 得分

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

实验目的:对java的接口,包与泛型以及字符串处理的相关练习。 实验内容:

对接口,包与泛型相关练习:

1、 编程证明:接口内的数据成员被自动设置为 static 和 final。 程序内容:

package staticandfinal;

/*

* staticAndFinal.java */ interface inter{ int x=2;

}

class derived implements inter{ }

public class StaticAndFinal{

public static void main(String args[]){ inter in=new derived();

// in.x=100; // 不能赋值

System.out.println(in.x+\ \ }

}

运行结果: run: 2 2

成功构建 (总时间: 0 秒)

2、 编写一个类,它具有一个 protected 数据成员。在同一个文件内再编写第二个类,在这

个类内编写一个方法,以操作第一类内的 protected 数据成员。 程序内容: package pro;

/*

* Pro.java */

class class1{

protected int arg;

}

class class2{

public void function1(){

// System.out.println(class1.arg); // 不能操作该变量 System.out.println(\调用结束!\ } }

public class Pro {

public static void main(String[] args) { class1 c1 = new class1(); class2 c2 = new class2();

c2.function1(); } }

运行结果: run:

调用结束!

成功构建 (总时间: 0 秒)

3、 采用 public、private、protected 以及默认等类型创建一个类,然后定义这个类的一个对

象。观察在访问所有类成员时会出现哪种类型的编译错误。 程序内容:

package testclass;

class Test{

public String pub; private String pri; protected String prot; String fri;

Test(){

this.pub = \ this.pri = \ this.prot = \ this.fri = \ } }

public class Testclass {

public static void main(String[] args) {

Test tmp = new Test();

System.out.println(tmp.pub); System.out.println(tmp.prot); System.out.println(tmp.fri); System.out.println(tmp.pri); } }

运行结果: run: public protected friendly

Exception in thread \可以在testclass.Test中访问private at testclass.Testclass.main(Testclass.java:28) Java Result: 1

成功构建 (总时间: 1 秒)

4、 编写一个类,它具有public、private、protected 以及默认等类型的成员,将这个类存放

在某个包中。 在另外一个包内再写第二个类,在此类内编写一个方法,以操作第一类内的各个数据成员,观察在访问所有类成员时会出现哪种类型的编译错误。 程序内容:

包Testclass1中: package Testclass1;

public class Testclass1{ public String pub; private String pri; protected String prot; String fri;

public Testclass1(){

this.pub = \ this.pri = \ this.prot = \ this.fri = \ } }

包Testclass2中: package testclass2;

import Testclass1.*;

public class Testclass2 {

public static void main(String[] args) { Testclass1 tmp;

tmp = new Testclass1(); System.out.println(tmp.pub); System.out.println(tmp.pri); System.out.println(tmp.prot); System.out.println(tmp.fri); } }

运行结果: run: public

Exception in thread \可以在Testclass1.Testclass1中访问private at testclass2.Testclass2.main(Testclass2.java:17) Java Result: 1

成功构建 (总时间: 1 秒)

对字符串处理相关练习:

1、 编写一个采用随机函数生成句子的游戏。现有4个字符串数组:article、noun、verb、

preposition,它们的内容分别是: the、a 、one 、some、any ; boy、girl、dog、town、car ;

drove、jumped、ran、walked、skipped和 to、from、over、under、on。 依照句法要求:article + noun + verb + preposition + article + noun,编写程序以产生 20 个句子。 程序内容:

package sentence;

import java.util.* ; import java.awt.*; import java.applet.*;

public class Sentence extends Applet { Random r=new Random ();

String list[][]={{\ {\

{\ {\

{\ {\

TextArea output; Button m; int b,c;

public void init() {

output=new TextArea(25,50); m=new Button(\开始\ add(output); add(m); }

public boolean action(Event e,Object o){ output.setText (\

if(e.target==m){

for(int i=0;i<20;i++){

output.appendText((i+1)+\ for(int j=0;j<6;j++){

c=(int)(5*r.nextFloat ());

output.appendText((list[j][c])+\ }

output.appendText(\ } }

return true; } }

运行结果截图:

本文来源:https://www.bwwdw.com/article/53od.html

Top