CS10, 09S, UCSB
H05: (Based on Horstman, Chapter 5) Total points: ?
Accepted: on paper, in lecture (1-1:50pm) on Wednesday, April 15th only.
No email submission allowed.
Name: (2 pts)______________________________ UCSBNetID (2 pts) _____________________
Section (6 pts) Circle one: Thu 4pm Fri 10am Fri 11am Fri noon
Take pity on your TAs! Our TAs have to process over 2500 paper assignments over the course of this quarter!
(86 students * 10 weeks * 3 per week = 2580).
So, please remember to circle your discussion section. This helps them to sort your submissions first by section, and then by last name. This makes entering your grades in Gauchospace a lot easier. I've increased the points for section this week to help emphasize how important this is to making your TAs lives manageable.
Circle the section number that you are officially registered for on GOLD now.
Prof. Conrad is aware that some of you want to switch sections, and we'll try to get to that issue this week, now that the account problems have mostly been resolved.
- (3 pts) Fill in the blanks:
Chapter 5 introduces this technical term: predicate method.
A predicate method has a return type of ____________________________.
This implies that it can only return one of two values:
either ___________________________ or _____________________________
- A quadratic equation object (one that encapsulates the coefficients a,b,c) is degenerate if the value for a is zero.
It has a degenerate root (with the value -c/b) if both a is equal to zero, and b is not equal to zero.
I will now ask you to write two predicate methods. Be sure that you choose names for these methods that follow the "common convention" for naming predicate methods described in Chapter 5. Once you've understood that convention, the most obvious name to choose can be easily found in the problem statements below.
- (5 pts) Write a predicate method for a QuadraticEquation class that allows a user of the object to directly check whether the equation is degenerate. Assume the class has instance variables
coefficientA, coefficientB, and coefficientC, all of type double.
- (5 pts) Write a predicate method for this same class that allows a user of the object to directly check whether the equation has a degenerate root.
(Hint: you are not being asked to calculate the value of that root, but to write a predicate method that determines whether or not such a root exists.)
- (16 pts) Complete the truth tables. The first three columns are filled in for you.
In Java, you'd have to write out the word true or false, but for purposes of this table, you may simply write T or F, as long as your handwriting is clear.
Note that any marking that is "ambiguous", i.e. it is not absolutely clear whether you intended T or F, will not receive any credit.
| a |
b |
a&&b |
a||b |
!a |
!(a ||b) |
(!a) && b |
| true |
true |
true |
|
|
|
|
| true |
false |
false |
|
|
|
|
| false |
true |
false |
|
|
|
|
| false |
false |
false |
|
|
|
|
- (5 pts) Assume that you have a class for a Box that we might want to draw on a 2D graphics surface. A box object has four instance variables all of type double, called
ulx, uly, width, height. The variables ulx and uly stand for the x and y coordinate of the "upper left corner" of the box.
- (Side note: the coordinate system for many of the Java graphics classes has the origin at the upper left, with x increasing to the right, and y increasing down the page, not up, as we are used to from the traditional Cartesian plane. Python graphics do something similar, as do many other computer graphics systems. Go figure.)
Write a predicate method called inBox, that takes two parameters of type double. These parameters should be called x and y.
Your method should return true if the value of x is between ulx and ulx + width, and the value of y is between uly and uly + height. Use < and >, not <= and >=.
- (3 pts) What do you have to be careful about when testing whether one string is equal to another string?
(3 pts) What is the common error that can occur when using the switch statement in Java (and other languages like C and C++ that have a similar statement?)
End of H05