Computer Science APEA 15-100, Summer 2009
Lab 14
Read these instructions first!
- The homework collaboration policy does not apply to labs. In
labs, you may work alone or in small groups. And you may show each
other your code, and help each other in any manner. This is
encouraged, in fact!
- That said, the participation policy is still in effect -- you must
not only attend labs, but actually work during the labs. If you have
completed the assigned work, then you should either help others, if they
request it, or you should delve deeper into the assigned material.
But you must use the lab time exclusively to explore the material covered in
that lab.
- In particular, you may not use lab time to do your homework.
- There is nothing to submit for labs.
- ComplexNumber Class
- Tracing Code
- Mystery Methods
- Astroid Drawing
- ComplexNumber Class
Based on your experience with the Ratio class, write a new class,
ComplexNumber (and ComplexNumberDemo), where a complex number is a pair of
doubles, "a" and "b", representing the number (a + bi), where i equals the
square root of -1. Your ComplexNumber class should include a reasonable
constructor (that takes two doubles, "a" and "b") and public methods to add,
subtract, multiply, divide, and invert (which are explained succinctly in
the Operations
section of the Wikipedia page on Complex Numbers). Also include test
code for each public method.
- Tracing Code
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.
Hint: No loop runs for more than 6 iterations (so if your answer is doing
so, stop and check your work!).
- Mystery Methods
State what the following program does in general, and in just
a few words of plain English. (No credit will be awarded
for stating what it does line-by-line, expression-by-expression -- instead,
find the general pattern and state is in just a few word.)
Note: some of these methods may seem a bit confusing,
and in fact some are, in that there may be some much easier, clearer, and
more sensible ways to do the same thing.
-
public static boolean f(int x) {
boolean b = false;
while (x != 0) {
if (x % 10 == 3)
b = !b;
x = x/10;
}
return b;
}
-
public static int f(int
x, int y) {
int a = x - y;
int b = 1 -
(3*a)/(3*a-1);
return b*x + (1-b)*y;
}
-
public static int f(int x) {
int a = x%10;
int b = (x/10)%10;
int c = x/100;
int d = 100*a + 10*b + c;
return d;
}
-
public static int f(int x, int y) {
return (x % 2) * (y % 2);
}
- public static int
f(int n) {
int result = 0;
for (int i=1; i<=n; i++) {
int sum = 0;
for (int j=1; j<=n; j++)
sum += j;
result += 2*sum - n;
}
return result;
}
- public static
boolean f(int n) {
int s = (int)Math.round(Math.sqrt(n));
if (s*s != n) return false;
int i = s/n;
while (i*i < s)
i++;
return (i*i == s);
}
- public static int
f(String s) {
int result = 0;
while (s.length() > 0) {
String t = "";
for (int i=s.length()-1; i>=0; i--) {
char c = s.charAt(i);
if ((c >= '0') && (c < '9'))
t += ((char)(c+1));
else if ((c > '0') && (c <= '9'))
result++;
}
s = t;
}
return result;
}
-
Note: This method uses a "switch" statement. While
you are not responsible for writing code that uses "switch" statements,
you should be able to briefly read about switch statements and
understand enough to solve this problem under lab conditions.
public static String f(int n) {
char b = (char)('a'+3);
for (int i=n; i/2*2==i; i--) b++;
String c = "", d = "mccain";
switch (b/'e') {
case 3: c = "obama";
case 2: d = "huckabee";
case 0:
break;
case 1: char g = 'z';
for (int i='a'; i<b; i++) g--;
c += g;
default:
d = "clinton";
}
return ((n % 2 == 1) ? "o" : "") + b + c + b + d.substring(6);
}
-
public String
f(String s, int i) {
if ((s == null) || (i < 0) || (i >= s.length()))
return s;
String result = "";
for (int j=0; j<s.length(); j++)
if (i != j)
result += s.charAt(j);
return result;
}
-
public boolean f(String s1, String s2) {
if ((s1 == null) || (s2 == null))
return false;
for (int i=0; i<s1.length(); i++)
if (s2.equals(f(s1,i)))
return true;
return false;
}
- Astroid Drawing
Write and test this graphics method:
public void
fillAstroid(Graphics page, Color color, int cx, int cy, int r)
This method takes a page, a color, a center point (cx,cy), and a radius, and
draws a single filled astroid
(the type of hypocycloid in the Steelers logo) in the given color that is
just contained by the circle with the given center and radius. Do this as
follows:
1. Draw "x" and "y" axes centered inside the circle.
2. Place n evenly-spaced points along each of the x and y axes. (You do
not have to actually draw these points, but you will use them in the next
step...)
3. In the first quadrant:
* Draw a line from (0, n) to (1, 0)
(That is, from the highest point on the y-axis to the first point
to the right on the x-axis)
* Draw a line from (0, n-1) to (2, 0)
(That is, from the second highest point on the y-axis to
the second point to the right on the x-axis)
* And so on (as you move down the y axis, you move right on the x
axis). Of course, you must use a loop to draw all these lines!
4. Do the same for the other three quadrants.
Here is a picture of an astroid drawn in this manner, with about 10 points
on each positive or negative coordinate axis:

Carpe diem!