Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Lab 2
Due: Mon 7-Sep-2009 at 11:59pm (email copy to your CA)
(no late submissions accepted).
Read these instructions first!
- The lab instructions from lab1 all apply here.
- In addition, in this lab you have
non-programming problems. Here are the guidelines for those:
- Place all your solutions to the
free-response (non-programming) problems in a single file named Lab2
(with whatever extension is appropriate for the format you choose, such
as Lab2.txt or Lab2.html, etc). You must use one of these file formats:
plain text (txt), or RTF, or HTML, or Word (doc, not docx), or
PDF. No other file formats will be accepted.
- Show your work. Correct answers
without supporting calculations will not receive full credit (and may in
fact receive no credit at all!).
- Submit all your source (.java) files and your
one free-response (non-programming) file all in a single zipped folder,
lab2-<andrewId>.zip, sent by email as an attachment to your CA. For
example, I would submit lab2-koz.zip (you should replace "koz" with your
andrewId).
- Do not submit .java~ files (with the tilde
(~) at the end). These are DrJava backup files. They are not
useful.
- Do not submit .class files. These are
compiled bytecode files. They also are not useful.
- Do not submit project files or any other
files. Just .java files and your one free-response file.
Programming guidelines:
- Style counts!!! Use well-named
variables, proper indenting, reasonable commenting, etc.
- Note: You may not use Java concepts we have not
yet covered, including loops (do/while/for), conditionals ("if" statements
or ternary operators (?:)), arrays, or methods from any classes in
java.util.* (besides Scanner or others we explicitly use) to solve these
problems (which isn't a problem for most of you, seeing as we have not yet
covered these!). While they may be helpful, every problem here is solvable
without them, and they are not permitted for now.
- Short Answers
- Tracing
- Mystery Code
- Non-Graphics Methods
- nearestBusStop
- xIntercept
- xInterceptOfParabola
- isPerfectSquare
- toUpperCase
- Painting Flags
- Benin
- The Central African Republic
- Short Answers
In the written portion of your submission, answer each of the following
questions. Be very brief, but show your work where appropriate.
Assume that "x" and "y" are non-negative integer values, and ignore any
errors (such as dividing by zero):
- If (x % 2) equals 1, then we know that x is ____________.
- True or False: (x % y % y) always equals (x % y)?
- For this part, assume
that "x" and "y" are boolean values (though these are not very good names
for boolean values):
Fill in the blanks with one of “always”, “sometimes”, or “never”.
Be sure to show your work, using truth tables to prove
your answer. The first problem is done for you to
demonstrate.
- !(x || y)
___sometimes____
equals (x && y)
x y
(x || y) !(x || y)
(x && y)
T T
T F
T
T F
T
F F
F T
T
F F
F F
F T
F
Note: The truth table clearly shows that the two
expressions are equal when x is true and y is false or when x is false
and y is true, but are unequal when both x and y are false or both are
true.
Note: the column labeled (x || y) is not part of the
final answer, but is still important to include. It
is a “helper column”, and it simplifies the computation of the next
column. You should include helper columns as you
deem necessary.
- (!!x)
________________ equals x
- (x && !y)
________________ equals (!x || !y)
- (x && !y)
________________ equals !(x || !y)
Tracing
In the written portion of your
submission, indicate what each of the following will print or (if it is
graphical) draw. Do not run these programs. Figure this out by
hand. Remember to show your work.
a)
|
public static void main(String[] args) {
int x = 5, y = 2, z = 3;
System.out.println(x + y * z);
System.out.println(x % y % z);
System.out.println(10 + 9 / x * z - y);
}
|
b)
|
public void paint(Graphics page) {
int c = getWidth(), d = getHeight();
page.fillRect(c/2-10, 50, 20, 100);
page.fillOval(c/4,d*3/4,c*3/4, d/4);
}
|
Mystery Code
In the written portion of your submission, answer the following question
in general, and in just a few words of plain English.
The first problem is done for you to demonstrate.
Hint: while you should definitely trace the code in this problem by hand to
make some sense out of it, you may also wish to actually test the code in a
compiler with samples of your choosing, at least to verify that it works as
you think it does!
Note: these methods may seem a bit confusing, and in
fact they are! There are some much easier, clearer, and
more sensible ways to do the same thing. This is to test your
understanding of data and expressions, not to teach the best way to write
these methods.
- What does this method do (in general)?
public static
boolean mysteryMethodA(int x, int y) {
return ((x % 2) * (y % 2) == 0);
}
Answer: The product is zero if either (x % 2) is zero and/or (y %
2) is 0. (x%2) is 0 if x is even. (y%2) is zero if y is
even. And so, the method checks if x and/or y are even.
Note: you will receive zero credit for simply explaining,
step-by-step, what the method does. For example, this response
would receive zero credit: "The method takes the remainder when x
is divided by 2 and multiplies it by the remainder when y is divided by
2, and then the method returns true if this product is zero and false
otherwise." This is all entirely correct, but misses the point of
the question, which is to abstract away from the details and determine
the essence of the computation. Your final answer (the underlined
portion above), must be in just a few words of plain English.
- What does this method do (in general)?
public static
boolean mysteryMethodB(int x, int y) {
return (((x == 0) && (y == 0)) ||
((x != 0) &&
(y != 0) && ((x/y) + (y/x) == 2)));
}
Hint: there are several cases to consider! What if x>y?
x==y? x<y?
- What does this method do (in general)?
public static int
mysteryMethodC(int x,
int y) {
int a = x - y;
int b = 1 -
(3*a)/(3*a-1);
return b*x + (1-b)*y;
}
Non-Graphics Methods
Starting from Lab2.java, write the unfinished
methods. Note that we may include some tests in our autograder besides
those included in the provided test methods.
Painting Flags
Paint each of the following flags (one flag per Java file) using only
filled rectangles and filled ovals. You should use custom colors, matching
the colors in the flags as closely as possible. Also, your flags may not be
fixed-sized, but rather they must entirely fill the window, even when the
window is resized, leaving a 10-pixel gray margin on all sides (you are
not responsible for dealing with window sizes below 50x50). While the window's size may change, you may assume the
window will be roughly "flag-shaped" -- you will not be graded on how your
flags appear in, say, a tall thin window (which is not at all
"flag-shaped").
Note: All these flag images are from the very informational
CIA
World Factbook, which includes a
flags-of-the-world page.
Note: As we are limited to just rectangles and ovals this week, you
should replace any stars in flags with ovals (of the same size, location,
and color).
Note: This week, you are not
responsible for the "thin white stripe" that occurs between stripes when the
screen size is not an even multiple of the stripe size. Ask your CA
for details.
a. Benin
(file: Lab2FlagOfBenin.java)
(larger
image with details)
b. The Central African Republic
(file: Lab2FlagOfTheCentralAfricanRepublic.java)
(larger
image with details)
Carpe diem!