Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Lab 4
Due: Thu 24-Sep-2009 at 11:59pm
(email copy to your CA)
(no late submissions accepted).
Read these instructions first!
- The lab instructions from
lab2
all apply here.
- As discussed in class,
collaboration is required on
this lab. You must work in groups for any and all parts of this
lab (no solo work!). List all your group members (names + andrewId's)
in a file, Lab4Log.txt, along with the times that you worked with them.
Of course, they should list all the same people and all the same times in
their log files, too!
- As explained in that document:
- Submit your free-response answers in a
single file named Lab4.txt (or .doc, .pdf, .rtf, or .html -- but not
.docx!).
- Your top-level directory (which must exist)
should be named lab4-<andrewId>. Submit a zipped version of this.
- Do not submit .java~ files, .class files,
project files, or any other extraneous files.
Programming guidelines:
- Style counts!!! Use well-named variables,
proper indenting, reasonable commenting, etc.
- Note: You may use conditionals ("if" statements
or ternary operators (?:)) and loops (do/while/for). However, you may
not use Java concepts we have not yet covered, including 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
- Mystery Code
- Non-Graphical
Programming Problems
- factorial
- gcd
- lcm
- indexOf
- lastIndexOf
- isSubstring
- nthHappyNumber
- Painting Flags (United States)
-
Bonus/Optional:
wordsearchContains
- 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 possibly-negative integer values,
and "a", "b", and "c" are boolean values, and ignore any
errors (such as dividing by zero):
- The following code does not loop forever because of ____________.
int i=0;
while (i
<= 0)
i--;
- If ((Math.pow(x,2) < 15) &&
(Math.abs(x) > x)) is true, then x must be between ____________
and ___________ (inclusive).
- Fill
in the blank with one of “always”, “sometimes”, or “never”.
Be sure to show your work, using a truth table to
prove your answer:
((a || b) ? (!a && b) : (a || !b))
________________ equals (!a)
- Mystery Code
In the written portion of your submission, state what
each of the following methods do in general, and in just a few words of
plain English.
a)public static boolean f(int x) {
assert(x > 0);
boolean b = false;
while (x > 0) {
if (x % 10 == 3)
b = !b;
x = x/10;
}
return b;
}
b)public static String g(int x) {
if (x == 0) return "0";
String s = "";
int y = Math.abs(x);
while (y > 0) {
s = (y%2) + s;
y /= 2;
}
if (x < 0) s = "-" + s;
return s;
}
- Non-Graphical
Programming Problems
Do the following problems as defined by their comments in the file
Lab4.java. Read the comments and review the test
cases carefully!
- factorial
- gcd
- lcm
- indexOf
- lastIndexOf
- isSubstring
- nthHappyNumber
- Painting Flags (United States)
Paint the United
States flag using loops for the stripes and the stars (which
will be painted as 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. 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: 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).
a. United States
(file: Lab4FlagOfUnitedStates.java)
(larger
image with details)
-
Bonus/Optional:
wordsearchContains
Write the following method:
public static
boolean wordsearchContains(String wordsearch, String word)
This method takes a two strings, the first representing a wordsearch
(stored in a single string!), and returns true if the second string occurs
in that wordsearch and false otherwise.
To represent a wordsearch as a string, we simply store each row concatenated
to the next where we separate the rows with a | (that is, a vertical bar, or
a pipe). So, consider this wordsearch:
t a c w
n o o c
d o g x
The rows are: "tacw", "nooc",
and "dogx", so we represent the entire
wordsearch as: "tacw|nooc|dogx".
Note that this wordsearch contains:
cat
cod
coon
dog
ox
You are guaranteed the wordsearch contains only lowercase letters, and that
the rows are all the same positive length (you do not need to handle other
cases in any way).
Here is a test method for you: public static void testWordsearchContains() {
System.out.print("Testing wordsearchContains... ");
// These tests cover this wordsearch:
// t a c w
// n o o c
// d o g x
assert(wordsearchContains("tacw|nooc|dogx", "cat"));
assert(wordsearchContains("tacw|nooc|dogx", "cod"));
assert(wordsearchContains("tacw|nooc|dogx", "coon"));
assert(wordsearchContains("tacw|nooc|dogx", "dog"));
assert(wordsearchContains("tacw|nooc|dogx", "ox"));
assert(!wordsearchContains("tacw|nooc|dogx", "caw"));
assert(!wordsearchContains("tacw|nooc|dogx", "cow"));
assert(!wordsearchContains("tacw|nooc|dogx", "con"));
assert(!wordsearchContains("tacw|nooc|dogx", "dogs"));
assert(!wordsearchContains("tacw|nooc|dogx", "ax"));
System.out.println("Passed all tests!");
}Note: This approach of representing a 2-dimensional board in a
1-dimensional string is not a very good idea. Soon, we will learn about
other ways we can more naturally represent this data, such as in a
2-dimensional array.
Carpe diem!