JAVA习题集(含答案) 下载本文

JAVA习题集

JAVA习题集(含答案)

习题一:

1. 简述Java的发展过程。 2. 什么是软件? 3. 什么叫做源程序? 4. 什么叫做编译?

5. 什么是Java 的byte-codes?它的最大好处是什么?

6. 机器语言程序、高级语言程序以及可表示成Java字节码的程序之间的区别是什么? 7. Java程序可分为哪两种?分别如何运行? 8. 试简述J2SE、J2ME与J2EE的简单区别。 9. 练习使用浏览器查看Java API文档。 10. SDK的编译命令是什么?

11. 试修改例1-1程序,使其输出的字符串为“I’d like to study Java !”,并在DOS命令行

环境下编译与运行该程序。

习题一参考答案 :

1. 答:

1991:Sun Microsystems公司进军消费电子产品(IA)市场。

1991.4:Sun 成立“Green”小组,以C++为基础开发新的程序设计语言,并将其命名

为Oak。

1992.10:Green小组升格为First Person公司,他们将Oak的技术转移到Web上,并

把Oak改名为Java。

1993~1994:Web在Internet上开始流行,致使Java得以迅速发展并获得成功。 1995.5:Sun Microsystems公司正式发表Java与HotJava产品。

1995.10:Netscape与Sun Microsystems合作,在Netscape Nevigator中支持Java。 1995.12:微软(Microsoft)IE加入支持Java的行列。 1996.2:Java Beta测试版结束,Java 1.0版正式诞生。

第1页 共36页

JAVA习题集

1997.2:Java发展至1.1版。Java的第一个开发包JDK(Java Development Kit)发布。 1999.7:Java升级至1.2版。 2000.9:Java升级至1.3版。 2001.7:Java升级至1.4版。

2. 答:软件可以理解为程序的另一种名称。

3. 答:直接使用高级语言书写的程序代码称为源程序

4. 答:把用高级语言编写的源程序翻译成可执行(目标)程序的过程称为编译。

5. 答:Java源程序经Java的编译器编译过后产生的程序称为Java 的byte-codes。它的

最大好处是可以实现跨平台运行。

6. 答:机器语言程序是可以让计算机直接运行的程序;高级语言程序是必需经过编译或

解释才可以让计算机运行的程序,并且程序的运行依赖于该语言的语言处理系统;可表示成Java字节码的程序必需经过编译产生Java字节码程序,Java字节码程序可以实现跨平台运行。

7. 答:Java程序分为:Java Application(Java应用程序)与应用在www上的Java applet

(Java小应用程序)。Java Application可以在Java平台上独立运行,以main() Method作为程序入口,由Java解释器加载执行。而Java applet则是内嵌在Html文件中,必须搭配浏览器来运行,它没有程序入口。

8. 答:J2ME(Java 2 Micro Edition):用于嵌入式Java消费电子平台。不论是无线通讯、

手机、PDA等小型电子装置都可采用其作为开发工具及应用平台。J2SE[Java 2 (Software Development Kit)Standard Edition]:是Java最通行的版本,是用于工作站、PC机的Java标准平台。J2EE(Java 2 Enterprise Edition):可扩展的企业应用Java 2平台,它提供了企业e-Business架构及Web Services服务。

第2页 共36页

JAVA习题集

9. 答:略 10. 答:javac

11. 答:修改程序如下:

