Monday, January 28, 2013

throws FileNotFoundException

throws FileNotFoundException explained (sort of):

An exception is an error that occurs at runtime. It is a response to unexpected condition or it is generated as a result of your executing a throw statement.


  • Checked exceptions: an exception that is a user error or a problem that cannot be foreseen by the programmer. Ex: If the file is to be opened but cannot be seen, exception occurs. These exceptions cannot be ignored at the time of compilation.
  • Runtime exceptions: an exception that could have been avoided by the programmer. Unlike checked exceptions, runtime exceptions can be ignored at the time of compilation.
  • Errors: These are not exceptions at all, but problems that arise beyond the control of the programmer. Errors are typically ignored because you can't do anything about them. If a stack overflow (overflow of memory in the parameters/preconceived memory) occurs, an error will rise. These are ignored at the time of compilation.
Hierarchy:

Throwable:
  • Error
  • Exception
    • IO Exception
    • RuntimeException
A really good example:

Let's look at the throw statement in context. The following pop method is taken from a class that implements a common stack object. The method removes the top element from the stack and returns the object.
public Object pop() {
    Object obj;

    if (size == 0) {
        throw new EmptyStackException();
    }

    obj = objectAt(size - 1);
    setObjectAt(size - 1, null);
    size--;
    return obj;
}
The pop method checks to see whether any elements are on the stack. If the stack is empty (its size is equal to 0),pop instantiates a new EmptyStackException object (a member of java.util) and throws it.



Wednesday, January 23, 2013

Lab 19d

During lab 19d, we attempted to sort the words by length. We encountered several problems involving the sort() method and making sure that we imported the correct things. Unfortunately, we didn't realize that we could have used the sort method (we didn't have to create our own sort method). I found out later that ArrayList does not have a sort() method. Instead, we use the static sort method of the Collections class. At first, I thought that the sort() method only applies when you want to sort objects by ASCII characters. It was interesting to see that it worked in sorting strings by length. Another problem that we ran into was that we did not import something very important, so our code didn't run at first. We needed to import java.util.Arraylist. Once we dealt these problems, the lab became a lot easier to understand and work out. The sort() method is actually quite useful, and it was interesting to see it actually work in a code.

Monday, January 14, 2013

ArrayLists

ArrayList( )
The first constructor builds an empty array list.
ArrayList(collection c)
The second constructor builds an array list initialized with elements of collection c.
ArrayList(int capacity)
The third constructor builds an array list that has an initial capacity.

Objects of type String are added to it.

import java.util.*;
class ArrayListDemo
{

public static void main(String args[])

{

ArrayList al = new ArrayList(); // creates new array list
System.out.println("Size of al: " + al.size()); // prints size of array list

al.add("A");
al.add("B");
al.add("C"); // adds elements to array list
al.add(1, "A2"); // adds A2 (index 1) to the array list while shifting other elements over

System.out.println("Size of al after additions: " + al.size()); // prints size of array list

System.out.println("Contents of al: " + al); // prints elements of array list

al.remove("C"); removes C from array list
al.remove(2); // removes element (index 2) from array list

System.out.println("Size of al after deletions: " + al.size()); // prints size of array list
System.out.println("Contents of al: " + al); // prints elements of array list

}
}

Output:
Size of al: 0
Size of al after additions: 4
Contents of al: [A, A2, B, C]
Size of al after deletions: 2
Contents of al: [A, A2]