Lab on File I/O

This lab assignment will introduce some of the ideas necessary to do Program 4. Some of the methods you implement here will carry over to Programming Assingment 4 with minimal changes (in particular the methods for reading and writing files).

This assignment involves reading in a data file containing pairs of x-y values, finding certain properties of the data (in particular the maximum and minimum x,y values and the average of all the values), and then draw points connected by lines in a separate window. But we've already done most of the work for you, you just have to implement the following functions (listed in LabExtra.java):

  1. public static int[][] readData(String filename)

  2. public static int maximumValueX(int[][] data) (actually, i already did this for you)

  3. public static int minimumValueX(int[][] data)

  4. public static int maximumValueY(int[][] data)

  5. public static int minimumValueY(int[][] data)

  6. public static double averageValue(int[][] data)

  7. public static int[][] scaleData(int[][] data, int scale)

  8. public static void writeDataToFile(String filename, int[][] data)

To get the program to draw the lines you need to successfully implement 1,2,3,4, and 5. Methods 6,7, and 8 are there as extra excercises.

Download the following files:

LabExtraMain.class

ExitListener.class

WindowUtilities.class

LineDrawer.class

LabExtra.java

LabExtra.class



Note that most of these files are classes – they are already compiled for you and ready to go. The only file you need to edit is LabExtra.java. The main method in LabExtraMain.class will call the methods listed above (the other classes are for the graphics portion of the program) and take care of all the user interaction for you. I've also included some test data and a class file LabExtra.class containing a partial solution (methods 1-5) that you can run to get a sense of what the program is supposed to do. Make sure all the above files are in the same folder (including test.data) and type java LabExtraMain in the terminal and type test.data when it asks your for a file name. (NOTE: you might need to resize the graphics window that pops up to get it to display the lines).

Now that you know what this thing is supposed to take a look at the formatof test.data. The first line is a single integer which is the number of x-y pairs listed in the program. After that the x-y pairs are listed with each pair on a separate line and the numbers separated by whitespace. You can assume they will all be integers (just as in Programming Assingment 4).

The first thing you need to do is implement readData. It might be worth looking at the tio documentation for the ReadInput class here. Once you create an instance of ReadInput you can read data from the file just like reading data from the console. You are all familiar with Console.in.readInt( ), right? Well, Console.in is an instance of ReadInput so you've been using this all along. So if your instance is named file_reader then you can read an integer from the file with the statement file_reader.readInt( ).

Implementing methods 2-5 should be relatively simple, especially since we've filled in one of them for you.



Emacs

I have heavily commented the code and formatted it so it should be easy to follow. I would recommend using emacs to do your editing (type xemacs & in the terminal) because it has syntax highlighting and auto-indent features. If you want to learn more about emacs check out this tutorial. For now just open up your file in emacs and then search through the menus for the Syntax Highlighting option. With that turned on you should see everything color coded. I find that it makes code much easier to read. Also if you hit Ctrl-I on a line it will be automatically indented. Marvelous!



For Those Who Want to Dig Deeper

Here are the links to the source code for the above files if you want to see how these different files fit together. If you are interested in the graphical component then check out LineDrawer.java.

LabExtraMain.java

ExitListener.java

LineDrawer.java

WindowUtilities.java