java 实验报告

更新时间:2024-03-01 23:35:01 阅读量: 综合文库 文档下载

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

实验1 分析成绩单

一、实验目的:掌握字符输入、输出流用法。 二、实验代码: Fenxi:

import java.util.*; public class Fenxi{ public static double getTotalScore(String s){ Scanner scanner=new Scanner(s); scanner.useDelimiter(\ double totalScore=0; while(scanner.hasNext()){ try{ double score=scanner.nextDouble(); totalScore=totalScore+score; } catch(InputMismatchException exp){ String t=scanner.next(); } } return totalScore; } }

AnalysisResult: import java.io.*; import java.util.*;

public class AnalysisResult{ public static void main(String args[]){ File fRead=new File(\ File fWrite=new File(\ try{ Writer out= new FileWriter(fWrite,true);//以尾加方式创建指向文件fWrite的out流 BufferedWriter bufferWrite=new BufferedWriter(out); //创建指向out的bufferWrite流 Reader in=new FileReader(fRead); //创建指向文件fRead的in流 BufferedReader bufferRead=new BufferedReader(in); //创建指向in的bufferRead流 String str=null; while ((str=bufferRead.readLine())!=null){ double totalScore =Fenxi.getTotalScore(str); str=str+\总分:\

System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); } bufferRead.close(); bufferWrite.close(); } catch(IOException e){ System.out.println(e.toString()); } } }

三、结果截图:

四、实验分析:

(1) 改进程序,使得能统计出每个学生的平均成绩。 答:

(2) 现在有如下格式的货物明细(文本格式)goods.txt

品名:电视,length:102 cm,width:89 cm,height:56 cm.

品名:轿车,length:4502 cm,width:178 cm,height:156 cm. 品名:桌子,length:125 cm,width:78 cm,height:68 cm. 答:

CalculateVolume.java import java.io.*; import java.util.*;

public class CalculateVolume{

public static void main(String args[]){ File fRead=new File(\

File fWrite=new File(\ try{Writer out=new FileWriter(fWrite,true);

BufferedWriter bufferWrite=new BufferedWriter(out); Reader in=new FileReader(fRead);

BufferedReader bufferRead=new BufferedReader(in); String str=null;

while((str=bufferRead.readLine())!=null){ double s=Jisuan.getVolume(str); str=str+\体积:\ System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); }

bufferRead.close(); bufferWrite.close(); }

catch(IOException e){

System.out.println(e.toString()); } } }

Jisuan.java

import java.util.*; public class Jisuan{

public static double getVolume(String s){ Scanner scanner=new Scanner(s);

scanner.useDelimiter(\ double volume=1;

while(scanner.hasNext()){ try{

double distance=scanner.nextDouble(); volume=volume*distance; }

catch(InputMismatchException exp){ String t=scanner.next();

}

} }

return volume; }

实验2 统计英文单词

一、实验目的:掌握使用Scanner类解析文件。 二、实验代码: Dictionary.java: import java.util.*;

public class Dictionary{ public static void main(String args[]){ Vector allWord,noSameWord; WordStatistic statistic=new WordStatistic(); statistic.setFileName(\ statistic.wordStatistic();//statistic调用WordStatistic()方法 allWord=statistic.getAllWord(); noSameWord=statistic.getNoSameWord(); System.out.println(\共有\个英文单词\ System.out.println(\有\个互不相同英文单词\ System.out.println(\按字典顺序排列:\ String s[]=new String [noSameWord.size()]; for(int i=0;i

OutputWordMess.java:

import java.util.*;

public class OutputWordMess{ public static void main(String args[]){ Vector allWord,noSameWord; WordStatistic statistic=new WordStatistic(); statistic.setFileName(\ statistic.wordStatistic();//statistic调用WordStatistic()方法 allWord=statistic.getAllWord(); noSameWord=statistic.getNoSameWord(); System.out.println(\共有\个英文单词\ System.out.println(\有\个互不相同英文单词\ System.out.println(\按出现频率排列:\ int count[]=new int[noSameWord.size()]; for(int i=0;i

WordStatistic.java: import java.io.*; import java.util.*;

public class WordStatistic{

Vector allWord,noSameWord; File file=new File(\ Scanner sc=null; String regex; WordStatistic(){ allWord=new Vector(); noSameWord=new Vector();//regex是由空格·数字和符号(!\组成的正则表达式 regex=\ try{ sc=new Scanner(file);//创建指向file的sc sc.useDelimiter(regex); //sc调用useDelimiter(String regex)方法,向参数传递regex } catch(IOException exp){ System.out.println(exp.toString()); } } void setFileName(String name){ file=new File(name); try{ sc=new Scanner(file); sc.useDelimiter(regex); } catch(IOException exp){ System.out.println(exp.toString()); } } public void wordStatistic(){ try{ while(sc.hasNext()){ String word=sc.next(); allWord.add(word); if(!noSameWord.contains(word)) noSameWord.add(word); } } catch(Exception e){} } public Vector getAllWord(){ return allWord; } public Vector getNoSameWord(){ return noSameWord;

} }

三、结果截图:

四、实验分析:

按字典顺序输出全部不相同的单词。 答:

Dictionary.java import java.util.*;

public class Dictionary{

public static void main(String args[]){ Vector allWord,noSameWord;

WordStatistic statistic=new WordStatistic(); statistic.setFileName(\

statistic.wordStatistic();//statistic调用WordStatistic()方法 allWord=statistic.getAllWord();

noSameWord=statistic.getNoSameWord();

System.out.println(\共有\个英文单词\

System.out.println(\有\个互不相同英文单词\ System.out.println(\按字典顺序排列:\ String s[]=new String [noSameWord.size()]; for(int i=0;i

Arrays.sort(s);

for(int i=0;i

实验3 读取压缩文件

一、实验目的:掌握ZipInputStream流的使用。 二、实验代码: import java.io.*;

import java.util.zip.*;

public class ReadZipFile {

public static void main(String args[]) { File f=new File(\ File dir=new File(\ byte b[]=new byte[100]; dir.mkdir(); try{

ZipInputStream in=new ZipInputStream(new FileInputStream(f)); ZipEntry zipEntry=null;

while((zipEntry=in.getNextEntry())!=null) { File file=new File(dir,zipEntry.getName());

FileOutputStream out=new FileOutputStream(file); int n=-1;

System.out.println(file.getAbsolutePath()+\的内容:\ while((n=in.read(b,0,100))!=-1) { String str=new String(b,0,n); System.out.println(str);

out.write(b,0,n); }

out.close(); }

in.close(); }

catch(IOException ee) { System.out.println(ee); } } }

三、结果截图:

四、实验分析:

编写一个GUI程序,提供一个对话框,用户可以使用这个对话框选择要解压缩的Zip文件,设置解压后所得到的文件的存放目录。 答:

import java.io.*;

import java.util.zip.*; import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private JFileChooser fileChooser = new JFileChooser(\ private JButton button = new JButton(\打开想要解压文件\ public MyFrame() { this.setTitle(\解压\ this.setPreferredSize(new Dimension(500, 400)); this.getContentPane().add(button, BorderLayout.CENTER); button.addActionListener(this);

this.pack(); this.setLocationRelativeTo(null); this.setIgnoreRepaint(true); }

public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof JButton) { openFile(); } }

public void openFile() { fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle(\打开Zip文件夹\ int ret = fileChooser.showOpenDialog(null); if (ret == JFileChooser.APPROVE_OPTION) { System.out.println(fileChooser.getSelectedFile().getAbsolutePath()); File f=new File(\ File dir=new File(\ byte b[]=new byte[100]; dir.mkdir(); try{ZipInputStream in=new ZipInputStream(new FileInputStream(f)); ZipEntry zipEntry=null; while((zipEntry=in.getNextEntry())!=null){ File file=new File(dir,zipEntry.getName()); FileOutputStream out=new FileOutputStream(file); int n=-1; System.out.println(file.getAbsolutePath()+\的内容:\ while((n=in.read(b,0,100))!=-1){ String str=new String(b,0,n); System.out.println(str); out.write(b,0,n); } out.close(); } in.close(); } catch(IOException ee){ System.out.println(ee); } } }

public static void main(String[] args) { Frame frame = new MyFrame(); frame.setVisible(true);

}

}

补充

1、用命令行带参的方式实现dir命令的功能,不带参数表示查询当前目录,带参

数表示查询指定的目录,如dir c:\\ ,并支持dir *.java的文件后缀过滤功能。 答:

Dir.java

import java.io.*;

import java.util.*; import java.text.*; public class Dir {

public void GoBack(){

this.dir(System.getProperty(\ }

public void GoBack(String userdir) { if(userdir.equals(\

this.dir(System.getProperty(\ } else {

this.dir(userdir,1); } }

public void dir(String str,int m) { String dir=null; int n;

double Byte=0; dir=str; n=m;

File f=new File(dir); if (!f.exists()) {

System.out.println(\文件或目录不存在!\ System.exit(0); }

while (f.isDirectory()) { File[] dire=f.listFiles();

for (int i=0; i

SimpleDateFormat formatter=new SimpleDateFormat (\h:m\

String date=formatter.format(new Date(dire[i].lastModified())) ; if(m==0){

if(dire[i].getName().endsWith(\ System.out.printf(\\\n\ } } else {

if(dire[i].isFile()){

System.out.printf(\\\n\ } else {

System.out.printf(\\\n\ } } }

break; } }

public static void main(String[] args) throws IOException{ Dir test=new Dir();

String userdir=System.getProperty(\ System.out.print(userdir+\ System.out.print(\

//if(new InputStreamReader(System.in).read()==13){//显示当前目录 // test.GoBack(); //}

Scanner scanner=new Scanner(System.in); userdir=scanner.next();

test.GoBack(userdir); } }

2、给当前的类所在的java源文件添加行号,拷贝到另一个文件temp.txt。 答:

Temp.java

import java.io.*; public class Temp {

public static void main(String[] args) { File file = new File(\ try{

File tempFile = new File(\ FileReader inOne = new FileReader(file);

BufferedReader inTwo = new BufferedReader(inOne ); FileWriter tofile = new FileWriter(tempFile); BufferedWriter out = new BufferedWriter(tofile); int i = 0;

String s = inTwo.readLine(); while (s != null) { i++;

out.write(i + \ out.newLine();

s = inTwo.readLine(); }

inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close(); }

catch (FileNotFoundException e) { e.printStackTrace(); }

catch (IOException e) { e.printStackTrace(); }

System.out.println(\已给当前的类所在的java源文件添加行号,拷贝到temp.txt\ } }

3、编写一个图书类(Book),要求:

① 每本图书数据包括图书ISBN号、图书名、单价,即在类Book中要声明存放这3个数据的成员变量。

② 定义1个构造方法,此构造方法能将当前图书的ISBN号、图书名、单价赋成用户提供的真实数据值,而不是系统默认值。

③ 能用图书ISBN号作为关键属性来判断两本图书是否相等,即在类Book中要重新编写从父类Object继承得到的public boolean equals(Object obj)方法。

④能用图书ISBN号作为关键属性来判断两本图书的大小(即先后顺序),即让类Book实现java.lang.Comparable接口,并实现接口Comparable中的public int compareTo(T o)方法。

⑤ 能用打印图书对象的方式将当前图书的ISBN号、图书名、单价在一行打印,并且每个数据间用Tab间作为分隔。即在类Book中要重新编写从父类Object继承得到的public String toString()方法。

编写上面图书类的测试类(Test.java),要求:

① 编写代码创建封装以下两个图书数据的图书对象,并将这两个对象打印

ISBN 978-7-115-16922-8 978-7-900680-38-9 图书名 Java 程序设计与问题解决 东软Java软件工程师实例参考手册 单价 49.00 58.00 ② 编写代码判断这两个图书是否相等,并根据方法返回结果来判断,打印这两本图书是否相等

③ 编写代码判断这两个图书的大小,并根据方法返回结果来判断,打印这第一本图书是否大于第二本图书

④ 最终控制台的输出结果要求如下: 图书1:

978-7-115-16922-8 Java 程序设计与问题解决 49.00 图书2:

978-7-900680-38-9 东软Java软件工程师实例参考手册 58.00 图书1等于图书2:不相等 图书1大于图书2:大于 答:

Book.java

import java.lang.Comparable;

public class Book implements Comparable{ String isbn; String name; double price; public Book(){ }

public Book(String isbn,String name,double price){ this.isbn=isbn; this.name=name; this.price=price; }

public boolean equals(Object obj){

return isbn.equals(obj); }

public int compareTo(Book o){

return isbn.compareTo(o.isbn); }

public String toString(){

return isbn+\ } }

Test.java

public class Test{

public static void main(String args[]){

Book book1=new Book(\程序设计与问题解决\ Book book2=new Book(\东软Java软件工程师实例参考手册\

System.out.println(\图书1:\\n\ System.out.println(\图书2:\\n\ System.out.print(\图书1等于图书2:\ if(book1.equals(book2)==true) System.out.println(\相等\ else

System.out.println(\不相等\

System.out.print(\图书1大于图书2:\ if(book1.compareTo(book2)<0) System.out.println(\大于\ else

System.out.println(\小于\ } }

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

Top