1 |
h04 |
CS56 F17 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section 2pm, 4pm or 5pm |
||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h04: HFJ 9,10,11: Constructors, Statics, Exceptions
ready? | assigned | due | points |
---|---|---|---|
true | Thu 10/05 02:00PM | Tue 10/10 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.)
NOTE: This assignment is due by electronic upload to Gradescope.
See this link for additional information: http://www.cs.ucsb.edu/~pconrad/cs32/15F/pdf/GradescopeSubmissionHelp.pdf
Reading Assignment:
- Read HFJ 9, HFJ 10, and HFJ 11 along with the online reading notes that go with those chapters. Then, do the problems below. Refer back to previous chapters as needed.
- (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 (2pm, 4pm or 5pm) 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.
-
From HFJ 9:
-
(5 pts) Under what conditions does the compiler create a no-arg constructor for you?
-
(5 pts) Under what conditions does the compiler NOT create a no-arg constructor for you?
-
-
From Chapter 10:
Be sure you understand precisely the difference between auto boxing and auto unboxing before answering the following questions.
-
(10 pts) Write a line (or two) of Java code that would result in auto-unboxing but NOT auto-boxing
-
(10 pts) Write a line (or two) of Java code that would result in boxing but NOT auto-unboxing
-
-
From : Exceptions in Java can be divided into two broad categories:
- One category is the kind that, if there is any chance it can happen in the code has to be “caught or declared to be thrown”
- The other category is the kind that can happen, but doesn’t have to be “caught or declared to be thrown”.
-
(10 pts) Exceptions that do not have to be caught or declared to be thrown are extensions of (i.e. they are subclasses of) what class?
-
(10 pts) What is the rationale for having some exceptions that do NOT have to be declared to be thrown or caught? (i.e. why did the designers of Java put that feature into the language?)
-
(10 pts) What is the rationale for having some exceptions that DO have to be declared to be thrown or caught? (i.e. why did the designers of Java put that feature into the language?)
-
(20 pts) From : Assume that s is an object of type
Student
, and that there is a methodpublic void registerFor(String courseNum)
that might throw theNoSuchUCSBCourseException
.Write a segment of Java code that will:
- call
s.registerFor(someCourse);
- write
"Success"
onSystem.out
if the registration succeeded (i.e. that exception doesn’t happen) - write
"Sorry " + someCourse + "does not exist"
toSystem.out
if that exception occurs.
Hint: this question is designed to see if you understand how to use
try
/catch
. On an exam, you would not get this hint. - call
-
(10 pts) From : Write the code that creates a new kind of exception called
BadSuitException
. This exception might be thrown, for example, by a program that expects a character that is one of ‘H’,’D’,’C’,’S’ for Hearts, Diamonds, Clubs and Spades when it encounters an illegal value (one other than ‘H’, ‘D’,’C’ or ‘S’.)BadSuitException
should be a subclass ofIllegalArgumentException
. Write ONLY the code that creates the new kind of exception.Hint: If you do this properly, it should be very short. The best answer is a single line. There are also answers that are two lines, or at most five. If you write more than five lines, you are likely on the wrong track. On an exam, you would not get this hint.