1
h00
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"

h00: HFJ 1, 2: Java basics

ready? assigned due points
true Mon 04/01 07:00PM Mon 04/08 12:30PM

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

Throughout the quarter, when I refer to HFJ, this means your Head First Java, 2nd Edition textbook.

  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. (20 pts) According to chapter two, anytime you need to test a class in Java, you need at least two classes. (As we'll discuss in lecture, this isn't strictly true, but let's go with it for now.) In the (oversimplified) view the author is presented (it is just chapter two, after all,) what are the two roles that these two classes are playing? Idenfity the roles, and explain each briefly,
  3. (10 pts) Throughout the book, some important material is sometimes in the little "side boxes" and dialogues. It's important to read everything on every page. One of these little side-boxes contains some important information about the "heap" in Java, and how it differs from the heap in C++. (C++ is not specifically mentioned, but from taking CS 16/24/32, you should know about how the heap works in C++.) Briefly explain what the authors tell us about the heap in Java—something that is definitely different from the heap in C++.
  4. (10 pts) Explain briefly the relationships between/among (a) source code, (b) the Java Compiler, (c) Java bytecodes, and (d) JVM. You get full credit if you write a sentence (or a few sentences) that make it clear you understand how each of these relates to the big picture, and to the other three. To ensure full credit, *DO NOT JUST COPY TEXT FROM THE BOOK*. Put this in your own words. I should get at least a slightly different answer from each student (or pair of homework buddies.)
  5. (20 pts) On page 5 and 6, there is a set of exercises, and the answer to those. Here is a similar set of exercises, but the answer are not provided. Fill in the blanks.
    java code explanation
    boolean cs56IsAwesome= true;
     
    Course c = new Course("CMPSC56","S19");
     
    String thisQuarter = "S19";
     
    if (c.getQuarter().equals(thisQuarter))
     
  6. (20 pts) Now, the same kind of exercise, but in reverse---I give you the description, you give me the code. These are designed that you should be able to just reason them out from the examples of Java code given on p.4 and p.5, and your general programming background from CS16 and CS24 in C/C++---no other knowledge of Java should be needed.
    java code explanation
     
    declare a variable that indicates whether this year is a leap year or not, and initialize it to say that it is not a leap year
     
    declare a variable item of type MenuItem, and initialize it to a "Caesar Salad" that costs 8.95. Assume those are the two parameters that the constructor takes.
     
    declare a variable of type double called total
     
    an assignment statement that calls the method getPrice on the variable item and adds the result into the variable total
  7. (10 pts) In Chapter 2, the author describes two different approaches to a problem—one taken by "Larry", and the other taken by "Brad". Based on what you read here, and in your own words, how would you characterize the main difference between the two approaches?