Computer Science 15-110 (Lecture 4), Spring 2010
Homework 1
Due: Thu 21-Jan-2009 at 10pm (email copy to koz)
(no late submissions accepted).
Hw1 Submission Coordinator: koz
Read these instructions first!
- Be sure to include your name, your Andrew ID, and your section clearly on the top of
each file in your assignment.
- For non-programming problems:
- Place all your solutions to the non-programming problems in a single
file named Hw1 (with whatever extension is appropriate for the format you
choose, such as Hw1.txt or Hw1.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.
- For programming problems:
- Place each solution in its own file named exactly as given below, and
with a class name that exactly matches the file name. So if the file name
is Hw1Foo.java, the main class in that file must be Hw1Foo.
- 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 tertiary 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.
- What to submit
- Create a submission directory like this: "koz-hw1" (replace
"koz" with your andrew id)
- Place all the files you are submitting in that directory (and not in a
subdirectory!), zip that
directory, and submit the zipped directory
- How to submit
- Send an email with the zipped submission directory as an attachment
to the hw1 submission coordinator by the submission deadline. Do not miss the deadline,
even by one minute! Email problems are not a valid excuse
for late submissions.
- It is recommended that you "cc" yourself in that email, too, just to
confirm that you properly sent the email.
- Note:
- Improper submissions will be penalized up to 10 points and may be
rejected.
- Late submissions will be rejected.
- Attend Recitation
- Short Answers
- Tracing
- Mystery Code
- Console Programs
- nearestBusStop
- lineIntersection
- Painting Flags
- Central African Republic
- Netherlands Antilles
-
Bonus/Optional: Short
Answers
-
Bonus/Optional: Duplicate Detector
- Attend Recitation
This is worth 10% of the hw. Students who attended recitation #1
receive this credit. Those who did not attend do not receive credit.
Attending recitation is a required element of this course. Please be
sure to attend all future recitations (whether or not credit is given).
Ride the bike!!!
- 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):
- (x % 1) always equals ____________.
- (x % x) always equals ____________.
- If x > 1, then (1 % x) always equals __________.
- If x < y, then (x / y) always equals ________.
- If x < y, then (x % y) always equals ________.
- 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.
- Mystery Code
In the written portion of your submission, state what the following program
does in general, and in just a few words of plain English.
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!// Mystery code
import java.util.Scanner;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a 4-digit integer (between 1000 and 9999): ");
// You may assume the user enters a number in the desired range.
int x = scanner.nextInt();
int a = x%10;
System.out.println(1000*a + x/10);
}
}
- Console Programs
- nearestBusStop
Write a program, Hw1NearestBusStop.java, that reads a street number
(which you may assume is positive), and prints the street number of the
nearest bus stop, where buses stop on streets that are multiples of 8 (8th,
16th, 24th, etc). So we see:
* For 11th street, the nearest bus stop is 8th street.
* For 13th street, the nearest bus stop is 16th street.
* For 12th street, however, both 8th street and 16th street are equally far,
but we prefer the lower bus stop, which is 8th street.
You can assume there is a bus stop on 0th street. Your program must
match the following output exactly (where the user input is
underlined, and therefore can change each time you run the program):
Enter the street
number: 12
------------------------
The nearest bus stop to street 12 is at street 8.
Remember: do not use conditionals, loops, arrays, etc. This can be done
(in one line of code!) using only what we have covered Week #1's notes (in
fact, using just addition, division and multiplication of integers).
- lineIntersection
Write a program, Hw1LineIntersection.java, that reads 4 integers: the
slope m1 and y-intercept b1 of line1, then the slope m2 and y-intercept b2
of line2. The program should print the (x,y) coordinate of the point
of intersection of the two lines. You may assume the two lines
intersect at a point where both x and and y are integers. Your program
must match the following output exactly (where the user input is
underlined, and therefore can change each time you run the program):
Enter two lines that
intersect at a point (x,y)
where both x and y are integers:
Enter m1:
5
Enter b1: 2
Enter m2: 3
Enter b2: 4
------------------------
These lines intersect at (1,7).In this example, we are
intersecting the lines y=5x+2 and y=3x+4 (see?). These lines intersect
at the point (1,7).
Hint: To write this program, you will have to do some simple algebra
to find the point of intersection of the equations y1=m1x+b1
and y2=m2x+b2. Since the y value must
be the same when these lines intersect (right?), you can simply set these
two equations equal to each other (so y1=y2, that is,
m1x+b1 = m2x+b2) and solve for
x. Then, plug this value of x into either equation to find y.
- 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, and you may not draw outside of the visible window. 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: You must deal with the "thin white stripe" that
occurs between stripes when the screen size is not an even multiple of the
stripe size.
Note: You do not have to be "pixel perfect" in positioning the
stars, but you must be "very close", even as the window size changes.
In particular, you have to maintain the obvious alignment properties.
For example, in the first flag below (Central African Republic), the star
must remain vertically centered in the blue stripe. In the second flag
below (Netherlands Antilles), the top star must be centered horizontally,
the middle two stars must be centered vertically and lie entirely outside
the red stripe, and the lower two stars must be left-flush and right-flush
respectively with the red stripe.
a.
Central African Republic
(file:
Hw1FlagOfTheCAR.java)
Note: Due to a DrJava bug, it cannot properly run files with
names as long long as "Hw1FlagOfTheCentralAfricanRepublic.java" (even though
they are legal Java file names, and other IDE's, like JCreator, can run this
just fine). Thus, we have to abbreviate the name as we have here.
(larger
image with details)
b. Netherlands Antilles
(file: Hw1FlagOfTheNetherlandsAntilles.java)
(larger
image with details)
-
Bonus/Optional: 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):
- Find a Java expression that always equals (x % y) but that does not
use the % operator.
- If (x / y) equals (y % x), then what do we know must be true
about x and y? Prove it (as best you can).
- If ((x – 1) % y) / (y – 1) is
non-zero, then what do we know must be true about x and y? Again,
prove it as best you can. Hint: remember that this
expression uses integer division, and so truncates as needed!
-
Bonus/Optional: Duplicate Detector
In the file Hw1DuplicateDetector.java, write a program that reads in three
integer values and prints out 0 if there are any duplicate values (that is,
fewer than three unique values were entered) and prints out 1 otherwise
(that is, three unique values were entered). Order should not matter.
So, for example, your program should print out 0 if the user enters 3, 2, 2
or 2, 3, 2, or 2, 2, 3, or 2, 2, 2.
Carpe diem!