《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十七章练习题答案.docx

上传人:太** 文档编号:67069797 上传时间:2022-12-23 格式:DOCX 页数:22 大小:49.89KB
返回 下载 相关 举报
《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十七章练习题答案.docx_第1页
第1页 / 共22页
《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十七章练习题答案.docx_第2页
第2页 / 共22页
点击查看更多>>
资源描述

《《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十七章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十七章练习题答案.docx(22页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Java语言程序设计(基础篇)(第10版梁勇著)第十七章 练习题答案17.1import java.io.*; import java.util.*;public class Exercisel7_01 public static void main(String args) throws lOException try (Formatter output = new Formatter(new FileOutputStream(Exercisel7_01.txttrue); )(for (int i = 0; i 100; i+) output.format(%d, (int)(Math.r

2、andom() * 100000); ) ) )17.2import java.io.*;public class Exercisel7_02 public static void main(String args) throws lOException try (DataOutputStream output = new DataOutputStream(new FileOutputStream(Exercisel7_02.dat true); )for (int i = 0; i 100; i+) output.writelnt(int)(Mathrandom() * 100000); )

3、System.out.println(Done);)17.3import java.io.*;class FixedLengthStringlO /* Read fixed number of characters from a Datalnput stream */ public static String readFixedLengthString(int size, Datalnput in) throws lOException / Declare an array of characterschar chars = new charsize;/ Read fixed number o

4、f characters to the arrayfor (int i = 0; i size; i+) charsi = in.readChar();return new String(chars);)/* Write fixed number of characters to a DataOutput stream */ public static void writeFixedLengthString(String s, int size, DataOutput out) throws lOException char chars = new charsize;/ Fill in str

5、ing with characterss.getChars(0j s.length(), chars., 0);/ Fill in blank characters in the rest of the arrayfor (int i = Math.min(s.length(size); i chars.length; i+) charsi=;/ Create and write a new string padded with blank characters out.writeChars(new String(chars);) )17.10import java.io.*;public c

