15-100 Lecture 34 (Monday April 18, 2005)

Exam 3

Exam 3 will be on Friday and will be an online coding question. It will be out of 100pts with 60pts coming from a toString or equals method and then the other 40 pts will be from a slightly harder to code method (it will be on the same difficulty level as a union or intersection of two sets).
File input/output

  String filename = "Data.txt"; // Or, use args[0]
  File inputFile = new File (fileName);
  FileReader reader = new FileReader (inputFile);
  BufferedReader br = new BufferedReader (reader);

  // Or, in one line 
  BufferedReader br = new BufferedReader (new FileReader (new File(args[0])));

Making an object File (the string is the name of the file you want to access) is how we are getting information off of the hard drive and putting it into a form that Java can understand and manipulate.

So what is the "args[0]"? This comes from main's signature:

  public static void main (String[] args)
args[0] is refering to the String array in main. In this case it must be set up with the first String in args to be the file's name that we want to access. So new File(fileName) is making an instance of File that will allow manipulation of the file of fileName. You need the FileReader as an interface that allows Java to read information from an object of type File.

The BufferedReader is an old friend which we usually use for IO. This will allow us to do the readLine() method that we have been using for all of the course. So making a BufferedReader for the FileReader will allow us to use the readLine() on the file. Note that each time you call readLine() the BufferedReader will return the first line of code that it hasn't read (this means you will linearly progress through the file and can't skip around. Also once a line has been read the BufferedReader won't read it again...so be sure to store the information in a variable).

Reversing arrays:

We want to reverse the array (aka: items, from our previous collection class, needs to referance a reversed array). We can do this in two ways: make a new array the same as array, only in the reverse order and then set items equal to it. Or you can go and swap the objects in items until you get the half way point and then stop.


class Collection {
  Object[] items;
  int count;

  public void reverseCopy() {
    Object[] reversedArray = new Object[items.length]; //could also make an arrray of size count
    //this will cause there to be null's that we have to make sure are left at the back of our
    //reversed array (opposed to swapping them to the front)
    
    //set boundries so only reversing non-null objects
    for(int index = (count-1); index >= 0; index--) {
      reversedArray[(count-1)-index] = items[index];
    }
  
    items = reversedArray;
  }

  public void reverseSwap() {
    for(index = 0; index < count/2; index++) {
      swap(index, (count-1)-index);
    }
  }

  //remember your three way swap to avoid overwritting information before it is swapped
  private void swap(int index1, int index2){
    Object temp = items[index1];
    items[index1] = items[index2];
    items[index2] = temp;
  }
If you want to return a reversed array and not change the order of items then you can modify the code to:

  public Collection reverseSwap() {
    Collection reversedCollection = new Collection(this);//making use of our copy constructor
    for(index = 0; index < count/2; index++) {
      reversedCollection.swap(index, (count-1)-index);
    }
    return reversedCollection;
  }
}