77范文网 - 专业文章范例文档资料分享平台

《Java程序设计》参考答案1(4)

来源:网络收集 时间:2019-01-26 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

try{

BufferedReader br=new BufferedReader(new FileReader(articleName)); word=word.toLowerCase();

while((sentence=br.readLine())!=null){ sentence=sentence.toLowerCase(); int index=sentence.indexOf(word); while(index>=0){ count++;

index=sentence.indexOf(word,index+word.length()); } }

}catch(IOException e){} return count; }

public static void main(String args[]){ String article=\ String word=\

WordCount w=new WordCount();

System.out.println(\单词\在文章\中出现的次数为:\ } }

18、参考代码如下: import java.io.*;

public class N_Digital{

public void initNum(int[] num){ if (num == null) {

throw new NullPointerException(\参数不能为null!\ }

for (int i=0;i

num[i]=(int)(Math.random()*40)+10; System.out.print(num[i]+\ } }

public void sortNum(int[] obj){ if (obj == null) {

throw new NullPointerException(\参数不能为null!\ }

int tmp;

for (int i = 0 ;i < obj.length ;i++ ){

//切记,每次都要从第一个开始比。最后的不用再比。 for (int j = 0 ;j < obj.length -i -1 ;j++ ) {

第 16 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

//对邻接的元素进行比较,如果后面的小,就交换 if (obj[j]

public void writeToFile(int[]num,String file){ try{

File f=new File(file);

if(!f.exists())f.createNewFile();

PrintWriter out = new PrintWriter(new FileWriter(file)); for(int i=0;i

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

out.close();

}catch(IOException ioe){ } }

public static void main(String []args){ int n=(int)(Math.random()*40)+10; int num[]=new int[n];

N_Digital nd=new N_Digital(); nd.initNum(num); nd.sortNum(num);

System.out.println();

nd.writeToFile(num,\ } }

19、参考代码如下: import java.io.*;

public class CopyFile {

public static void main(String args[]){ FileInputStream from = null; FileOutputStream to = null; try {

第 17 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

from = new FileInputStream(\ to = new FileOutputStream(\ byte[] buffer = new byte[4096]; int bytes_read;

while ( (bytes_read = from.read(buffer)) != -1) { to.write(buffer, 0, bytes_read); }

from.close(); to.close();

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

}

} }

一个完整的copy文件的参考程序如下: import java.io.*; class CopyFile{

public boolean copy(String fromFileName, String toFileName,boolean override) { File fromFile = new File(fromFileName); File toFile = new File(toFileName);

if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) { return false; }

if (toFile.isDirectory()) {

toFile = new File(toFile, fromFile.getName()); }

if (toFile.exists()) {

if (!toFile.canWrite() || override == false) { return false; } }

else {

String parent = toFile.getParent(); if (parent == null) {

parent = System.getProperty(\ }

File dir = new File(parent);

if (!dir.exists() || dir.isFile() || !dir.canWrite()) { return false; } }

FileInputStream from = null; FileOutputStream to = null;

第 18 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

try {

from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytes_read;

while ( (bytes_read = from.read(buffer)) != -1) { to.write(buffer, 0, bytes_read); }

return true; }

catch (IOException e) { return false; }

finally {

if (from != null) { try {

from.close(); }

catch (IOException e) { } }

if (to != null) { try {

to.close(); }

catch (IOException e) { } } } }

public static void main(String args[]){ CopyFile cf=new CopyFile();

if(cf.copy(\ System.out.println(\文件拷贝成功!\ } else{

System.out.println(\文件未拷贝!\ } } }

20、参考代码如下: import java.io.*;

class ReadFileContent{

public static void main(String args[]){ int i=0;

第 19 页 共 32 页

浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编

版权所有 不得转载 违者必究

try {

BufferedReader br = new BufferedReader(new FileReader(\ String line = br.readLine();

PrintWriter out = new PrintWriter(new FileWriter(\ while(line!=null){ i++;

out.println(i+\ line = br.readLine(); }

br.close(); out.close(); }catch (Exception e) { e.printStackTrace(); } } }

21、参考代码如下: import java.awt.*;

import java.awt.event.*;

public class TestButton extends Frame implements ActionListener{ Button jb1,jb2; TextField jt;

public TestButton() { super(\

jb1=new Button(\ jb2=new Button(\ jt=new TextField(20);

jb1.addActionListener(this); jb2.addActionListener(this); setLayout(new FlowLayout()); add(jt); add(jb1); add(jb2);

setSize(250,100); setVisible(true); }

public void actionPerformed(ActionEvent e){ if(e.getSource()==jb1){

jt.setText(jb1.getActionCommand()); }

if(e.getSource()==jb2){

jt.setText(jb2.getActionCommand()); }

第 20 页 共 32 页

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库《Java程序设计》参考答案1(4)在线全文阅读。

《Java程序设计》参考答案1(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/445641.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: