Computer Science 15-100 (Sections T & U), Spring 2008
Homework 3
Due: by email on Fri 1-Feb-2008 at 10:00am (email copy) 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.
- Note that we may have a modified online submission process for
this assignment. This will be announced in class on Thursday
31-Jan-2008.
- In any case, 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 2 (pp 104-106):
Exercises 2.3 - 2.11 and 2.17 - 2.18.
- Write a program, Hw3Q2.java, that reads in two doubles from the file
in.txt and writes "true" to the file out.txt if the doubles
are "almost equal", and false otherwise. We'll say that two doubles
are almost equal if they are within 0.001 of each other (regardless of which
is the larger value). This program should work like this:
in.txt
contents:
2.001 2.000
Program
output:
<none>
out.txt contents (after running program):
true
Of course, your program should work for any legal input!
Remember to exactly match the output!
Hint: Remember that we cannot use "==" to test doubles for
equality. That is why we use "amost equals". Also, you may wish
to use the Math.abs method, which computes the absolute value of its only
argument.
- Write a program, Hw3Q3.java, that reads in six doubles from the file
in.txt, representing the 3 endpoints of a triangle (as described
below), and writes "true" to the file out.txt if the triangle is a
right triangle, and false otherwise. If we refer to the doubles, in
order, as x0, y0, x1, y1, x2, y2, then these represent the points (x0,y0),
(x1,y1), and (x2,y2). If you connect these 3 points to each other, you
will get a triangle. Now, it is a right triangle if it obeys the
Pythagorean Theorem -- a2 + b2 = c2, where
c is the length of the hypotenuse, and a and b are the lengths of the other
legs. So we have two challenges here.
First, we have to find the length of a side given its endpoints. This
can be done using the distance formula. The distance from the point
(x1, y1) to the point (x2, y2) is given by this formula:

Note that you may want to use the Math.pow method that raises its first
argument to the power of the second argument. So, for example,
Math.pow(2,3) computes the value of 23, and Math.pow(3,8)
computes the value of 38. For the square root, you can
either raise the value to the 0.5, or if you prefer, you can use the
Math.sqrt method that computes the square root of its one argument.
Now that we have the lengths of the three sides, we still have the problem
that we do not know which one may be the hypotenuse. Answer: if
it's a right triangle, the hypotenuse is the longest side. So,
set "c" equal to the longest side (using Math.max). Then, set "a"
equal to the shortest side (with Math.min). Finally, set "b" to the
remaining side (basically, b is the median of the side lengths (right?), and
you may recall from hw2 how to compute this).
Now that we have side lengths a, b, and c (the hypotenuse, if this is a
right triangle), we want to determine whether a2 + b2
= c2. The final issue is that these are double values, and
we cannot use "==" to test for equality here. Instead, use the
technique from the previous problem, and assume the values are equal if they
are within 0.001 of each other.
This program should work like this:
in.txt
contents:
0 3 4 0 0 0
Program
output:
<none>
out.txt contents (after running program):
true
This example includes the points (0,3), (4,0), and (0,0).
The lengths are 3, 4, and 5, and 32 + 42 = 52.
So this is a right triangle.
Of course, your program should work for any legal input!
Remember to exactly match the output!
Hint: Remember that we cannot use "==" to test doubles for
equality. That is why we use "amost equals". Also, you may wish
to use the Math.abs method, which computes the absolute value of its only
argument.
- Write a program, Hw3Q4.java, that reads in a String (consisting of
exactly 3 UPPERCASE letters) from the file
in.txt and writes an encoded version of the String to the file out.txt.
To encode the string, replace 'A' with 'B', 'B' with 'C', and so on, and
replace 'Z' with 'A'. Thus, this program should work like this:
in.txt
contents:
YAZ
Program
output:
<none>
out.txt contents (after running program):
ZBA
Of course, your program should work for any legal input!
Remember to exactly match the output!
Hint: To read in a String, use the scanner.next method (not
scanner.nextString, just scanner.next). Also, to extract a character
from a String, use the charAt method described in class.
Note: This is a simple example of a so-called Caesar Cypher.
See the Wikipedia page
for more details.
- Write a program, Hw3Q5.java, that reads in a String (consisting of
exactly 3 UPPERCASE letters) from the file
in.txt and writes the decoded version of the String to the file out.txt.
This problem basically is the opposite of the previous problem -- here you
start with an encoded string and you must produce the original string.
Thus, this program should work like this:
in.txt
contents:
ZBA
Program
output:
<none>
out.txt contents (after running program):
YAZ
Of course, your program should work for any legal input!
Remember to exactly match the output!
- Write a program that uses the drawing methods we have covered to draw
each of the following flags. Important note: the flag 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, you should
use built-in colors (like Color.green) rather than custom colors.
- Hw3Q6a.java
(Mauritius)
- Hw3Q6b.java
(Benin)
- Hw3Q6c.java
(Maldives)
- Hw3Q6d.java
(Czech Republic)
- Hw3Q6e.java
(St Vincent and the Grenadines)
Carpe diem!