1 |
h03 |
CS56 F19 |
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" |
h03: HFJ 5,6: Random, ArrayList, first look at the Java API
ready? | assigned | due | points |
---|---|---|---|
true | Mon 09/30 05:00PM | Mon 10/07 05:00PM |
Printable PDF 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.)
Reading Assignment:
- Read HFJ Chapter 5 and HFJ Chapter 6, and consult the online reading notes by clicking on the chapter links at https://ucsb-cs56.github.io/f19/hwk/h03/. Then, do the problems below.
- Note that you may also need to look back at HFJ Chapter 4 (or even at earlier chapters) for some problems.
- (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.
-
(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 anint
variable. You can find an example of this in HFJ Chapter 5 -
(10 pts) (From HFJ Chapter 5) Assume that
n
is anint
variable that has already been assigned some value greater than or equal to 1. Write a few lines of Java code that declare a newint
variablex
and assign it a random integer between0
andn-1
(inclusive, uniformly distributed over all n possible values.) -
(10 pts) (From HFJ Chapter 5) Proponents of Test Driven Developent (TDD) suggest you write your tests before you write your code. Why?
-
(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.)
-
(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”?
-
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 calledStudent
exists.-
(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 theStudent
objects, just the array of references, initially null). -
(10 pts) Now, write a line of java that makes an
ArrayList<Student>
ofStudent
references (HFJ Chapter 6 style). Capacity is unimportant—choose 5, or take the default, whatever you like. (Don’t allocate theStudent
objects, just theArrayList<Student>
, initially empty).
-
-
(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. (You are encouraged to check your answer by typing it into a main program online before submitting it).