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