1 |
h01 |
CS56 M16 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section 9am or 10:30am |
||
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 | Wed 06/22 09:30AM | Thu 06/23 09:30AM |
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.)
Please:
- No Staples.
- No Paperclips.
- No folded down corners.
Reading Assignment:
- Read (especially pages 59-62) and online reading notes.
- Read and online reading notes.
- If you don’t have your book yet, buy it! But in the meantime you can read this either at the UCSB library, or online.
- Visit the course wiki at http://foo.cs.ucsb.edu/56wiki and you’ll find the link to the online textbook.
- As you read, also consult the online reading notes. To find them, go to the online version of this homework, at /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 (either 9am or 10:30am) in the space indicated. 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 :
-
(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 , 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 , p. 59-62 and 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
-
(5 pts) Does the amount of memory taken up by an object reference differ for different kinds of objects (say
String
vs.ArrayList<String>
?) - (5 pts) Does the amount of memory taken up by the object itself differ for different kinds of objects (assuming the same JVM)?
-
(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?
-
-
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?
-