1.  Write out the differences and similarities between interfaces and abstract classes.

Abstract classes = used for modeling “is-a” type relationships.

Interfaces = used for modeling “can-do” type relationships.

Can inherit from only one class. But can implement multiple interfaces.

Variables:

-No restriction for abstract classes (can have any type of variables).

-Interfaces can only have public static final variables.

Constructors:

-Abstract classes cannot be instantiated but they can have multiple constructors.

-No constructors in interfaces. Cannot be instantiated either.

Methods:

-No restrictions for abstract classes.

-All methods are public abstract methods.

Both interfaces and abstract classes can be used as data types and can take advantage of polymorphism.

2.  The following method transfers the Integer values in an ArrayList to an int array and returns that array. There are 6 errors in the code. Point them out and their possible fixes.

public static int transferToArray(ArrayList numbers) {

int[] num = new int[numbers.length];

for (int i = 1; i < numbers.length; i++)

num[i] = numbers[i];

return num;

}

Solution:

-numbers.length should be numbers.size() when creating the array

-numbers.length should be numbers.size() when traversing through the ArrayList

-i = 0, not i = 1

-return type should be int[] not int

-not numbers[i] but numbers.get(i)

-can’t directly do numbers[i] = should do a cast to (Integer)

3.  Write a method called countEvenNumbers which will count the number of even numbers present in an array and returns the count. It will take an int array as a parameter. Assume no null values are in the array.

Solution:

int countEvenNumbers(int[] array) {

int count = 0;

for (int i = 0; i < array.length; i++) {

if (array[i] % 2 == 0)

count++;

}

return count;

}

4.  Define and describe a shallow copy and a deep copy of an object, including their differences.

Solution:

Look at lecture notes on Cloneable.

5.  Create the class Building. The class will contain two private properties, name type String, the name of the building, and stories type int, the number of stories (floors) in the building. The default constructor will set the name to just “Building” and the number of stories will be 10. The class will implement the interfaces Comparable<Building> (comparisons based upon the stories in the building), Comparator<Building> (comparisons based upon the name of the building), and Cloneable (returns a deep copy).

Solution:

class Building implements Comparator<Building>, Comparable<Building>, Cloneable {

private String name;

private int stories;

public Building() {

name = “Building”;

stories = 10;

}

public Building(String name, int stories) {

this.name = name;

this.stories = stories;

}

// get/set methods

public int compareTo(Building b2) {

if ( stories > b2.getStories())

return 1;

else if ( stories < b2.getStories())

return -1;

else

return 0;

}

public int compare(Building b1, Building b2) {

return b1.getName().compareTo(b2.getName());

}

public Object clone throws CloneNotSupportedException {

return super.clone();

}

}

6.  Create the class Campus. The class will contain one property, buildings an array type Building. The class will implement Comparable<Campus> making comparisons based upon each of the buildings. Starting from the first building in the list and moving to the end, compare the same building positions of both campuses. The first buildings in the sequence to differ will define the comparison of the two campuses. If there are no differing buildings, then the two campuses are equal. If the two buildings are equal at some point but their array lengths differ, then the array with the larger array is bigger.

class Campus implements Comparable<Campus> {

private Building[] buildings;

public Campus() {

}

public Campus(Building[] buildings) {

this.buildings = buildings;

}

// get/set methods

public int compareTo(Campus campus) {

Building[] b2 = campus.getBuildings();

for (int i = 0; i < buildings.length; i++) {

// length check

if (i > b2.length)

return 1;

int c = buildings[i].compareTo(b2[i]);

if (c != 0)

return c;

}

// length check

if (b2.length == buildings.length)

return 0;

else if (b2.length > buildings.length)

return -1;

}

}

7.  Draw UML Diagrams showing the relationship between the Building and Campus. Also indicate all their properties and methods. No need to show interfaces (but show their methods inside the classes).

Solution: