Site Navigation

Site Search

CS 110: Lab 12

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Shearer, Stetson
Team 2
Amezcua Gutierrez, Edson
Wagster, Nathan
Team 3
Baird, Owen
Smith, Stephanie
Team 4
Berman, Jake
Awan, Samara
Team 5
Coudriet, Blake
Bloom, Thomas
Team 6
Erickson, Joel
Olden, Greg
Team 7
Goeke, Max
Cuddington, Chris
Team 8
Harris, Alexander
Straub-Walden, Andy
Team 9
Heflick, Liz
Williamson, Sarah
Team 10
McCauley, Rylee
Hastings, Jake
Team 11
Millard, Ryan
Quayle, Weston
Team 12
Barrett Wright, Matthew
Hogan, Martin
Team 13
Millard, Mike
Ling, Nathan
Team 14
Field, Sarah
Nash, Chris
Team 15
Akana, Chris
Olivares, Scott
Fill in: Hansen, Mitchell

Note: Partners will change every week.

You may wish to review basic pair programming guidelines before you begin.

You should change roles every 10 to 15 minutes.

Create a plain text file to use as input to a Java program

Plain text files can be created using Notepad or any other text editor. jGRASP allows for easy creation of plain text files from the file menu

  1. Navigate to your U:\labs\lab12 folder in jGRASP's left browse pane
  2. Use jGRASP's file menu to create a new Plain Text file, saving it as quotations.txt in folder U:\labs\lab12
  3. Copy and paste (or retype) the following quotations into the file quotations.txt
  4. Save the contents of the file
The best condition in life is to be not so rich as to be envied nor so poor as to be damned.
- Josh Billings
Life can only be understood backwards, but it must be lived forwards.
- Soren Kierkegaard
Living well and beautifully and justly are all one thing.
- Socrates
Life is a flower of which love is the honey.
- Victor Hugo
Life is like a chariot-wheel that ever rolls along.
- Anacreon

Note: by default, you will not be able to see *.txt files in jGRASP's browse pane. You can check that the file was saved correctly using Windows My Computer

Read text files as input to a Java program

Begin by writing a Java program that reads from a text file and displays the first line of content to the computer screen

  1. Create the new Java class IoDemo.java that has a main method that
    • Prompts the user for a name of a text file
    • Reads in the first line from the file
    • Echoes (displays) that line on the computer screen

Note: Lab 12 will use only one Java file containing a main method (not two)

Sample Session

Enter the name of the input file: quotations.txt
The best condition in life is to be not so rich as to be envied nor so poor as to be damned.

Read until end of file

Modify your IoDemo main method to use a while loop, reading in one line at a time, and displaying the line, until it reaches the end of the input file

Hint: Use a while loop to keep reading while the input line is not equal to null

Sample Session

Enter the name of the input file: quotations.txt
The best condition in life is to be not so rich as to be envied nor so poor as to be damned.
- Josh Billings
Life can only be understood backwards, but it must be lived forwards.
- Soren Kierkegaard
Living well and beautifully and justly are all one thing.
- Socrates
Life is a flower of which love is the honey.
- Victor Hugo
Life is like a chariot-wheel that ever rolls along.
- Anacreon

Write strings to an output file

  1. Modify your program to
    • Prompt the user for the name of both an input and output file
    • Read in the contents of the input file, line by line
    • Change the characters to all uppercase
    • Write the changed characters to the output file (line by line)

Sample Session

Enter the name of the input file: quotations.txt
Enter the name of the output file: allupper.txt

To check the contents of the output file (e.g. allupper.txt), use jGRASP's file menu, open file (or open toolbar icon)

THE BEST CONDITION IN LIFE IS TO BE NOT SO RICH AS TO BE ENVIED NOR SO POOR AS TO BE DAMNED.
- JOSH BILLINGS
LIFE CAN ONLY BE UNDERSTOOD BACKWARDS, BUT IT MUST BE LIVED FORWARDS.
- SOREN KIERKEGAARD
LIVING WELL AND BEAUTIFULLY AND JUSTLY ARE ALL ONE THING.
- SOCRATES
LIFE IS A FLOWER OF WHICH LOVE IS THE HONEY.
- VICTOR HUGO
LIFE IS LIKE A CHARIOT-WHEEL THAT EVER ROLLS ALONG.
- ANACREON

To Receive Credit

You are allowed to drop one lab grade for the entire quarter.