北科大Java设计实验报告3.docx

上传人:b****5 文档编号:5777207 上传时间:2023-01-01 格式:DOCX 页数:21 大小:487.01KB
下载 相关 举报
北科大Java设计实验报告3.docx_第1页
第1页 / 共21页
北科大Java设计实验报告3.docx_第2页
第2页 / 共21页
北科大Java设计实验报告3.docx_第3页
第3页 / 共21页
北科大Java设计实验报告3.docx_第4页
第4页 / 共21页
北科大Java设计实验报告3.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

北科大Java设计实验报告3.docx

《北科大Java设计实验报告3.docx》由会员分享,可在线阅读,更多相关《北科大Java设计实验报告3.docx(21页珍藏版)》请在冰豆网上搜索。

北科大Java设计实验报告3.docx

北科大Java设计实验报告3

 

《Java程序设计》上机题(三)

1.编写一个程序,分别统计文本文件中元音字母a、e、i、o、u的个数。

请将源程序文本填入下框:

//infile.txt

abcdefghigklmnopqrstu,

abcdefghigklmnopqrst,

abcdefghigklmnpqrst,

abcdefghgklmnpqrst,

abcdfghgklmnpqrst

//StatisticLetter.java

importjava.io.*;

publicclassStatisticLetter{

publicstaticvoidmain(String[]args){

BufferedReaderin;

inta=0;

inte=0;

inti=0;

into=0;

intu=0;

intc;

try{

in=newBufferedReader(newFileReader("infile.txt"));

while((c=in.read())!

=-1)

{

charc1=(char)c;

if(c1=='a')

{

a+=1;

}

elseif(c1=='e')

{

e+=1;

}

elseif(c1=='i')

{

i+=1;

}

elseif(c1=='o')

{

o+=1;

}

elseif(c1=='u')

{

u+=1;

}

System.out.print(c1);

}

in.close();

System.out.println("\na出现"+a+"次"+

"\ne出现"+e+"次"+

"\ni出现"+i+"次"+

"\no出现"+o+"次"+

"\nu出现"+u+"次");

}catch(IOExceptione1){

e1.printStackTrace();

}

}//endmethodmain

}//endclassStatisticLetter

运行结果截图:

2.编写一个程序,从命令行参数中获得文件名,读入该文件,统计该文件的行数。

注意处理各种可能出现的异常情况。

请将源程序文本填入下框:

//ReadLineNumber.java

importjava.io.*;