public class BegintoLearn {

public static void main(String args[]) {

System.out.println(\ } }

程序运行部分略。

习题二

1. 下面哪些是合法的常量

(1) 2M (2) 4 (3) 8I (4) 6.5 (5) ch3 (6) 5.6E03 (7) \\t (8) –90 (9) 012 (10) 0x14

2. int、char、float和double类型的变量各占有多少个字节?试写出它们各自的取值范围。 3. 下列符号那些可以作为Java的变量名?

(1) 3a (2) main (3) KFC (4) ch.s (5) File (6) a*b (7) m_n (8) new (9) nn2 (10) -x (11) _y (12) exp (13) ha? (14) m&m (15) j,k (16)p:q 4. 什么叫做变量的作用域?

5. 试写出下列Java表达式的运算结果:

(1) 15+4*5-12 (2) (18-4)/7+6 (3) 2>=5

第3页 共36页

JAVA习题集

(4) 6<=6

(5) 5>2 && 8<8 &&23<36 (6) 56/9+3.6 (7) 48%9+5*5-4 (8) 9-7<0 || 11>8 (9) (3>2) ? 8 : 9 (10) 9= =8 && 3<7

6. 假设int m=2,float n=0.1f,经过下列表达式的运算后,m、n各为什么值?

(1) m=9/8; (2) m=45%8+4*5-4; (3) m=36+36*2%m--; (4) m*=5/m-1; (5) n-=5*3.1;

7. 编写一程序,利用System.out.println()方法分别输出下列语句的执行结果:

(1) 将变量m的初值赋值为10,变量n的初值赋值为5。 (2) 变量m的值加3,n的值加5。

(3) 求m和n的平均值,并将该值赋于变量p。 (4) 将m的平方乘以n的平方并赋值给变量q。 请注意各变量定义时所应该使用的数据类型。

8. 已知圆球体积公式为,编写一程序,设计一个求圆球体积的方法,并在主程序中调用

它,求出当r=3时,圆球的体积值。

习题二参考答案:

1. 答:

(1) 不合法 (2) 合法 (3) 不合法 (4) 合法 (5) 不合法 (6)合法 (7) 合法 (8) 合法 (9) 合法 (10) 合法 2. 答:

第4页 共36页

JAVA习题集

变量类型 内存中所占字节数 取值范围

int 4 -231~ (231-1) float 4 3.4e-038 ~ 3.4e+038 double 8 1.7e-308 ~1.7e+308 char 2 0 ~ 65535。

3. 下列符号那些可以作为Java的变量名?

(1) 不合法 (2)合法 (3)合法 (4) 不合法 (5)合法 (6) 不合法 (7) 合法 (8). 不合法 (9)合法 (10). 不合法 (11)合法 (12)合法 (13) 不合法 (14) 不合法 (15) 不合法 (16) 不合法

4. 答:变量的作用域指的是可以访问这一变量的程序代码的范围。 5. 答:

(1) 23 (2) 8 (3) false (4) true (5) false (6) 9.6 (7) 24 (8) true (9) 8 (10)false 6. 答:

(1) 1; (2) 21; (3) 45; (4) –45;

第5页 共36页

JAVA习题集

(5) –15.4;

7. 答:程序如下: public class ProNum {

public static void main(String args[]) {

float p; long q; int m=10,n=5;

System.out.println(\ m=m+3;n=n+5;

System.out.println(\ p=(float)(m+n)/2;

System.out.println(\ q=(m*m*n*n);

System.out.println(\ } }

8. 答:程序如下:

class Sphere { int r; double v;

public void CalVolume() { v=3.14*r*r*r*4/3; }

public void display() { System.out.println(\ }

n=\ n=\ 第6页 共36页

JAVA习题集

}

class SphereClass

{ public static void main(String args[]) { Sphere s = new Sphere(); s.r = 3; s.CalVolume(); s.display(); } }

习题三

1. 什么是结构化程序设计?

2. 使用if语句与使用条件运算符“?:”有什么不同? 3. 使用for循环与while循环的条件有什么不同? 4. 简述break语句与continue的区别?

5. 编写程序:由键盘输入两个字符串“12”与“24”,将它们转换成整数,然后计算并

输出这两个数的和。

6. 编写程序:由键盘输入给出一个百分制成绩,要求输出成绩等级’A’、’B’、’C’和’D’,

90分以上为’A’,75~89为’B’,60~74为’C’,60分以下为’D’。

7. 编一程序,求一个10项所组成的等差数列,其奇数项之和为135,偶数项之和为150。 8. 用for语句输出下列数字金字塔:

1

1 3 1 1 3 5 3 1

1 3 5 7 5 3 1

1 3 5 7 9 7 5 3 1

第7页 共36页

JAVA习题集

9. 编写程序:由键盘输入一正整数,求出小于且等于这个数的所有质数。

10. 编写程序:由键盘输入一整数,求出该数所有的因子,如输入6,则输出的6

的所有因子为1、2、3、6。

11. 假设有一条钢材长2000米,每天截取其中的一半,编一程序求出多少天后,

钢材的长度开始短于5米?

12. 编写程序:利用数列来取得 的近似值。并计算在得到3.14159之前,这个数

列要取到第几项? 13. 使用java.long.Math类,生成10个0~99之间的随机整数,求出它们中的最大

值和最小值。

提示:java.long.Math类支持random方法:Math. Random( )。该方法返回值是正数,类型为

double,范围为0~1。如果要得到其它范围的数,则要进行相应的转换。例如要得到(a,b)之间的整数的方法是:(int) ((b-a+1)*Math.random()+a)。若要得到(0,99)之间的整数可以使用下列语句: int m = (int) (100*Math.random());

14. 编写程序:声明一数组来存放12个月的英文名称,由用户从键盘输入月份,

如输入8,则程序输出相应的月份名称:August。同时请大家考虑若是用户输入了1~12以外的内容,你的程序将如何应对?

15. 编写程序:由键盘输入一16位的长整数,统计0~9这十个数字每一个出现的

次数。

16. 编写程序:将两个各有6个整数的数组,合并成一个由小至大排列的数组(该

数组的长度为12)。

17. 编写程序:以递归的方式实现1+2+3+……+n(n = 200)的计算。

习题三参考答案:

1. 答:具有顺序结构流程、选择结构流程以及循环结构流程这三种结构的程序设计技术

称为结构化程序设计。

2. 答:条件运算符“?:”只是if语句的一种最简单的形式,而if语句则具有更强的功

能,它除了“?:”所表示的单分支结构外,还具有双分支结构以及多分支结构等。

第8页 共36页

JAVA习题集

3. 答:for语句往往在已知循环次数的前提下使用,在循环次数未知时则需使用while语

句进行循环。

4. 答:在循环语句被执行时若遇到break语句则强行结束本轮循环,而遇到continue语句

时将不在执行该语句下面的本次循环的语句,回到循环开始处继续执行下一次循环。

5. 答:程序如下: import java.io.*; public class InNum {

public static void main(String args[]) throws IOException {

int num1,num2; String str;

BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\ str=buf.readLine(); num1=Integer.parseInt(str);

System.out.print(\ str=buf.readLine();

num2=Integer.parseInt(str); System.out.println(\ } }

6. 答:程序如下: import java.io.*; public class Grade {

public static void main(String args[]) throws IOException {

第9页 共36页

JAVA习题集

int score; char ch; String str;

BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\ str=buf.readLine(); score=Integer.parseInt(str); if (score>=90) ch='A'; else if (score>=75) ch='B'; else if (score>=60) ch='C'; else ch='D';

System.out.print(\ } }

7. 答:程序如下: public class ShuLie {

public static void main(String args[]) {

int a ,q ,n; q=(150-135)/5; a=(135-20*q)/5; for (n=0;n<10;n++)

System.out.print((a+n*q)+\ \

第10页 共36页

JAVA习题集

} }

8. 答:程序如下: public class Pyramid {

public static void main(String args[]) { int i,j,k; for(i=0;i<=4;i++)

{ for ( j=0;j<20-i;j++) System.out.print(\ \ for (k=0;k<=2*i;k++) if (k<=i)

System.out.print(\ else

System.out.print(\ System.out.println(); } } }

9. 答:程序如下: import java.io.*;

public class PrimeNumber{

public static void main(String args[]) throws IOException {

int n=0; int m; String str;

BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\ str=buf.readLine();

第11页 共36页

JAVA习题集

m=Integer.parseInt(str);

outer:for(int i=3;i<=m;i+=2){ //outer loop for(int j=2;j

System.out.print(\ \

n++; //outputanewline if(n==10) //after10numbers { System.out.println(); n=0; } }

System.out.println(); } }

10. 答:程序如下: import java.io.*; public class Factor12{

public static void main(String args[]) throws IOException { int m; String str;

BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\ str=buf.readLine();

m=Integer.parseInt(str);

第12页 共36页

JAVA习题集

System.out.print(m+\ \ System.out.println( ); for(int i=1;i<=m;i++) if (m%i==0)

System.out.print(\ \ } }

11. 答:程序如下: public class MSteel{

public static void main(String args[]) {

int d=0; float m=2000; while (m>=5) { m=m/2; d++;

System.out.print(d+\ \ System.out.println(m); }

System.out.print(\ \ days\ } }

12. 程序如下: public class AlmostPi{

public static void main(String args[]) {

int n;

第13页 共36页

JAVA习题集

long m; double s,t; n=1; m=0; s=0; do

{ t=(double)n/(2*m+1); m++; n=-n; s=s+t;

} while (4*s-3.14159> 0.0000001 || 4*s-3.14159< -0.0000001); System.out.println(m); } }

13. 答:程序如下:

public class LSRnd{

public static void main(String args[]) {

int mun,n,max1,min1; max1=0; min1=100;

for (n=1;n<=10;n++)

{ mun=(int)(100*Math.random()); System.out.print(mun+\ \ if (mun>max1) max1=mun; if (mun

第14页 共36页

JAVA习题集

System.out.println();

System.out.println(\ \ System.out.println(\ \ } }

14. 答:程序如下: import java.io.*; public class StrArry {

public static void main(String args[]) throws IOException { int m; String str; String

month[]={\,\ BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\ str=buf.readLine(); m=Integer.parseInt(str); if (m>=1 && m<=12)

System.out.println(month[m-1]); else

System.out.print(\ } }

15. 答:程序如下:

第15页 共36页

JAVA习题集

import java.io.*; public class StatNum {

public static void main(String args[]) throws IOException { int m; char s; String str; int a[]=new int[10]; BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print(\ str=buf.readLine(); for (int i=0;i<=15;i++) { s=str.charAt(i);

m=(int)s-48; // 字符数据转换为整型数据时转换的是其Unicoad代码。 a[m]=a[m]+1; }

for (m=0;m<10;m++)

System.out.println(m+\ \ } }

16. 答:程序如下: import java.io.*; public class SortArray {

public static void main(String args[]) throws IOException {

int m,n,k;

第16页 共36页

JAVA习题集

int aa[]=new int[6]; int bb[]=new int[6]; int cc[]=new int[12];

for (int i=0;i<6;i++) //利用产生随机数的方式为数组赋值。 { m=(int)(100*Math.random()); aa[i]=m;

n=(int)(100*Math.random()); bb[i]=n;

System.out.println(aa[i]+\ \ }

for (int i=0;i<6;i++) //先将两个数组进行排序 for (int j=i;j<6;j++) { if (aa[i]>aa[j])

{int t=aa[i];aa[i]=aa[j];aa[j]=t;} if (bb[i]>bb[j])

{int t=bb[i];bb[i]=bb[j];bb[j]=t;} }

m=0; //用合并法将两个有序数组排序并合并 n=0; k=0;

while (m<6 &&n<6) { if (aa[m]<=bb[n]) { cc[k]=aa[m];m++;} else

{cc[k]=bb[n];n++;} k++; } while (m<6)

{cc[k]=aa[m];m++;k++;} while (n<6)

第17页 共36页

JAVA习题集

{cc[k]=bb[n];n++;k++;} for (int i=0;i<12;i++)

System.out.print(cc[i]+\ \ } }

17. 答:程序如下: class RecuSum{

static long Sum1(int n){ if (n==1) return 1; else

return n+Sum1(n-1); }

public static void main(String args[]) { int n=200;

System.out.println(\ } }

习题四

1类和对象之间的关系。

2声明一个类Person,成员变量有姓名、出生年月、性别。有成员方法以显示姓名、年龄和性别。

3 声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。

第18页 共36页

JAVA习题集

4 声明一个矩阵类Matrix,有这样一些实例方法:将一个矩阵转置、求两个矩阵的和。 5 举例说明类(静态)成员的特点。

6 说明继承和接口的区别已及它们各自的作用。 7 写出下面程序的运行结果 class Leve1{ String name=\ int height,width; public Leve1(){;} public Leve1(Leve1 le) { name=le.name; height=le.height; width=le.width;}

public Leve1(String na,int h,int w) { name=na; height=h; width=w;}

public int higherthen(Leve1 t) { return this.height-t.height;} public void print(){

System.out.println(\

System.out.println(\ }

class Leve2 extends Leve1{ int width; public Leve2(){;}

public Leve2(Leve1 le,int h,int w) { super(le); height=h; width=w;} public int widerthen(){

第19页 共36页

JAVA习题集

return width-super.width;} public void print(){

System.out.println(\

System.out.println(\ }

public class Lev_ex{

public static void main(String args[]){ Leve1 le1=new Leve1(\ Leve2 le2=new Leve2(le1,40,25); le1.print(); le2.print();

System.out.println(\ System.out.println(\

} }

8 Java中的包和windows的文件夹有什么关系? 9 说明类的成员的访问权限和包的关系。

10 创建两个String类的对象str1和str2,判断sr2是否是str1的子串。如果是,输出str1中,在子串ztr2前和后的字符串。如:

“Action”是“addActionListener”的子串,在此子串前是字符串”add”,后面是字符串”Listener”。

11 写出下面程序的运行结果 class Father{

int x;

public Father(int x) {

this.x=x;} public Father(){;} public int fun(){ int f=0;

for(int i=1;i<=x;i++)

第20页 共36页

JAVA习题集

f=f+i; return f;} public int fun(int x) { int f=1;

for(int i=this.x;i<=x;i++) f=f*i; return f;} }

class Son extends Father{

public Son(int a) { super(a);} public int fun(){ int f=1;

for(int i=1;i<=x;i++) f=f*i; return f;} }

public class Pol_ex{

public static void main(String args[]){ Father f1=new Father(5); Son s1=new Son(3); System.out.println(s1.fun()); System.out.println(f1.fun()); System.out.println(f1.fun(6)); f1=s1;

System.out.println(s1.fun()); System.out.println(f1.fun()); System.out.println(f1.fun(6)); }

}

第21页 共36页

JAVA习题集

习题四参考答案:

2、

import java.util.*; public class Person {private String name; private char sex; private int year,month; public Person( ){}

public Person(String nm,char sx,int y,int m) { name=nm; sex=sx; year=y; month=m;}

public void printPerson( )

{Calendar now=Calendar.getInstance(); int age=now.get(Calendar.YEAR)-year;

System.out.println(\ public static void main(String args[]) {Person pe1=new Person(\ pe1.printPerson();} } 3、

public class Rectangle {double width,length,girth,area; public Rectangle(){};

public Rectangle(double wd,double le)

第22页 共36页

JAVA习题集

{ width=wd;length=le;} public void setWidth(double wd) {width=wd;}

public void setLength(double le) {length=le;}

public double getWidth( ) {return width;}

public double getLength( ) {return length;} public double girth() {return 2*(width+length);} public double area() {return width*length;} public void printRectangle()

{ System.out.println(\ public static void main(String args[]) {Rectangle re1=new Rectangle(10,20); Rectangle re2=new Rectangle(); re2.setWidth(3); re2.setLength(4); re1.printRectangle();

System.out.println(\ re2.printRectangle();

System.out.println(\ } 4、

public class Matrix {private int mx[][],m,n; public Matrix(int r,int c)

第23页 共36页

JAVA习题集

{m=r; n=c;

mx=new int[m][n]; iniMatrix();} public Matrix() {m=3; n=3;

mx=new int[3][3]; iniMatrix();} public void iniMatrix() {int i,j;

for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++)

mx[i][j]=(int)(Math.random()*100);} public void tranMatrix() {int i,j,t;

int mt[][]=new int[m][n]; for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++) mt[i][j]=mx[i][j]; t=m; m=n; n=t;

mx=new int[m][n]; for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++) mx[i][j]=mt[j][i];} public void printMatrix() {int i,j;

for(i=0;i<=m-1;i++)

第24页 共36页

JAVA习题集

{ for(j=0;j<=n-1;j++)

System.out.print(\ \ System.out.println();} }

public void addMatrix(Matrix b) {int i,j;

for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++)

mx[i][j]=mx[i][j]+b.mx[i][j]; } public static void main(String args[]) {Matrix ma=new Matrix(4,3); Matrix mb=new Matrix(4,3);

System.out.println(\ matrix_A:\ ma.printMatrix();

System.out.println(\ matrix_B:\ mb.printMatrix();

System.out.println(\ ma.addMatrix(mb); ma.printMatrix();

System.out.println(\ mb.tranMatrix(); mb.printMatrix(); }} 10、

public class Substring

{public static void main(String args[]) {String str1=new String(\ String str2=new String(\ int n;

n=str1.indexOf(str2);

第25页 共36页

JAVA习题集

if(n>=0){System.out.println(\

System.out.println(\ System.out.println(\\ }}

习题五

问答题:

1. 请解释try、catch、finally块之间的关系。

2. try块的嵌套有几种形式?他们对异常分别是怎样处理的? 3. 简述throw,throws语句的作用。

习题五参考答案:

1. try块用来监视某段代码执行过程中是否发生异常,若发生则产生异常对象并抛出。catch用于捕获异常并处理它。finally块中的语句无论是否发生异常都将被执行。

2.有两种形式。一种是显式嵌套,它是指在某个方法中,某一个try块又包含另一try块,当内层try块抛出异常对象时,首先对内层try块的catch语句进行检查,若与抛出异常类型匹配则由该catch处理,否则由外层try块的catch处理。

另一种是隐式嵌套,它是指在不同的方法中,若方法1的try块中调用方法2,而方法2又包含一个try语句,则方法1中的try块为外层,方法2中的try为内层。

3.throw语句用于在程序中自行抛出异常,throw语句执行后,其后继语句将不再执行,执行流程将直接寻找后面的catch语句,如果catch语句中的参数能匹配throw语句抛出的

第26页 共36页

JAVA习题集

Throwable对象,则执行相应的异常处理程序。如果catch语句中的参数没有一个能与该对象相匹配,则由缺省的异常处理器终止程序的执行并输出相应的异常信息。

throws语句用于声明一个方法可能引发的所有异常,这些异常是要求调用该方法的程序处理的。

习题六

问答题:

1 线程有那些特点。

2 什么是线程的生命周期?线程的是怎样在各种状态下转换的。

3 编写一个线程程序,有两各线程,分别在屏幕上显示1~50之间的奇数和偶数。观查一共有几个线程在运行,各个线程是怎样被处理器执行到的。

4 编写程序同上题,利用Java对线程的调度技术,使屏幕上先显示1~50之间的奇数,再显示1~50之间的偶数。

5 一个多线程的程序怎么会出现死锁?

6 编写一个程序,创建两个线程,其中一个产生5个1~100之间的随机整数,另一个线程将这5个数加起来。观察运行的情况,分析结果是否正确。如果结果有问题,想办法解决它。

习题六参考答案:

3.

public class Thread1 extends Thread { int d;

public Thread1(String name,int d) {super(name); this.d=d;} public void run() {int k;

第27页 共36页

JAVA习题集

System.out.println();

System.out.print(getName()+\ \ for(k=d;k<=50;k=k+2) System.out.print(k+\

System.out.println(getName()+\ public static void main(String args[]) {Thread1 th1=new Thread1(\ Thread1 th2=new Thread1(\ th1.start(); th2.start();

System.out.println(\ 4.

public class Thread2 extends Thread { int d;

public Thread2(String name,int d) {super(name); this.d=d;} public void run() {int k;

System.out.println();

System.out.print(getName()+\ \ for(k=d;k<=50;k=k+2) System.out.print(k+\

System.out.println(getName()+\ public static void main(String args[]) {Thread2 th1=new Thread2(\ Thread2 th2=new Thread2(\ th1.start(); th2.setPriority(1); th2.start();

第28页 共36页

JAVA习题集

System.out.println(\ 6.

class Databuf

{private int value,totalvalue; private boolean writeable=true; int sort=0;

synchronized void senddata() {while(!writeable) try

{this.wait();}

catch(InterruptedException e) {System.out.println(e);}

value=(int)(Math.random()*100); writeable=false; notifyAll();}

synchronized int adddata() {while(writeable) try

{this.wait();}

catch(InterruptedException e) {System.out.println(e);} totalvalue=totalvalue+value; writeable=true; notify(); return value;} int getvalue() {return value;} int gettotalvalue() {return totalvalue;} }

第29页 共36页

JAVA习题集

class Send extends Thread {Databuf da;

public Send(Databuf d) {da=d;} public void run() {int i;

synchronized(da) {for(i=1;i<=5;i++) {try {sleep(1);}

catch(InterruptedException e) {System.out.println(e);} da.senddata();

System.out.println(\ }}} }

class Add extends Thread {Databuf da;

public Add(Databuf d) {da=d;} public void run() {int i;

synchronized(da) {for(i=1;i<=5;i++) {try {sleep(1);}

catch(InterruptedException e) {System.out.println(e);} da.adddata(); }

第30页 共36页

JAVA习题集

System.out.println(\ } } }

public class Thread3

{public static void main(String args[]) {Databuf da=new Databuf(); Send s1=new Send(da); Add a1=new Add(da); s1.start(); a1.start(); } }

习题七

习题:

1 有那些数据流和文件读写有关? 2 缓冲流在数据输入输出中的作用。

3 从键盘输入若干个字符,当输入字符”#”时中止输入。统计输入字符的个数,并将它们按输入时的逆序输出。如:

输入:inputstream# 输出:maertstupni

4 用字符输入输出流,将一个文本文件从硬盘某个文件夹复制到另外一个文件夹中。 5 在屏幕上显示c:\\windows目录下扩展名为*.txt的所有文件。

6 在磁盘文件中有10个整数按从小到大的顺序排列。在其中插入一个整数,插入后数据依然有序。

第31页 共36页

JAVA习题集

习题七参考答案:

3、

public class Charstream

{ public static void main(String args[]) {char s[]=new char[100]; int i,d=0;

System.out.println(\ characters:(end with '#') \ i=0; try

{d=System.in.read( ); } catch(Exception e) {System.out.print(e);} while((char)d!='#') {s[i]=(char)d; i=i+1; try

{d=System.in.read(); } catch(Exception e) {System.out.print(e);} }

for(i=i-1;i>=0;i--) System.out.print(s[i]); System.out.println(); }} 4、

import java.io.*; public class Filecopy

{public static void main(String args[]) {int d;

第32页 共36页

JAVA习题集

try

{FileReader rf=new FileReader(\ FileWriter wf=new FileWriter(\ while((d=rf.read())!=-1) wf.write((char)d);

System.out.println(\ rf.close(); wf.close(); }

catch(IOException e) {System.out.println(e);} } } 5、

import java.io.*;

class ListFilter implements FilenameFilter {private String pre=\ public ListFilter(String filterstr) {int i,j;

filterstr=filterstr.toLowerCase(); i=filterstr.indexOf(\ j=filterstr.indexOf(\ if(i>0)

pre=filterstr.substring(0,i); else if(i==-1) if(j>0)

pre=filterstr.substring(0,j-1); if(j>=0)

ext=filterstr.substring(j+1);

第33页 共36页

JAVA习题集

}

public boolean accept(File dir,String filename) { boolean y=true; try

{filename=filename.toLowerCase();

y=filename.startsWith(pre)&filename.endsWith(ext); }

catch(NullPointerException e) {} return y;} }

public class File_filter

{public static void main(String args[]) {File f=new File(\ ListFilter ls=new ListFilter(\ System.out.println(f.getAbsolutePath()); String str[]=f.list(ls); for(int i=0;i

import java.io.*; public class Insert

{public void InserttoFile(RandomAccessFile rwf,int d) {int i,j,k,t; try{ rwf.seek(0); for(i=1;i<=10;i++) { rwf.seek((i-1)*4);

if(d

第34页 共36页

JAVA习题集

for(k=10;k>=i;k--) {rwf.seek((k-1)*4); t=rwf.readInt(); rwf.seek(k*4); rwf.writeInt(t);} rwf.seek((i-1)*4); rwf.writeInt(d); }

catch(EOFException e){} catch(IOException e){} }

public void showdata(RandomAccessFile rwf) {int i=0,d=0; try

{rwf.seek(i*4); while(true) {d=rwf.readInt(); System.out.print(d+\ i++;

rwf.seek(i*4);} }

catch(EOFException e){} catch(IOException e){} }

public static void main(String args[]) {try{

Insert ra=new Insert();

RandomAccessFile rwf=new RandomAccessFile(\ System.out.println(\

第35页 共36页

JAVA习题集

ra.showdata(rwf);

System.out.println(\ ra.InserttoFile(rwf,53); ra.showdata(rwf); rwf.close();}

catch(FileNotFoundException e){} catch(IOException e){}} }

第36页 共36页