ImageVerifierCode 换一换
格式:DOCX , 页数:21 ,大小:487.01KB ,
资源ID:5777207      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5777207.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(北科大Java设计实验报告3.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

1、北科大Java设计实验报告3Java程序设计上机题(三)1.编写一个程序,分别统计文本文件中元音字母a、e、i、o、u的个数。请将源程序文本填入下框:/infile.txtabcdefghigklmnopqrstu,abcdefghigklmnopqrst,abcdefghigklmnpqrst,abcdefghgklmnpqrst,abcdfghgklmnpqrst/StatisticLetter.javaimport java.io.*;public class StatisticLetter public static void main(String args) BufferedRea

2、der in; int a = 0; int e = 0; int i = 0; int o = 0; int u = 0; int c; try in = new BufferedReader(new FileReader(infile.txt); while (c = in.read() != -1) char c1 =(char)c; if(c1 = a) a += 1; else if(c1 = e) e += 1; else if(c1 = i) i += 1; else if(c1 = o) o += 1; else if(c1 = u) u += 1; System.out.pr

3、int( c1 ); in.close(); System.out.println(na出现 + a + 次 + ne出现 + e + 次 + ni出现 + i + 次 + no出现 + o + 次 + nu出现 + u + 次 ); catch (IOException e1) e1.printStackTrace(); /end method main/end class StatisticLetter运行结果截图:2.编写一个程序,从命令行参数中获得文件名,读入该文件,统计该文件的行数。注意处理各种可能出现的异常情况。请将源程序文本填入下框:/ReadLineNumber.javaimp

4、ort java.io.*;public class ReadLineNumber public static void main(String args) BufferedReader in; String ss; int n =0; try in = new BufferedReader(new FileReader(args0); ss = in.readLine(); while (ss != null) n += 1; ss = in.readLine(); in.close(); System.out.println(文件 + args0 + 共有 + n +行); catch (

5、ArrayIndexOutOfBoundsException e1) System.out.println(请输入文件名!); catch (FileNotFoundException e2) System.out.println(No + + args0); catch (IOException e3) e3.printStackTrace(); /end method main/end class ReadLineNumber运行结果截图:3.将两个文本文件的内容合并到另一个文本文件中。请将源程序文本填入下框:/CombineText.javaimport java.io.*;public

6、 class CombineText public static void main(String args) try /创建新文件t1.txt File dir = new File(myDir); if (!dir.exists() dir.mkdir(); File myFile = new File(dir, t1.txt); /将infile1.txt的内容读入到t1.txt BufferedWriter br1 = new BufferedWriter(new FileWriter(myFile, true); BufferedReader in1 = new BufferedRe

7、ader(new FileReader(infile1.txt); String temp1 = in1.readLine(); while (temp1 != null) & (temp1 != ) String temp3 =temp1 + n; System.out.println(temp3); br1.write(temp3); temp1 = in1.readLine(); br1.close(); in1.close(); /将infile2.txt的内容读入到t1.txt BufferedWriter br2 = new BufferedWriter(new FileWrite

8、r(myFile, true); BufferedReader in2 = new BufferedReader(new FileReader(infile2.txt); String temp2 = in2.readLine(); while (temp2 != null) & (temp2 != ) String temp4 = temp2 + n; System.out.println(temp4); br2.write(temp4); temp2 = in2.readLine(); br2.close(); in2.close(); /合并成功输出反馈信息 System.out.pri

9、ntln(The new file is created!); catch(IOException ioe) System.err.println(ioe); /end method main/end class CombineText运行结果截图:4.编写一个程序在程序所在目录下创建以下目录:Root、RootBranch1、RootBranch2;然后在RootBranch1中创建两个空文件Leaf1.txt和Leaf2.txt。请将目录和文件操作的相关信息保存在文本文件log.txt中。请将源程序文本填入下框:/CreateBranch.javaimport java.io.*;publ

10、ic class CreateBranch BufferedWriter br; BufferedReader fin; public static void main(String args) /新建文件目录 File dir1 = new File(Root); if (!dir1.exists() dir1.mkdir(); File dir2 = new File(RootBranch1); dir2.mkdir(); File dir3 = new File(RootBranch2); dir3.mkdir(); /新建文件 File myFile1 = new File(dir2,

11、 Leaf1.txt); File myFile2 = new File(dir2, Leaf2.txt); String temp = dir1.getAbsolutePath() + n + dir2.getAbsolutePath() + n + dir3.getAbsolutePath() + n + myFile1.getAbsolutePath() + n + myFile2.getAbsolutePath() + n; try myFile1.createNewFile();/创建文件Leaf1.txt myFile2.createNewFile();/创建文件Leaf2.txt

12、 File f1 = new File(log.txt);/创建文件log.txt /将文件信息输入到log.txt BufferedWriter br1 = new BufferedWriter(new FileWriter(f1, true); System.out.println(temp); br1.write(temp); br1.close(); /创建成功输出反馈信息 System.out.println(创建成功!); catch(IOException e) e.printStackTrace(); /end method main/end class CreateBranc

13、h运行结果截图: 5.队列是一种“先进先出”的数据结构,数据进入队列时只能排在队尾,从队列中取数据时只能从队头读取。请用java语言实现队列类Queue,其中包含inQueue、outQueue、readFront、showQueue、emptyQ、fullQ、length方法,分别为入队操作、出队操作、读取队首元素、显示队列中所有数据、判断队空、判断对满、获取队列长度。新建Queue类的对象,进行以上各种操作。请将源程序文本填入下框:/QueueInheritance.javaimport java.io.*;/结点类class Node public int element; public

14、 Node link; public Node() /无参构造函数 link = null; public Node(int newelement,Node newlink) /带参构造函数 element = newelement; link = newlink; public void setElement(int newelement) /设置当前结点的数据元素 element = newelement; public int getElement() /取当前结点的数据元素 return element; public void setLink(Node newlink) /设置下一个

15、结点的对象引用 link = newlink; public Node getlink() /取下一个结点的对象引用 return link; /队列类class Queue public Node front; public Node rear; public int size; private int maxSize; public Queue(int s) /有参构造函数 maxSize = s; /为队列定义最大空间 front = rear = new Node(); public void inQueue(int number) throws Exception /入队操作 if(

16、fullQ() throw new Exception(队列已满!); else rear.setLink(new Node(number,rear.link); rear = rear.link; size+; public int outQueue() throws Exception /出队操作 if(emptyQ() throw new Exception(队列已空!); else int temp; temp = front.link.getElement(); front.setLink(front.link.link); size-; return temp; public in

17、t readFront()throws Exception /读取队首元素 if (emptyQ() throw new Exception(队列已空!); else return front.link.getElement(); public void showQueue() /显示队列中所有数据 Node CuFront = new Node(); CuFront=front; for(int i = 0; i size; i+) System.out.print(CuFront.link.getElement()+ ); CuFront.setLink(CuFront.link); Cu

18、Front=CuFront.link; public boolean emptyQ() /判断队空否 if(size = 0) return true; else return false; public boolean fullQ() /判断队满否 if(maxSize = size) return true; else return false; public int length() /获取队列长度 return size; public class QueueInheritance public static void main(String args) throws IOExcept

19、ion BufferedReader Input=new BufferedReader(new InputStreamReader(System.in); String numberString; System.out.println(请输入队列的长度:); numberString=Input.readLine(); int s =Integer.parseInt(numberString); Queue queue = new Queue( s ); System.out.println(请输入入队的数据:); try for(int j=0;js;j+) numberString=Inp

20、ut.readLine(); int number=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(此时队列中的数据为:); qu

21、eue.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(Exception e) System.out.println(e.getMessage(); /e

22、nd method main/end class QueueInheritance运行结果截图:6.堆栈Stack是java.util库中的工具类。请生成n个1到10之间的随机整数,n值由命令行参数给出。请打印出这n个数,并进行如下操作:如果是奇数则将该数压入堆栈;如果是偶数并且栈不为空,则将栈顶元素出栈;如果是偶数但栈为空,则忽略该数。请给出程序和操作结果。请将源程序文本填入下框:/StackInheritance.javaimport java.util.Stack;import java.util.Random;public class StackInheritance public s

23、tatic void main(String args) try int n =Integer.parseInt(args0); /n值由命令行参数给出 if(n = 0 ) throw new Exception(请输入一个正整数!); int num = new intn; Random rand = new Random(); Stack stack = new Stack(); for(int i = 0; i n; i+) numi = rand.nextInt(10)+1; /生成1-10之间的随机整数 System.out.println(第 + (i+1) + 个随机数: +n

24、umi); /打印出随机数 if(numi%2 = 1) String s = Integer.toString(numi); stack.push(s); /将随机数压入堆栈 else if(!stack.empty() System.out.println(stack.pop();/栈顶元素出栈 else continue; /忽略该数,继续下一个随机数 catch (ArrayIndexOutOfBoundsException e1) System.out.println(没有参数或参数不足!); catch (NumberFormatException e2) System.out.p

25、rintln(输入的不是数字!); catch(Exception e) e.printStackTrace(); /end method main/end class StackInheritance 运行结果截图:7.新建Book类,包括书名、书号、价格、字数等属性、toString()方法,以及构造函数。请新建包含Book对象的集合,按照书号进行排序并输出排序之后的结果。请将源程序文本填入下框:/BookNumber.javaimport java.util.*;class Book String bookName, bookNumber, bookPrice, bookWordsNum

26、; public Book(String name, String number, String price, String wordsNum) bookName = name; bookNumber = number; bookPrice = price; bookWordsNum = wordsNum; public String getNumber() return bookNumber; public String toString() String output; output = 书名: + bookName + 书号: + bookNumber + 价格 + bookPrice + 字数 + bookWordsNum; return output; public class BookNumber public static void main(String args) Map map = new TreeMap();/利用TreeMap对键的自动排序功能 Book b = new Book5; b0 = new Book(高等数学, 105, 20, 3万); b

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

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