1 |
h01 |
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" |
h01: HFJ 3,4: Primitives, References, Instance Variables, Methods
ready? | assigned | due | points |
---|---|---|---|
true | Mon 04/08 05:00PM | Mon 04/15 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.)
This homework (like all homeworks this quarter) will be due in lecture
Please:
- No Staples.
- No Paperclips.
- No folded down corners.
Reading Assignment:
- Read HFJ Chapter 3 (especially pages 59-62) and the online reading notes.
- Read HFJ Chapter 4 and the online reading notes.
- As you read, also consult the online reading notes linked above. To do so, go to the online version of this homework, at https://ucsb-cs56.github.io/s19/hwk/h01/ and click the links.
- Then, do the problems below.
- (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.
- Based on your reading in HFJ Chapter 3:
-
(5 pts) In Java, a variable can store a primitive or a reference. Briefly: What’s the difference?
-
(5 pts) If I write
3.4
, is that of typedouble
, orfloat
? -
(5 pts) Declare
x
as adouble
and assign it the value 3.4 (as adouble
) -
(5 pts) Declare
y
as afloat
and assign it the value 3.4 (as afloat
)
-
-
(5 pts) In C++, the name of a plain old array of
Student
objects is not an object, but is rather a pointer to aStudent
(i.e. it is of typeStudent*
. What about in Java—is an array an object, yes or no? -
Variables that represent a primitive type (e.g.
boolean x;
orint y;
) and variables containing object references (String w;
orStudent z;
) have this in common—they are both composed of bits in memory.But, as explained in HFJ Chapter 3, they differ in what the bits “actually” represent. You won’t get this one by just guessing—you really have to read the book.
-
(5 pts) What do the bits that represent
int y;
represent?Assume that
y
is assigned the value 13 -
(5 pts) What do the bits that represent
String w;
represent?Assume that
w
is assigned the value"foo"
.
-
-
Based on your reading in HFJ Chapter 3, p. 59-62 and HFJ Chapter 4 p. 84:
-
(10 pts) Suppose I have a class called
Student
.
How do I declare and allocate space for a plain old Java array calledstudents
that can hold 5 references to Student objects? -
(10 pts)
Java
for
loops look pretty much just like C++for
loops (see HFJ page 10 if you really need to check.) Given that, assuming there is a default constructorStudent()
that you can call to create a newStudent
object, write afor
loop that initializes all of the elements of the arraystudents
(from the previous problem) to be instances of theStudent
> class.
-
- Consider these questions about memory—answers are in HFJ Chapter 3
-
(5 pts) Assuming the same JVM, can the amount of memory taken up by an object reference differ for different kinds of objects (say
String
vs.ArrayList<String>
?) - (5 pts) Assuming the same JVM, can the amount of memory taken up by the object itself differ for different kinds of objects
-
(5 pts) Can the amount of memory taking up for an object reference for a object particular type (say
String
) differ from one JVM to another?
-
-
HFJ Chapter 4 discusses the difference between the
==
operator and the.equals
method.-
(10 pts) Under what circumstances should you use the
==
operator to compare two variables? -
(10 pts) Under what circumstances should you use the
.equals
method to compare two variables?
-