These are the solutions for the Sixth Assignment. Please grade Homework Six. For each problem there are a number of aspects you need to consider: - 15 points setting up the connection to the input file correctly - 25 points the part of the loop that deals with reading data correctly - 25 points the part in the loop that deals with processing data correctly - 25 points for the part that deals with putting the correct result out - 10 points for anything extra (procedures, extra-clean or effective code) They had to solve 4 problems plus problem 9. 1 was solved already. Please give the grades to me in two columns: one for the 4+1=5 problems and one for the extra problems that they solved. Let me know if you have questions. Homework Eight has been posted. Solutions to Homework Seven will come to you tonight. ... Adrian :::::::::::::: Eight.java :::::::::::::: import java.io.*; import java.util.*; class Eight { public static void main(String[] args) { String inputFile = args[0]; Eight.methodEight(inputFile); } public static void methodEight(String name) { int max = -1; String longest = ""; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line); if (st.countTokens() > max) { longest = line; max = st.countTokens(); } line = handset.readLine(); } System.out.println("The line with most words in file " + name + " is:"); System.out.println("(" + longest + ")"); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Five.java :::::::::::::: import java.io.*; import java.util.*; class Five { public static void main(String[] args) { String inputFile = args[0]; Five.methodFive(inputFile); } public static void methodFive(String name) { int count = 0, words = 0; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { count = count + st.nextToken().length(); words = words + 1; } line = handset.readLine(); } cable.close(); System.out.println("This file has " + count + "/" + words + " = " + (double)count/words + " characters per word."); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Four.java :::::::::::::: import java.io.*; import java.util.*; class Four { public static void main(String[] args) { String inputFile = args[0]; Four.reportAverageLineLength(inputFile); } public static void reportAverageLineLength(String name) { int count = 0, lines = 0; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { count = count + st.nextToken().length(); } lines = lines + 1; line = handset.readLine(); } cable.close(); System.out.println("This file has " + count + "/" + lines + " = " + (double)count/lines + " characters per line."); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Nine.java :::::::::::::: import java.io.*; import java.util.*; class Nine { public static void main(String[] args) { String in = args[0], outFile = args[1]; try { FileInputStream cable = new FileInputStream(in); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); FileOutputStream stream = new FileOutputStream(outFile); PrintWriter out = new PrintWriter(stream, true); String line = handset.readLine(); while (line != null) { out.println(line); line = handset.readLine(); } cable.close(); stream.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: One.java :::::::::::::: import java.io.*; /** * In this program I solve the problem of counting the number of lines * in a file. This is problem one in homework six ( * * @author Adrian German * */ public class One { /** The main method is only two lines long because a procedure (method) * is used to count the lines in a file. The name of the file is passed * on the command line and is available to the main method as the first * of the command line arguments, args[0]. args[0] is passed to the * One.countLines(String) method and the int returned by the method is * placed in the count variable (which is local to main). All of this is * described on one line. The next line prints count and the program ends. */ public static void main(String[] args) { int count = One.countLines(args[0]); System.out.println("There are " + count + " lines in file " + args[0]); } /** The countLines method receives one argument, called name, of type String. * It sets a local variable, count, to zero, It then tries to create a new * FileInputStream, called cable, using the name of the file on the disk. On * top of the cable we put receiver, which is an InputStreamReader, and on top * of that one we create the handset (a BufferedReader) which will allow us to * read lines from the file with the name "name". So we start by reading the * first line from the handset and then we enter a loop: while the line is not * the end of file, increment the count by one, and read a new line, repeat. * If the try fails an error message is printed. Otherwise the file input stream * is closed. The program returns count which is the number of lines read from * the file. A return value of zero doesn't necessarily mean there was an error * since the file could have been empty. */ public static int countLines(String name) { int count = 0; try { FileInputStream cable = new FileInputStream(name); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { count = count + 1; // System.out.println("(" + line + ")"); line = handset.readLine(); } cable.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } return count; } } :::::::::::::: Seven.java :::::::::::::: import java.io.*; import java.util.*; class Seven { public static void main(String[] args) { String inputFile = args[0]; Seven.methodSeven(inputFile); } public static void methodSeven(String name) { int max = -1; String longest = ""; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { if (line.length() > max) { longest = line; max = line.length(); } line = handset.readLine(); } System.out.println("The longest line in file " + name + " is:"); System.out.println("(" + longest + ")"); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Six.java :::::::::::::: import java.io.*; import java.util.*; class Six { public static void main(String[] args) { String inputFile = args[0]; Six.reportAverageLineLength(inputFile); } public static void reportAverageLineLength(String name) { int count = 0, words = 0, i = 0; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { i = i + 1; count = 0; StringTokenizer st = new StringTokenizer(line); words = 0; while (st.hasMoreTokens()) { count = count + st.nextToken().length(); words = words + 1; } System.out.println("Line " + i + " has " + count + "/" + words + " = " + (double)count/words + " characters per token, on average."); line = handset.readLine(); } cable.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Ten.java :::::::::::::: import java.io.*; import java.util.*; class Ten { public static void main(String[] args) { String inputFile = args[0]; Ten.searchAndReplace(inputFile, args[1], args[2]); } public static void searchAndReplace(String name, String w1, String w2) { try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { String token = st.nextToken(); if (token.equals(w1)) { System.out.print(w2 + " "); } else { System.out.print(token + " "); } } System.out.println(); line = handset.readLine(); } cable.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Three.java :::::::::::::: import java.io.*; import java.util.*; class Three { public static void main(String[] args) { String inputFile = args[0]; Three.reportAverages(inputFile); } public static void reportAverages(String name) { int count = 0, lines = 0; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line); count = count + st.countTokens(); lines = lines + 1; line = handset.readLine(); } cable.close(); System.out.println("This file has " + count + "/" + lines + " = " + (double)count/lines + " words per line."); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } } } :::::::::::::: Two.java :::::::::::::: import java.io.*; import java.util.*; class Two { public static void main(String[] args) { String inputFile = args[0]; int count = Two.countTokens(inputFile); System.out.println("The total number of tokens in the file is: " + count); } public static int countTokens(String name) { int count = 0; try { FileInputStream cable = new FileInputStream( name ); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line); count = count + st.countTokens(); line = handset.readLine(); } cable.close(); } catch (Exception e) { System.out.println("Something went wrong: " + e.toString()); } return count; } } ------------------------------------------------------------------------------------