Chapter 13


Exercise 1 In Chapter 13, you learned about the following line:

String s = "C:my_backup\temporary\news";

What does the following code print out?

String s = "C:my_backup\temporary\news"; System.out.println("***\n" + s + "***");

What is the moral of this exercise?

Solution 1 The code prints the following bizarre output:

*** C:my_backup       emporary ews

The backslash-t is interpreted as a tab, and the backslash-n is interpreted as a newline. The moral is that you always have to use double backslashes in literal strings and chars if you want an actual backslash and not an escape code.

Exercise 2 The code examples in the "Writing and Reading Data" section defined an int called i, a float called f, a double called d, and so on. But the long was called n, which breaks the pattern. You might have expected the long to be called l. Why do you think this was not done?

Solution 2 The lowercase letter "ell" looks just like a "one." If you use a lowercase "ell" as a variable name, your code becomes hard to understand. Uppercase "oh" and lowercase "ell" are the two least readable variable names. (Notice how they are spelled out here, in order to make sure there is no confusion. It would be less helpful to tell you that O and l are bad variable names.)

Exercise 3 Write a program that creates a file containing 5,000 random doubles that are >= 0 and <200.

Solution 3 The following code creates a file that contains 5,000 random numbers in the required range:

import java.io.*; public class Ch13Q3 {   public static void main(String[] args)   {     try     {       FileOutputStream fos;       DataOutputStream dos;       fos = new FileOutputStream("RandomDoubles");       dos = new DataOutputStream(fos);       for (int i=0; i<5000; i++)       {         double randy = Math.random() * 200;         dos.writeDouble(randy);       }       dos.close();       fos.close();     }     catch (IOException x)     {       System.out.println("Caught IOException");     }   } }

Exercise 4 Write a program that verifies the file you created in the previous exercise. Your program should read the 5,000 doubles, making sure that each falls within the proper range. Your program should also make sure the file contains exactly 5,000 longs.

Solution 4 The following code validates the file that was created in Exercise 3:

import java.io.*; public class Ch13Q4 {   public static void main(String[] args)   {     try     {       FileInputStream fis =         new FileInputStream("RandomDoubles");       DataInputStream dis = new DataInputStream(fis);       boolean readBad = false;       for (int i=0; i<5000; i++)       {         double randy = dis.readDouble();         if (randy < 0  ||  randy > 200)         {           readBad = true;           System.out.println("Read bad double: " +                               randy);         }       }       if (!readBad)         System.out.println("File is valid.");     }     catch (IOException x)     {       System.out.println("Caught IOException");     }   } }

The for loop reads 5,000 doubles from the file and checks their range. If a double is out of range, the "Read bad double" message is printed out and the variable readBad is set to true. After the loop, readBad is checked. If it never got set to true, the file is considered valid.

Exercise 5 Look up the API documentation for the java.io.File class. An instance of this class contains information about an individual file. One of the methods of the class tells you the length in bytes of a file. Use this method to determine the number of bytes in the file you created in Exercise 3.

Solution 5 The following program uses the File class to read the size of the file created in Exercise 3:

import java.io.*; public class Ch13Q5 {     public static void main(String[] args)     {         File f = new File("RandomDoubles");         System.out.println("Length = " + f.length());     } }

The code prints out "Length = 40000". This is to be expected. The file contains 500 doubles, and each double is 8 bytes.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net