To Lecture Notes

IT 313 -- Feb 20, 2020

Review Exercises

  1. How if the class B is defined by
    public class B extends A {
    
    which of these statements is correct?
    a. A a = new A( );
    b. A a = new B( );
    c. B b = new A( );
    d. B b = new B( ); 
    
    Ans: a and d obviously correct. You can always assign an object to a reference variable of the same class. b is also correct; you can always assign an object to a reference variable of the base class. However, c is not correct. An A object is not a B object.
  2. What is the output?
    Person c = new Employee("June", 'F', 51, 2222, 59000.0);
    System.out.println(c);
    
    Ans:
    June F 51 2222 59000.0
    
    even though the Employee object is assigned to the Person reference variable c, the object knows what datatype it is so it executes the toString method of the Employee class.
  3. What is an iterator? How do you obtain the iterator of an ArrayList collection?
    Ans: an iterator object presents the objects of a collection sequentially. The iterator can be used implicitly, for example:
    for(Dog d : col) {
        System.out.println(d.getName( ));
    }
    
    or explicitly:
    Iterator itr = col.iterator( );
    while(itr.hasNext( )) {
        Dog d = itr.next( );
        System.out.println(d.getName( ));
    }
    
  4. Explain why each of these code lines is illegal:
    a. Speaker s = new Speaker( );
    b. ArrayList<int> col = new ArrayList<int>( );
    
    Ans: a is illegal because you can't instantiate an object from an interface. However, this is legal:
    Speaker s = new Dog("Taffy");
    
    b is illegal because you can't store values of a primitive datatype in an ArrayList. You need to store the wrapper class object instead:
    ArrayList<Integer> col = new ArrayList<Integer>( );
    
  5. What is an interface?
    Ans: It specifies the methods that a class is required to supply if the class implements the interface.
  6. How is an interface similar to and different from a base class.
    Ans: It is similar because methods in the interface or base class are overridden in the class implementing the interface or in the derived class. It is different because the interface methods must be overridden, whereas overriding is optional for a derived class.
  7. What are the required methods of each of these interfaces?
    Speaker  Comparable
    
    Ans: The required method of Speaker is speak; for Comparable it is compareTo.
  8. What does this statement mean?
    ArrayList<Speaker> col = new ArrayList<Speaker>(30);
    
    Ans: An ArrayList is being instantiated that contains objects that implement the Speaker interface. Its initial capacity is 30 objects.
  9. What is a HashMap collection? How does it differ from an ArrayList collection?
    Ans: Items in an ArrayList collection are added at an index or at the end of the collection. The items are referenced by index; items in a HashMap are inserted and referenced by key.
  10. Write a statement that creates a HashMap collection containing Dog objects. Set the initial capacity of the hashmap to be 100 and the load factor to be 0.67. Populate the collection with three Dog objects, something like this:
    Calendar c1 = Calendar.getInstance( );
    c1.set(2017, 4, 23);
    Dog d1 = new Dog("Molly", 'F', "Pomeranian", c1, 1234, false);
    System.out.println(d1);
    
    Calendar c2 = Calendar.getInstance( );
    c2.set(2018, 11, 2);
    Dog d2 = new Dog("Buster", 'M', "Dalmation", c2, 2345, true);
    System.out.println(d2);
    
    Calendar c3 = Calendar.getInstance( );
    c3.set(2015, 0, 18);
    Dog d3 = new Dog("", 'M', "Dalmation", c3, 3456, false);
    System.out.println(d3);
    
    Ans: We fixed up the Dog toString method because including the toString representation for the Calendar class was overkill. Here are final versions of the Dog and Main classes.

Midterm Exam

Project 4b

HashMap Iterators

Serialization