Week 7 Notes and Questions

Exceptions


Questions

Below is "Person.java" with all the javadoc comments deliberately not included.

import java.awt.*;      // for java.awt.Color
import java.util.*;     // for java.util.Date

class Person implements Comparable {
    int age;
    String name;
    Color hairColor;
    final java.util.Date creationDate;
    
    // Copy the values
    public Person(String name, int age, Color hair)
    {    
        this.name = name;
        this.age = age;
        this.hairColor = hair;
        creationDate = new java.util.Date();    
    }
    
    public Person(String name, int age) 
    {
        this(name,age,null);
    }
    
    // "Factory" method
    static public Person createBlonde(String name, int age)
    {
        return new Person(name,age,new Color(128,128,0));
    }

    public String toString() 
    {
        return name + ", who is " + age + " and was created on " + 
            creationDate;
    }

    // Required for "Comparable" interface
    public int compareTo(Object o) 
    {
        if (o instanceof Person) {
            Person other = (Person)o;
            if (other.age > age) {
                return 1;
            }
            if (other.age < age) {
                return -1;
            }
        }

        // The object wasn't a Person, or it was
        // a Person with the same age.  So we'll use
        // the hashcode to make the decision

        if (o.hashCode() == hashCode()) {
            // They are the same object
            return 0;
        }
         
        if (o.hashCode() > hashCode())  {
            return 1;
        }
        return -1;
    }
    

    public static void main(String [] args) 
    {

        java.util.LinkedList myCollection = new java.util.LinkedList();
        
        for (int i=0; i<20; i++) {
            // Create a new person with random age
            int age = (int) (Math.random() * 70);
            Person p = new Person("Person number " + i, age);
            myCollection.add(p);
        }

        java.util.Iterator it = myCollection.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            System.out.println(o);
        }
        System.out.println("The collection has " + myCollection.size() 
                           + " elements");
        
    }   
}


  1. Modify the main code so that the line creating a new Person now reads:
                Person p = new Person("Person number " + i, (age - 50));
    
    What happens when you run the program now?
  2. Modify the "Person" code above so that the first constructor throws an "IllegalArgumentException" if the age parameter supplied is less than zero. What do you expect will happen when you try to compile the program now?
  3. (If you've not already done so) add an exception handler to the program to deal with any "IllegalArgumentException"s that occur during main
  4. Change the code so that instead of throwing an "IllegalArgumentException", it throws an "Exception". What do you expect to happen when you compile the program now?
  5. Fix the program so that it builds and runs when the type of exception thrown is an "Exception"
  6. Create a new class called a "SillyAgeException". Change your Person class so that it throws a SillyAgeException for minus ages
  7. Add methods to your SillyAgeException so that it keeps track of what the silly age was. In the Person code, catch any SillyAgeExceptions, and display a message reporting what the silly age was.
  8. Add code to the "compareTo" method so that it throws a "ClassCastException" if it is called with an argument that is not a Person.
  9. Use a "finally" clause in the main method so that it displays a message "the program is now finishing"
  10. Modify the "main" method so that instead of creating 20 Person objects, it reads from the command line an argument containing the number of objects to create, e.g.
         $ java Person 25
    
  11. Make the main method throw an exception if the user asks for more than 100, or less than 5 Person objects.
  12. When do you expect the "finishing" message to be displayed if
    • The program finishes normally
    • The program throws an exception because the user asked for 1000 objects?
    Try it and see

Back to index page