• Home
  • Textbooks
  • Introduction to Java Programming and Data Structures, Comprehensive Version
  • Generics

Introduction to Java Programming and Data Structures, Comprehensive Version

Daniel Y. Liang

Chapter 19

Generics - all with Video Answers

Educators


Chapter Questions

Problem 1

Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList. You should check the array size before adding a new element to the stack. If the array is full, create a new array that doubles the current array size and copy the elements from the current array to the new array.

Check back soon!
02:52

Problem 2

In Listing 19.1, GenericStack is implemented using composition. Define a new stack class that extends ArrayList.
Draw the UML diagram for the classes then implement GenericStack. Write a test program that prompts the user to enter five strings and displays them in reverse order.

Jordan Gassaway
Jordan Gassaway
Numerade Educator

Problem 3

Create a Pair class that encapsulates two objects of the same data type in an instance of Pai $r$.

Check back soon!
01:13

Problem 4

Write a generic static method that returns the smallest value in an instance of Pair from Programming Exercise 19.3.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 5

Create a Triplet class that encapsulates three objects of the same data type in a given instance of Triplet.

Check back soon!

Problem 6

Create an Association class that encapsulates two objects of different types. Similar to Programming Exercise 19.5, create a Transition class that does the same of Association class with three objects.

Check back soon!

Problem 7

Knowing that any object of type java. 1 ang. Number can be evaluated as a double with its doubleValue() method, write a method that computes and returns the sum of the three numbers in an instance of Transition as defined in Programming Exercise 19.6.

Check back soon!

Problem 8

Write the following method that shuffles an ArrayList:
public static <E> void shuffle(ArrayList<E> list)

Check back soon!

Problem 9

Write the following method that sorts an ArrayList:
public static <E extends Comparable<E>> void sort (ArrayList<E> 1ist)

Check back soon!
01:13

Problem 10

Write the following method that returns the smallest element in an ArrayList:
public static <E extends Comparable<E>> E min(ArrayList<E> 1ist)

SS
Sarvesh Somasundaram
Numerade Educator
05:52

Problem 11

Use the Comp1 ex class introduced in Programming Exercise 13.17 to develop the ComplexMatrix class for performing matrix operations involving complex numbers. The ComplexMatrix class should extend the GenericMatrix class and implement the add, multiple, and zero methods. You need to modify GenericMatrix and replace every occurrence of Number by Object because Com plex is not a subtype of Number. Write a test program that creates the following two matrices and displays the result of addition and multiplication of the matrices by invoking the printResu119.

Tarandeep Singh
Tarandeep Singh
Numerade Educator

Problem 12

(Revising Listing 23.4) to make it be generic bubble sort.

Check back soon!