Colorado State University

Recitation R9 - File Input & Output
Summer 2016

CS160: Foundations in Programming


Preview

The purpose of this lab is to:

Phase 0: Setting Up

input.txt output.txt

Create and complete the following 3 classes inside your P9 project:

Phase 1: Starting R9

In your R9 class, declare the following 3 instance variables:

    private Rectangle [] rArray;
    private Triangle [] tArray;
    private Circle [] cArray;


Copy the following getter methods into your class for testing purposes:

    public Rectangle[] getRectangles(){
	return rArray;
    }

    public Triangle[] getTriangles(){
	return tArray;
    }

    public Circle[] getCircles(){
	return cArray;
    }


Create two methods with the following signatures:

    public void readFile( String inputFile ){ }
public void writeFile( String outputFile ){ }
Within the main method:

Phase 2: Completing the readFile method

First of all, you're going to have a choice. Each of the following input files is formatted a little bit differently. Depending on how the file is formatted, you will write the calls to Scanner in your readFile method differently. Choose and save one of the following files into your R9 project: Each file will have the following componenets: Implement the readFile method to:
  1. Open the input file of your choice
  2. Parse the contents of the input files so that it populates all three instance arrays.
  3. Here is an example to get you started.
To test this and make sure that your readFile method is working correctly, after the call to readFile in your main method put the following code in:
System.out.println(Arrays.toString(r9.rArray));
System.out.println(Arrays.toString(r9.cArray));
System.out.println(Arrays.toString(r9.tArray));
This should print out the contents of each of your arrays.

Phase 3: Completing the writeFile method

Now that we have populated the arrays using the readFile method, it's time to write our results out to a file!
  1. Open up the file using the PrintWriter class.
  2. Loop through your instance variables in the following order: rArray, cArray, tArray.
  3. Write each object inside the arrays out to file, one per line, with the element number prepended to the front. An Example would be:
    1: Rectangle, (Width: 4.00, Length: 2.20), Area: 8.80
    2: Circle, (Radius: 8.60), Area:  232.35
    3: Circle, (Radius: 7.90), Area:  196.07
    4: Circle, (Radius: 2.10), Area:  13.85
    5: Circle, (Radius: 6.18), Area:  119.98
    6: Triangle, (Height: 5.00, Base: 6.00), Area: 15.00
    7: Triangle, (Height: 3.30, Base: 8.20), Area: 13.53
    8: Triangle, (Height: 5.50, Base: 10.10), Area: 27.78
    
  4. Once you are finished:


Phase 4: Submitting your Assignment

Since there are multiple files associated with this recitation, we need a good way to turn them in! This is where JAR files come into play. It's a nice way of packaging all of your code, so that you only have to submit one file.
  1. Right click on your R9 project and select Export
  2. Expand Java, highlight the JAR file option, and then select Next. Your screen should look like this.
  3. Make sure that Export Java source files and resources is selected
  4. In the upper left hand corner, uncheck everything except your input.txt
  5. Under Select the export destination: Select Browse... I like to put my JAR files in the project level of my directory. But so long as you can find it, you can put the JAR file anywhere. Make sure your JAR File Specification matches this image before selecting Finish.

HackerRank


Submit R9.tar to Checkin and Complete the Hackerrank problems.

© 2015 CS160 Colorado State University. All Rights Reserved.