Algorithms and OOD (CSC 207 2013F) : Labs

Laboratory: Simple Input and Output


Summary: In this laboratory, you will explore the basics of input and output in Java.

Classes Used:

Preparation

a. Review The reading on simple input and output.

b. Create a new project for this lab.

c. Create a new package for this lab.

Exercises

Exercise 1: Basic Interaction

Write and test a main class called Greet that prompts the user for a name, reads a name from the keyboard, and prints a greeting to the screen. Your output should resemble the following:

  What is your name? Sam
  Have a nice day Sam!

To type a response, you may need to click in the console area of the Eclipse window.

Exercise 2: Creating a File

a. Write and test a main class called DumpInfo that prints the following three lines to the screen.

  • Your name
  • Your email address
  • Your favorite color

b. Update DumpInfo so that it instead creates a file and dumps the information to that file.

c. Predict what will happen when you have DumpInfo.java write to a file that already exists. (For example, if you run it a second time.)

d. Check your prediction experimentally.

e. Predict what will happen when you modify DumpInfo.java to write to a file in a classmate's directory.

f. Check your prediction experimentally.

Exercise 3: Reading a File

a. Write and test a main class called ShowInfo that reads a file of three lines named info and prints each line preceeded by a category prompt (Name, Email, Color). For example, if the file contains

J. Doe
doej@gmail.com
plaid

the output on the screen should be

Name: J. Doe
Email: doej@gmail.com
Fave Color: plaid

b. Modify info using gedit and verify that ShowInfo continues to work correctly.

Exercise 4: Insufficient Input

a. What do you expect to happen when you run ShowInfo if info contains only two lines (e.g., doesn't contain a color)?

b. Check your answer experimentally.

c. What do you expect to happen when you run ShowInfo if info does not exist?

d. Check your answer experimentally.

e. What do you expect to happen when you run ShowInfo using a file in a classmate's directory?

f. Check your answer experimentally.

Exercise 5: Copying Parts of a File

Write and test a main class, CopyTwoLines, that copies the first two lines from a file named source and places them in a file named destination.

Exercise 6: Copying Parts of a File, Revisited

Write a test a main class, NewCopier, that prompts the user for the name of two files and copies the first two lines from the first file to the second file.

Exercise 7: Rewinding a File"

Write a short main class, ReadTwice, that prompts the user for a file, builds a BufferedReader for the file, sets a mark, reads the first line from the file, resets the BufferedReader, and reads again.

For Those With Extra Time

If you finish the lab early, you may want to try some or all of the following.

Extra 1: Marks and Keyboard Input

Determine whether you can use mark and reset for input taken from the keyboard in addition to input taken from a file.

Extra 2: Multiple Marks

Determine whether you can place more than one mark in a file.

Extra 3: Reading Ahead

Determine what happens if you read more than readAheadLimit characters between calls to mark and reset. Does the size of readAheadLimit make a difference (e.g., do you get the same behavior for small and large readAheadLimit if you read only two characters beyond that limit)?

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.