Computer Science 15-100 (Sections T & U), Spring 2008
Homework 4
Due: Fri 8-Feb-2008 at 10:00am (online submission) and at recitation
(physical copy)
(no late submissions accepted).
- Be sure to include your name, your Andrew ID, and your section (T or
U) clearly on the top of each file in your assignment.
- Name your programs exactly as indicated.
- Use well-named variables, proper indenting, reasonable commenting,
etc.
- Provide the UI exactly as indicated. File-based UI should not
include prompts.
- Submit printed solutions in your recitation. Your
printed solutions must be identical copies of your emailed solutions!
- Note: You may not use loops (do/while/for) or conditionals
("if" statements) to solve these problems
(which isn't a problem for most of you, seeing as we have not yet covered
loops!). While they may be helpful, every problem here is solvable without
them, and they are not permitted for now.
- Show your work (for the non-programming problems). Correct answers without supporting calculations
will not receive full credit.
- Do the following Exercises from Chapter 3 (pp 152-153):
Exercises 3.1 - 3.9 and 3.11.
- Write a program that uses the drawing methods we have covered to draw
each of the following graphics. Important note: the graphics must fill the entire
window, even as the window is being resized by the user. In this case, you
may get dimensions that make the flag look peculiar, but nonetheless it
should adjust reasonably to the window's dimensions. Also, this week you should
color-match, rather just using built-in colors.
- Hw4Q2a.java
(Solomon Islands,
larger image)
Hint: One way to find the exact color of blue in this flag (on
a Windows PC, at least) is to copy the screen using PrtScr (print
screen). Then run the Paint program, and paste the screen's image
into it. Then use the eyedropper tool and click on the light blue
in the flag. Finally, from the Colors menu, select "Edit Colors",
then "Define Custom Colors", and there you will see the RGB values of
this shade of blue are 154, 176, and 217 respectively. Now, to
create that exact color of blue in Java, just do this:
page.setColor(new Color(154,176,217));
Even better, to give this color a name, store it in a well-named
variable like this:
Color solomonLightBlue = new Color(154,176,217);
page.setColor(solomonLightBlue);
Another Hint: After you draw one star with a Polygon instance,
you should repeatedly use the Polygon's
translate
method to move this star into the other positions to draw the other
stars. Pretty handy!
- Hw4Q2b.java
(Israel,
larger image)
Hint: study BasicGraphicsDemo to see how to draw, rather
than fill, a polygon, and also how to change the line thickness.
- Hw4Q2c.java
(Optical Illusion: the horizontal lines and vertical lines are the
same length!)
- Write a program, Hw4Q3.java, that reads in four doubles from the file
in.txt representing the coefficients of two linear equations (y =
mx+b), where the two lines are guaranteed to intersect in one point, and
writes their point of intersection (with both x and y values rounded to the
nearest 1/10th) to the file out.txt. This program should work
like this:
in.txt
contents:
2.0 3.0 4.0 1.0
Program
output:
<none>
out.txt contents (after running program):
1.0 5.0
Explanation: the input represents the lines y=2x+3 and y=4x+1,
and the output indicates that these two lines intersect at the point (1,5).
Note, however, that the output is not "1" but "1.0", as the output should
use printf to round the value to the nearest 1/10th.
Hint: The lines y1 = m1*x + b1 and y2 = m2*x+b2 intersect
where y1 = y2. That is, where:
m1*x + b1 = m2*x + b2
Now solve for x, then plug that value of x back into either equation to
solve for y.
- Write a program, Hw4Q4.java, that reads in two Strings from the file
in.txt and writes "true" to the file out.txt if
either of the strings contains exactly one copy of the other string
(without regard to case), and false otherwise. This program should work like this:
| in.txt contents |
program output |
out.txt contents |
explanation |
| AbCdEf Bc |
<none> |
true |
Bc is contained once in AbCdEf |
| BC abcdef |
<none> |
true |
BC is contained once in abcdef |
| efg abcdef |
<none> |
false |
neither string is contained in the other |
| ab abcabc |
<none> |
false |
ab occurs twice in abcabc |
| bb bbb |
<none> |
false |
bb occurs twice in bbb |
| bb bb |
<none> |
true |
bb occurs once in itself |
- Write a program, Hw4Q5.java, that reads in one even non-negative integer from the file
in.txt representing a number of years, and
using Moore's Law, writes how many times faster computers will operate that
many years from now to the file out.txt. This program should work
like this:
in.txt
contents:
10
Program
output:
<none>
out.txt contents (after running program):
32
Explanation: the input represents 10 years, which by Moore's
Law includes 5 doublings in speed, so computers in 10 years will be about 25
or 32 times faster. Hence the output is the number 32.
Hint: As the input is an even integer, you are assured
the result will also be an integer. Be sure to output the correct
integer, taking double math into account.
- Write a program, Hw4Q6.java, that performs the opposite calculation of
the previous problem -- that is, it reads in one non-negative integer that
is a power of 2 from the file
in.txt representing how many times faster computers will be in the
future as compared to today's computers, and using Moore's Law, writes how
many years from now computers will achieve that speed to the file out.txt. This program should work
like this:
in.txt
contents:
32
Program
output:
<none>
out.txt contents (after running program):
10
Explanation: the input represents 32 times faster, which is 25
times faster, which is 5 doublings of speed, which by Moore's Law will
require about 5*2, or 10 years. Hence the output is the number 10.
Hint: As the input is a power of 2, you are again
assured the result will also be an integer. Be sure to output the
correct integer, taking double math into account.
Carpe diem!