Sunteți pe pagina 1din 4

Java Software Structures, 4th Edition Exercise Solutions, Ch.

Chapter 6 Exercise Solutions


EX 6.1. Hand trace an ordered list X through the following operations.

An ordered list keeps the items in order no matter when they are added.
So the evolution of the list is as follows:

Operation List

X.add(new Integer(20)); 20

X.add(new Integer(35)); 20 35

Object Y = X.first(); 20 35 (Y takes on 20, list does not change)

X.add(new Integer(15)); 15 20 35

X.add(new Integer(10)); 10 15 20 35

X.add(new Integer(25)); 10 15 20 25 35

Object Y = X.removeLast(); 10 15 20 25 (Y takes on 35)

Object Y = X.remove(new 10 15 20 25 (Y takes on null)


Integer(35));

X.add(new Integer(45)); 10 15 20 25 45

EX 6.2. Given the resulting list X from Exercise 6.1, what would be the result of
each of the following?
a. X.first();
b. z = X.contains(new Integer(15));
X.first();
c. Y = X.remove(new Integer(10));
X.first();

X.first() returns 10
z = X.contains(new Integer(25)); z takes on true
X. first (); returns 10
Y = X.remove(new Integer(45)); Y takes on 45
X.first(); returns 10

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 6

EX 6.3. Consider the class ProgramOfStudy discussed in the chapter (Listing


6.2). Write a method addCourseBefore that would add the specified
course before the target course.
public void addCourseBefore(Course target, Course newCourse)
{
if (target == null || newCourse == null)
return;
int targetIndex = list.indexOf(target);
if (targetIndex != -1)
list.add(targetIndex - 1, newCourse);

EX 6.4. What would happen if a programmer erroneously changes the code


snippet “list.add(targetIndex + 1, newCourse);” to “list.set(targetIndex +
1, newCourse);”?

The code snippet list.add(targetIndex + 1, newCourse); is intended to add


the specified course after the target course. However, the programmer
erroneously replaces the course after the target course with the new
course.

EX 6.5. Compare and contrast ordered, unordered and indexed lists in terms of
adding elements in them.
The differences between ordered and unordered lists generally center on how
elements are added to the list. In an ordered list, we need only specify the
new element to add it. Its position in the list is based on its key value.
An unordered list supports three variations of the add operation. Elements can
be added to the front of the list, to the rear of the list, or after a particular
element that is already in the list.
In case of an indexed list, a new element can be inserted at a particular index,
or it can be added to the rear of the list without specifying an index at all.
When an element is inserted, the elements at higher indexes are shifted up to
make room.

EX 6.6. Hand trace an unordered list through the following operations.


X.addToFront(new Integer(20));
X.addToRear(new Integer(35));
Object Y = X.first();
X.addAfter(new Integer(15), new Integer(20));
X.addToFront(new Integer(10));
X.addToRear(new Integer(25));
Object Y = X.removeLast();
Object Y = X.remove(new Integer(35));
X.addAfter(new Integer(45), new Integer(15));

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 6

Operation List
X.addToFront(new
Integer(20)); 20

X.addToRear(new
Integer(35)); 20 35

Object Y = X.first(); 20 35 (Y takes on 20, list does not change)


X.addAfter(new
Integer(15), new 20 15 35
Integer(20));

X.addToFront(new
Integer(10)); 10 20 15 35

X.addToRear(new
Integer(25)); 10 20 15 35 25

Object Y = 10 20 15 35 (Y takes on 25)


X.removeLast();
Object Y = X.remove(new
Integer(35)); 10 20 15 (Y takes on 35)

X.addAfter(new
Integer(45), new 10 20 15 45
Integer(15));

EX 6.7. Discuss the benefits provided by the private method find in the ArrayList
class. What is the logic behind keeping this method as private?

The find method helps in code reuse. It simplifies the definition of the
already complex remove method, as this method uses the find method in
implementing its algorithm. The find method is also used to implement the
contains operation as well as the addAfter method for an
ArrayUnorderedList.

The find method supports the implementation of a public operation on the


list, rather than defining a new operation. Therefore, it is declared with
private visibility.

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 6

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

S-ar putea să vă placă și