import java.io.*; import java.util.Scanner; import java.util.Random; public class lab6 { public static void main(String[] args) throws IOException { /* Problem 1 * a - False: the writer line creates a new file called numbers.txt and replaces the old, so there are no * lines in the file named numbers.txt. Therefore this will throw a runtime error. * b - True. This was mentioned in class and in the slides * c - False: Believe it or not, java is smart enough not to go into an infinite loop on this input. * This is because the code given declares the FileWriter first and then scanner based on * that FileWriter. The point at which you're writing into the file is after the scanner * declaration, so it doesn't affect the Scanner, just the File. * d - True. We did it in class * e - True. You can test this on your own, with a simple block of code such as: * * PrintWriter writer = new PrintWriter("numbers.txt"); * writer.println("test"); * * Try it with and without the writer.close() command and you'll see it's necessary. * f - False: it will create a file called test but not test.txt. It will not be able to write to this. */ Random rand = new Random(); /* // 2a: the first two lines here were supposed to be assumed as a user input double A = 7; double B = 12; double x; x = rand.nextDouble(); // Now x is between 0 and 1 x = x*(B-A); // Now x is between 0 and 5. This is the length of the interval [A,B] x = x+A; // Now x is in the interval [A,B] */ /* //2b PrintWriter writer = new PrintWriter("numbers.txt"); int n = rand.nextInt(99); // This returns an integer in [0,99] n++; // Now it's in [1,100] as requested by the problem. for (int i=1; i3 && line.substring(line.length()-3, line.length()).equals("the")) the++; */ // This loop counts words, using the fact that words are always separated by spaces. // It also assumes the document doesn't have too many spaces, e.g. doesn't double-space after periods // Of course, if it did you could add an extra test to the if-statement to not double-count this for(int i=0; i