Computer Science 15-100 (Lecture 18), Spring 2009
Homework 1
Due: Thu 22-Jan-2009 at 11:59pm (email copy) and at Friday's
class/recitation
(identical physical copy)
(no late submissions accepted).
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.
- Also include a timesheet logging all the time you spent on the
assignment.
While this is required, you will not be graded on your number of hours,
but this information will be helpful to the course staff. You can
include the timesheet in the header of a Java file or in its own text file (timesheet.txt).
It would be most helpful if you indicate roughly which problems you were
working on at various times (so we can identify if there are any issues with
time demands of these questions).
- 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.
- Try to 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, zip that
directory, and submit the zipped directory
- How to submit
- Send an email with the zipped submission directory as an attachment
to your CA 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.
- Short Answers
- Tracing
- Mystery Code
- Duplicate Detector
- Painting Flags
-
Bonus/Optional: Short
Answers
- 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 3-digit integer (between 100 and 999): ");
// You may assume the user enters a number in the desired range.
int x = scanner.nextInt();
int a = x%10;
int b = (x/10)%10;
int c = x/100;
int d = 100*a + 10*b + c;
System.out.println(d);
}
}
- 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.
- 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. 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 must deal with the "thin white stripe" that
occurs between stripes when the screen size is not an even multiple of the
stripe size.
a. Tonga
(file: Hw1FlagOfTonga.java)
(larger
image with details)
b. Togo
(file: Hw1FlagOfTogo.java)
(larger
image with details)
c. Mauritania
(file: Hw1FlagOfMauritania.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!
Carpe diem!