1
h05
CS56 F18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
5pm, 6pm, 7pm
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h05: Data Structures (HFJ 16)

ready? assigned due points
true Thu 10/04 07:00PM Thu 10/11 07:50PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please read HFJ Chapter 16, then answer these questions. (Don’t worry that we skipped chapters 12-15. We aren’t using Swing GUIs and we’ll return to serialization, threads and networking later as needed.)

Note: During W12, the CS56 students were assigned to create homework questions from Chapter 16. Questions marked with (“Thanks to ___”) are based on questions they came up with.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts (5pm, 6pm, 7pm) in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. (10 pts) (Thanks to Raphael T.)

    Bob is trying to implement a car class with subclasses Honda, Ford, and Chevy. He wants to create a method which can polymorphically take an ArrayList<Car>, or an ArrayList of any of its subclasses (e.g. ArrayList<Ford>, ArrayList<Honda>).

    He suggests the following method declaration:

    public void takeCar(ArrayList<Car> list) { /* code goes here*/ }
    

    What is wrong with Bob’s logic and what would method declaration would correctly implement what he’s trying to do?

  3. (10 pts) Suppose you have a variable ArrayList<String> words; which has already been instantiated filled with a list of words.

    Now you want to sort those words in alphabetical order. Write one line of code that will do this, using the technique described in Chapter 16.

  4. (10 pts) In the Java Collections API, List, Set and Map are defined as:

       (Circle one):   classes   interfaces

  5. (Thanks to Vicente C.) List, Set and Map are fundamental concepts in the Collections API of Java.

    Briefly describe each of these (List, Set, and Map). For each, give an example of a programming situation where that one is more appropriate than the other two.

    1. (10 pts) List

    2. (10 pts) Set

    3. (10 pts) Map

  6. The declaration of the sort method in class java.util.Collections is

    public static <T extends Comparable<? super T>> void sort(List<T> list)
    

    Here’s that declaration again, several times, with particular parts highlighted. The highlighted parts are repeated in bold type just above the declaration.

    In each case, give a brief explanation of the highlighted part of the code.

    1. (10 pts)

           
        <? super T>  
      public static <T extends Comparable <? super T> > void sort(List<T> list)
    2. (10 pts)

           
        <T extends Comparable <? super T>>  
      public static <T extends Comparable <? super T>> void sort(List<T> list)
    3. (10 pts)

           
        List <T> list  
      public static <T extends Comparable <? super T>> void sort( List <T> list )

https://UCSB-CS56-F16.github.io/hwk/h14 </div>