CS10, 09S, UCSB

H07: (Based on Horstman, Chapter 7) Total points: ? (Printable PDF)

Accepted: on paper, in lecture (1-1:50pm) on Wednesday, April 29th only.
No email submission allowed.

Name: (2 pts)______________________________   UCSBNetID (2 pts)  _____________________

Section (3 pts) Circle one:         Thu 4pm                 Fri 10am                       Fri 11am                  Fri noon


  1. (5 pts) How do you declare and initialize an array called studentNames that can hold ten names of students as String objects?

    (Assume that the array is a local variable in a method, not an instance variable.)





  2. (5 pts) How do you declare and initialize an array called coinDemons that will contain the values 1, 5, 10 and 25, all in one Java Statement?

    (Assume that the array is a local variable in a method, not an instance variable.)




  3. (5 pts) There is a line of code missing here which causes this segment of Java code to not work.
    Fill in the missing line where indicated.


    String [] colleges;

    // missing line of code

    colleges[0] = "Letters and Science";
    colleges[1] = "Engineering";
    colleges[2] = "Creative Studies";

    (Please turn over for more problems)


  4. In Java, we can work with Arrays, or we can work with ArrayLists (as described in Section 7.2)

    1. (5 pts) Write a line of code that delcares and initializes an Array of 5 objects of type Complex (Assume you have a Complex class defined, and make this be a local variable in a method.)





    2. (5 pts) Write a line of code that declares and initializes an ArrayList of 5 objects of type Complex
      (Assume you have a Complex class defined, and make this be a local variable in a method.)





  5. (5 pts) In Section 7.2, the author suggests that there are (at least) two advantages of working with an ArrayList of Complex objects rather than an array of Complex objects. What are these?

    1. (2 pts)


    2. (2 pts)


  6. (5 pts) In Section 7.3, the author points out at least one situation where you should consider using a regular array rather than an ArrayList.
    1. (2 pts) What is this situation?



    2. (2 pts) What is the reason for using an Array in this situation rather than an ArrayList?




End of H07