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

h02: HFJ 5, 6: Random, ArrayList, first look at the Java API

ready? assigned due points
true Mon 04/15 05:00PM Mon 04/22 05:00PM

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 two lowest scores (if you have zeros, those are the two lowest scores.)


Reading Assignment:

  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 (4pm,5pm,6pm) 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) Write a few lines of code that demonstrate how to take a integer value that is in a String, and convert it to an integer value in an int variable. You can find an example of this in HFJ Chapter 5

  3. (10 pts) (From HFJ Chapter 5) Assume that n is an int variable that has already been assigned some value greater than or equal to 1. Write a few lines of Java code that declare a new int variable x and assign it a random integer between 0 and n-1 (inclusive, uniformly distributed over all n possible values.)

  4. (10 pts) (From HFJ Chapter 5) Proponents of Test Driven Developent (TDD) suggest you write your tests before you write your code. Why?

  5. (10 pts) From the online reading notes for HFJ Chapter 5: TDD is a part of a larger movement called eXtreme Programming (XP) that led to an even larger movement called Agile, that includes many other programming practices. Identify two of those other than TDD. (By identify, I mean that to earn full credit, you need to both name, and briefly explain (1 sentence) each of these two other practices. There are far more than just two, but any two that are legitimately core to Agile are acceptable.)

  6. (10 pts) Frequently asked “job interview” question that comes from somewhere in HFJ Chapter 4 or HFJ Chapter 5: briefly explain: part of Object-Oriented Programming is “encapsulation”. What is “encapsulation”?

  7. Review the difference between plain old java arrays (as in HFJ Chapter 4) and the ArrayList<T> type (as in HFJ Chapter 6).
    Assume that a class called Student exists.

    1. (10 pts) Write a line of java that makes a plain old Java array (HFJ Chapter 4 style) of Student references of size 5. (Don’t allocate the Student objects, just the array of references, initially null).

    2. (10 pts) Now, write a line of java that makes an ArrayList<Student> of Student references (HFJ Chapter 6 style). Capacity is unimportant—choose 5, or take the default, whatever you like. (Don’t allocate the Student objects, just the ArrayList<Student>, initially empty).

  8. (20 pts) From HFJ Chapter 5: Java 1.5 introduced a new (to Java) kind of for loop sometimes called a “foreach” loop (even though foreach is not a keyword in Java)—your textbook calls it the “enhanced for loop”. HFJ provides an overview of this kind of loop on p. 105 and 116. Write a few lines of code that declare an array of five integers, initializing them to the first five prime numbers (you can use a literal array initializer here—you don’t need to write code to compute the prime numbers), and then write a foreach type loop that iterates through that array printing out the values, one on each line.