- 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.
- 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.
- 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( ));
}
- 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>( );
- What is an interface?
Ans: It specifies the methods that a class is required to supply if the class
implements the interface.
- 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.
- 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.
- 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.
- 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.
- 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.