1 |
h02 |
CS56 W18 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section 4pm, 5pm or 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 | Tue 01/16 11:00AM | Mon 01/22 09: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 four lowest scores (if you have zeros, those are the four lowest scores.)
NOTE: This assignment is due by electronic upload to Gradescope.
See this link for additional information: https://ucsb-cs56-w18.github.io/static/gradescope.pdf
Reading Assignment:
- Read and , along with the online reading notes that go with those chapters. Then, do the problems below.
- Note that you may also need to look back at (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 (4pm, 5pm or 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.
-
(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 -
(10 pts) (From ) 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 ) 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 : 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 or : briefly explain: part of Object-Oriented Programming is “encapsulation”. What is “encapsulation”?
-
Review the difference between plain old java arrays (as in ) and the
ArrayList<T>
type (as in ).
Assume that a class calledStudent
exists.-
(10 pts) Write a line of java that makes a plain old Java array ( 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 ( 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 : 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.