publicclassReadLineNumber{

publicstaticvoidmain(String[]args){

BufferedReaderin;

Stringss;

intn=0;

try{

in=newBufferedReader(newFileReader(args[0]));

ss=in.readLine();

while(ss!

=null){

n+=1;

ss=in.readLine();

}

in.close();

System.out.println("文件"+args[0]+"共有"+n+"行");

}catch(ArrayIndexOutOfBoundsExceptione1){

System.out.println("请输入文件名!

");

}catch(FileNotFoundExceptione2){

System.out.println("No"+""+args[0]);

}catch(IOExceptione3){

e3.printStackTrace();

}

}//endmethodmain

}//endclassReadLineNumber

运行结果截图:

3.将两个文本文件的内容合并到另一个文本文件中。

请将源程序文本填入下框:

//CombineText.java

importjava.io.*;

publicclassCombineText{

publicstaticvoidmain(String[]args){

try{

//创建新文件t1.txt

Filedir=newFile("myDir");

if(!

dir.exists())

dir.mkdir();

FilemyFile=newFile(dir,"t1.txt");

//将infile1.txt的内容读入到t1.txt

BufferedWriterbr1=newBufferedWriter(newFileWriter(myFile,true));

BufferedReaderin1=newBufferedReader(newFileReader("infile1.txt"));

Stringtemp1=in1.readLine();

while((temp1!

=null)&&(temp1!

="")){

Stringtemp3=temp1+"\n";

System.out.println(temp3);

br1.write(temp3);

temp1=in1.readLine();

}

br1.close();

in1.close();

//将infile2.txt的内容读入到t1.txt

BufferedWriterbr2=newBufferedWriter(newFileWriter(myFile,true));

BufferedReaderin2=newBufferedReader(newFileReader("infile2.txt"));

Stringtemp2=in2.readLine();

while((temp2!

=null)&&(temp2!

="")){

Stringtemp4=temp2+"\n";

System.out.println(temp4);

br2.write(temp4);

temp2=in2.readLine();

}

br2.close();

in2.close();

//合并成功输出反馈信息

System.out.println("Thenewfileiscreated!

");

}catch(IOExceptionioe){

System.err.println(ioe);

}

}//endmethodmain

}//endclassCombineText

运行结果截图:

4.编写一个程序在程序所在目录下创建以下目录:

Root、Root\Branch1、Root\Branch2;然后在Root\Branch1中创建两个空文件Leaf1.txt和Leaf2.txt。

请将目录和文件操作的相关信息保存在文本文件log.txt中。

请将源程序文本填入下框:

//CreateBranch.java

importjava.io.*;

publicclassCreateBranch{

BufferedWriterbr;

BufferedReaderfin;

publicstaticvoidmain(String[]args){

//新建文件目录

Filedir1=newFile("Root");

if(!

dir1.exists())

dir1.mkdir();

Filedir2=newFile("Root\\Branch1");

dir2.mkdir();

Filedir3=newFile("Root\\Branch2");

dir3.mkdir();

//新建文件

FilemyFile1=newFile(dir2,"Leaf1.txt");

FilemyFile2=newFile(dir2,"Leaf2.txt");

Stringtemp=dir1.getAbsolutePath()+"\n"

+dir2.getAbsolutePath()+"\n"

+dir3.getAbsolutePath()+"\n"

+myFile1.getAbsolutePath()+"\n"

+myFile2.getAbsolutePath()+"\n";

try{

myFile1.createNewFile();//创建文件Leaf1.txt

myFile2.createNewFile();//创建文件Leaf2.txt

Filef1=newFile("log.txt");//创建文件log.txt

//将文件信息输入到log.txt

BufferedWriterbr1=newBufferedWriter(newFileWriter(f1,true));

System.out.println(temp);

br1.write(temp);

br1.close();

//创建成功输出反馈信息

System.out.println("创建成功!

");

}catch(IOExceptione){

e.printStackTrace();

}

}//endmethodmain

}//endclassCreateBranch

运行结果截图:

5.队列是一种“先进先出”的数据结构,数据进入队列时只能排在队尾,从队列中取数据时只能从队头读取。

请用java语言实现队列类Queue,其中包含inQueue、outQueue、readFront、showQueue、emptyQ、fullQ、length方法,分别为入队操作、出队操作、读取队首元素、显示队列中所有数据、判断队空、判断对满、获取队列长度。

新建Queue类的对象,进行以上各种操作。

请将源程序文本填入下框:

//QueueInheritance.java

importjava.io.*;

//结点类

classNode{

publicintelement;

publicNodelink;

publicNode()//无参构造函数

{

link=null;

}

publicNode(intnewelement,Nodenewlink)//带参构造函数

{

element=newelement;

link=newlink;

}

publicvoidsetElement(intnewelement)//设置当前结点的数据元素

{

element=newelement;

}

publicintgetElement()//取当前结点的数据元素

{

returnelement;

}

publicvoidsetLink(Nodenewlink)//设置下一个结点的对象引用

{

link=newlink;

}

publicNodegetlink()//取下一个结点的对象引用

{

returnlink;

}

}

//队列类

classQueue{

publicNodefront;

publicNoderear;

publicintsize;

privateintmaxSize;

publicQueue(ints)//有参构造函数

{

maxSize=s;//为队列定义最大空间

front=rear=newNode();

}

publicvoidinQueue(intnumber)throwsException//入队操作

{

if(fullQ())

thrownewException("队列已满!

");

else

{

rear.setLink(newNode(number,rear.link));

rear=rear.link;

size++;

}

}

publicintoutQueue()throwsException//出队操作

{

if(emptyQ())

thrownewException("队列已空!

");

else{

inttemp;

temp=front.link.getElement();

front.setLink(front.link.link);

size--;

returntemp;

}

}

publicintreadFront()throwsException//读取队首元素

{

if(emptyQ())

thrownewException("队列已空!

");

else{

returnfront.link.getElement();

}

}

publicvoidshowQueue()//显示队列中所有数据

{

NodeCuFront=newNode();

CuFront=front;

for(inti=0;i

{

System.out.print(CuFront.link.getElement()+"");

CuFront.setLink(CuFront.link);

CuFront=CuFront.link;

}

}

publicbooleanemptyQ()//判断队空否

{

if(size==0)

returntrue;

else

returnfalse;

}

publicbooleanfullQ()//判断队满否

{

if(maxSize==size)

returntrue;

else

returnfalse;

}

publicintlength()//获取队列长度

{

returnsize;

}

}

 

publicclassQueueInheritance{

publicstaticvoidmain(String[]args)throwsIOException{

BufferedReaderInput=newBufferedReader(newInputStreamReader(System.in));

StringnumberString;

System.out.println("请输入队列的长度:

");

numberString=Input.readLine();

ints=Integer.parseInt(numberString);

Queuequeue=newQueue(s);

System.out.println("请输入入队的数据:

");

try{

for(intj=0;j

{

numberString=Input.readLine();

intnumber=Integer.parseInt(numberString);

queue.inQueue(number);//数据入队

}

System.out.println("队列中的数据为:

");

queue.showQueue();//显示队列中所有数据

System.out.println();

System.out.println("出队列两个数据为:

");

System.out.println(queue.outQueue()+""+queue.outQueue()+"\n");//数据出队

System.out.println("此时队列中的数据为:

");

queue.showQueue();//显示队列中剩余数据

System.out.println();

System.out.println("此时队首数据为:

"+"\n"+queue.readFront());

System.out.println("此时队列的长度为:

"+"\n"+queue.length());

System.out.println("队列空否?

"+"\n"+queue.emptyQ());

System.out.println("队列满否?

"+"\n"+queue.fullQ());

}catch(Exceptione)

{

System.out.println(e.getMessage());

}

}//endmethodmain

}//endclassQueueInheritance

运行结果截图:

6.堆栈Stack是java.util库中的工具类。

请生成n个1到10之间的随机整数,n值由命令行参数给出。

请打印出这n个数,并进行如下操作:

如果是奇数则将该数压入堆栈;如果是偶数并且栈不为空,则将栈顶元素出栈;如果是偶数但栈为空,则忽略该数。

请给出程序和操作结果。

请将源程序文本填入下框:

//StackInheritance.java

importjava.util.Stack;

importjava.util.Random;

publicclassStackInheritance{

publicstaticvoidmain(String[]args){

try{

intn=Integer.parseInt(args[0]);//n值由命令行参数给出

if(n<=0)

{

thrownewException("请输入一个正整数!

");

}

intnum[]=newint[n];

Randomrand=newRandom();

Stackstack=newStack();

for(inti=0;i

{

num[i]=rand.nextInt(10)+1;//生成1-10之间的随机整数

System.out.println("第"+(i+1)+"个随机数:

"+num[i]);//打印出随机数

if(num[i]%2==1)

{

Strings=Integer.toString(num[i]);

stack.push(s);//将随机数压入堆栈

}

else

{

if(!

stack.empty())

System.out.println(stack.pop());//栈顶元素出栈

else

continue;//忽略该数,继续下一个随机数

}

}

}catch(ArrayIndexOutOfBoundsExceptione1){

System.out.println("没有参数或参数不足!

");

}catch(NumberFormatExceptione2){

System.out.println("输入的不是数字!

");

}catch(Exceptione){

e.printStackTrace();

}

}//endmethodmain

}//endclassStackInheritance

运行结果截图:

7.新建Book类,包括书名、书号、价格、字数等属性、toString()方法,以及构造函数。

请新建包含Book对象的集合,按照书号进行排序并输出排序之后的结果。

请将源程序文本填入下框:

//BookNumber.java

importjava.util.*;

classBook{

StringbookName,

bookNumber,

bookPrice,

bookWordsNum;

publicBook(Stringname,Stringnumber,Stringprice,StringwordsNum)

{

bookName=name;

bookNumber=number;

bookPrice=price;

bookWordsNum=wordsNum;

}

publicStringgetNumber()

{

returnbookNumber;

}

publicStringtoString()

{

Stringoutput;

output="书名:

"+bookName+"书号:

"+bookNumber

+"价格"+bookPrice+"字数"+bookWordsNum;

returnoutput;

}

}

publicclassBookNumber{

publicstaticvoidmain(String[]args){

Mapmap=newTreeMap();//利用TreeMap对键的自动排序功能

Book[]b=newBook[5];

b[0]=newBook("高等数学","105","20","3万");

b[

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1