6、lass Exercisel7_10 public static void main(String args) throws Exception / Check usageif (args.length != 2) System.out.println(Usage: java Exercisel7_10 SourceFile numberOfPieces);System.exit(l);BufferedlnputStream input = new BufferedlnputSt ream (new FileInputStream( new File(args0);)(int numberOf

7、Pieces = Integer.parselnt(args1);System.out.printin(File size: + input.available() + bytes);long fileSize = input.available();int splitFileSize = (int)Math.ceil(1.0 * fileSize / numberOfPieces);for (int i = 1; i = numberOfPieces; i+) try (BufferedOutputStream output = new BufferedOutputStream( new F

8、ileOutputStream(new File(args0 + i);)int value;int count = 0;/ What is wrong if these two conditions are placed in a different order?while (count+ splitFile(tfInputFile.getText()Integer.parselnt(tfNumberOfFiles . getText(););public void splitFile(String filename int numberOfPieces) try (Bufferedlnpu

9、tStream input = new BufferedInputStream( new FilcInputStrcam(new Filc(filcnamc);System.out.println(File size: + input.available() + bytes); long fileSize = input.available();int splitFileSize = (int) Math.ceil(l,0 * fileSize / numberOfPieces);for (int i = 1; i = numberOfPieces; i+) try (BufferedOutp

10、utStream output = new BufferedOutputStream( new FileOutputStream(new File(filename + . + i); )int value;int count = 0;/ What is wrong if these two conditions are placed in a different / order?while (count+ splitFileSize & (value = input.read() != -1) output.write(value);) catch (lOException ex) ex.p

11、rintStackTrace();)/* The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String args) launch(args);) )17.12import java.io.BufferedlnputStream;import java.io.BufferedOutputStream;import java.io.File;import jav

12、a.io.Fileinputstream;import java.io.FileOutputStream;public class Exercisel7_12 public static void main(String args) throws Exception / Check usageif (args.length 2) System.out . println(Usage: java Exercisel7_12 SourceFilel SoureFilen TargetFile); System.exit(l);try (/ The last file TargetFile is f

13、or outputBufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(new File(argsargs.length - 1); )(for (int i = 0; i gridPane, hBox);/ Create a scene and place it in the stageScene scene = new Scene(vBox, 400, 120);primarystage.setTitle(Exercisel7_13); / Set the stage title prima

14、rystage.setScene(scene); / Place the scene in the stage primarystage.show(); / Display the stagebtStart.setOnAction(e - j oinFile(tf Input F ile. getText()., Integer.parseInt(tfNumberOfFiles . getText(); ); )public void joinFile(String filename, int numberOfPieces) try (/ The last file TargetFile is

15、 for outputBufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(new File(filename);)(for (int i = 1; i = numberOfPieces; i+) try (BufferedlnputStream input = new BufferedlnputStream( new FileInputStream(new File(filename + i);)int value;while (value = input.read() != -1) outp

16、ut.write(value);)catch (lOException ex) ex.printStackTrace();)/* The main method is only needed for the IDE with limitedJavaFX support. Not needed for running from the command line. */public static void main(String args) launch(args);)17.14import java.util.Scanner;import java.io.*;public class Exerc

17、isel7_14 public static void main(String args) throws Exception Scanner input = new Scanner(System.in);(Enter a file to encrypt:);File inputFile = new File(input.nextLine(); System.out.print(Enter the output file:); File outputFile = new File(input.nextLine();try (BufferedlnputStream in = new Buffere

18、dInputStream( new FilelnputStream(inputFile);BufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(outputFile);)int value;while (value = in.read() != -1) output.write(value + 5);)17.15 import java.util.Scanner;import java.io.*;public class Exercisel7_15 public static void main

19、(String args) throws Exception Scanner input = new Scanner(System.in);System.out.print(Enter an encrypted file:);File inputFile = new File(input.nextLine();System.outprint(Enter the output file:);File outputFile = new File(input.nextLine();BufferedlnputStream in = new BufferedInputStream( new Fileln

20、putStream(inputFile);BufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(outputFile);int value;while (value = in.read() != -1) output.write(value - 5);17.16import java.io.*;import java.util.Scanner;public class Exercisel7_16 public static void main(String args) throws Except

21、ion Scanner input = new Scanner(System.in);System.out.print(Enter a filename:);String filename = input.nextLine();int counts = new int128;try (BufferedlnputStream fileinput = new BufferedlnputStream( new FilelnputStream(new File(filename);)int r;while (r = fileinput.read() != -1 ) counts(byte)r+;)Sy

22、stem.out.printf(H%15s%15snHASCII Code, Counts);for (int i = 0; i counts.length; i+)if (countsi != 0) System.out.printf (%15d%15dn, i, countsi);)17.17import java.io.*;public class Exercisel7_17 public static void main(String args) throws Exception BitOutputStream output = new BitOutputStream(newFile(

23、,Exercisel7_17.dat,);output.writeBit();output.close();System. out.println(Done);)public static class BitOutputStream private FileOutputStream output;private int value;private int count = 0;private int mask = 1; / The bits are all zeros except the last onepublic BitOutputStream(File file) throws lOEx

24、ception output = new FileOutputStream(file);)public void writeBit(char bit) throws lOException count+;value = value 1;if (bit = 1) value = value | mask;if (count = 8) output.write(value);count = 0;)public void writeBit(String bitstring) throws lOException for (int i = 0; i 0) value = value 0) int te

25、mp = dis.readlnt();total += temp;count+;System.out.print(temp + );)System.out.printIn(nCount is + count);System.out.printIn(nTotal is + total);)catch (FileNotFoundException ex) System.out.printIn(File not found);)catch (lOException ex) System.out.println(ex getMessage();)17.4import java.io.*;public

26、class Exercisel7_04 public static void main(String args) throws lOException try (BufferedReader input = new BufferedReader(new FileReader(args0);DataOutputStream output = new DataOutputStream(new FilcOutputStrcam(argsl);)(String line;while (line = input.readLine() != null) output.writeUTF(line);Stri

27、ng result =int mask = 1;for (int i = 7; i = 0; i-) int temp = value i; int bit = temp & mask;result = result + bit;)return result;)17.19import java.io.*;import java.util.Scanner;public class Exercisel7_19 public static void main(String args) throws lOException Scanner input = new Scanner(System.in);

28、System.out.print(Enter a file name:);String filename = input.nextLine();Fileinputstream in = new FileInputStream(filename);int value;while (value = in.read() != -1) System.out . print(getHex(getBits(value);)/* Convert the 8-bit string to a 2-digit hex number */public static String getHex(String bits

29、tring) / Get the first half hex numberint value = (bitstring.charAt(0) - 0,) * 8 +(bitstring.charAt(l) - 0,) * 4 +(bitstring.charAt(2) - 0) *2+ (bitstring.charAt(3) - 0) * 1;String result = + toHexChar(value);/ Get the second half hex number value = (bitstring.charAt(4) - 0) *8+ (bitString.charAt(5)

30、 - 0,) * 4 +(bitstring.charAt(6) - 0)*2+(bitString.charAt(7) - 0) * 1;return result + toHexChar(value);)/* Convert an integer to a single hex digit in a character */ public static char toHexChar(int hexValue) if (hexValue = 0)return (char)(hexValue + 0);else / hexValue = 10return (char)(hexValue - 1

31、0 + 1 A);)public static String getBits(int value) String result =心;int mask = 1;for (int i = 7; i = 0; i-) int temp = value i;int bit = temp & mask;result = result + bit;)return result;)17.20import java.io.*;import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;i

32、mport javafx.scene.control.Button;import javafx.scene.control.Label;import ScrollPane;import javafx.scene.control.TextArea;import javafx.scene.control.TextField;import javafx.scene.layout . BorderPane;import javafx.stage.Stage;public class Exercisel7_20 extends Application private TextField tfFilena

33、me = new TextField();private Button btSave = new Button(Save the change);private TextArea taBits = new TextArea();public void start(Stage primarystage) BorderPane panel = new BorderPane();panel.setLeft(new Label(Enter a file: );panel.setcenter(tfFilename);BorderPane pane = new BorderPane。;pane.setTop(panel);pane.setCenter(new ScrollPane(taBits);pane.setBottom(btSave);BorderPane.setAlignment(btSave, Pos.CENTER);/ Create a scene and place it in the stageScene scene = new Scene(pane 400, 120);primarystage.setTitle(Exercisel7_20); / Set the stage

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