Lab 6: Input and Output

  1. Create an empty directory named javalab6/ in your directory.
    1. Copy /home/umu/gvogl/java/examples/ConsoleString.java into your javalab6/ directory.
    2. Create a folder named cs1/ and copy Keyboard.class into that folder.
  2. Create a class named CommandLine to display command line arguments.
    1. Copy from ConsoleString.java to CommandLine.java.
    2. Change the class name from ConsoleString to CommandLine.
    3. Display the number of arguments using the public length attribute from the args array (args.length).
    4. Declare an integer i and loop from i = 0 to the length of args.
    5. Within the loop, print each argument i.
    6. Run the program using java CommandLine. The argument count should be 0.
    7. Run the program again using java CommandLine foo bar baz. The argument count should be 3.
  3. Create a class named EchoStrings to read and display the strings the user types.
    1. Copy from ConsoleString.java to EchoStrings.java.
    2. Change the class name from ConsoleString to EchoStrings.
    3. Prompt the user to enter strings or Ctrl-d to exit.
    4. Declare a string input and loop while (input = console.readLine()) is not null.
    5. Within the loop, display the input string. Also display the string's length using its length() method.
    6. Add an integer variable to count the number of iterations. After the while loop, display the number of strings entered.
    7. Compile and test the program by typing lines of input.
    8. Create a text file named input.txt with your name, the course name, assignment title and date on separate lines.
    9. Run the program using java EchoStrings < input.txt to see the file echoed to the screen.
    10. Run the program using java EchoStrings < input.txt > output.txt and then type cat output.txt.
  4. Create a class named FileCopy to copy files.
    1. Copy from ConsoleString.java to FileCopy.java.
    2. Add a usage() method that displays "Usage: java FileCopy inputfile outputfile" and calls System.exit(1).
    3. In main(), if args.length is not 2, call usage().
    4. Create a new FileReader reader and FileWriter writer using the first two arguments args[0] and args[1].
    5. Create a new BufferedReader in and PrintWriter out using reader and writer.
    6. Add import statements for these four classes.
    7. Create a String named inputLine and loop while (inputLine = in.readLine()) is not null.
    8. Inside the loop, print the input line to the print writer using out.println(inputLine).
    9. Add an integer variable to count the number of lines. After the while loop, display the number of lines copied.
    10. Run the program using java FileCopy input.txt copy.txt and then type cat copy.txt.
  5. Clean up and document the programs.
  6. Compress the folder using the Ark archiving program to produce javalab6.zip.
  7. Send the javalab6.zip file to the instructor as an attachment in a message with subject java lab 6.