Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Lab 3
Due: Mon 14-Sep-2009 at 11:59pm (email copy to your CA)
(no late submissions accepted).
Read these instructions first!
| a) |
public static void main(String[] args) {
int u = 11, v = 4;
int w = u/v;
double x = u/v;
double y = 1.0+u/v;
double z = 1.0*u/v;
System.out.println(w + "," + x + "," + y + "," + z);
w += ((u > v) ? u : v);
x *= ((u%v > v%u) ? u : v);
y /= ((w > x) ? ((x > Math.pow(y,2)) ? 4 : 3) : 2);
z = ((z < x) ? (int)(x/z) : (x/z));
System.out.println(w + "," + x + "," + y + "," + z);
} |
| b) |
public static void main(String[] args) {
// Remember: unicode for 'a' is 97
String s = "abcdefg";
int i = (int)Math.sqrt(s.length());
char c = s.charAt(i);
boolean b = ((c == 'c') ? (c<100) : (i<c/100));
System.out.println(s + "," + i + "," + c + "," + b);
if (s.charAt(s.length()-1) < c)
s += s;
else if (b == true)
s += c;
else
s += "Go Steelers!";
if ((i >= Math.sqrt(c)) || (b != (s.length() == 0)))
i *= 2;
else if (i < s.length())
i *= 4;
else
i *= 8;
b = !b;
c++;
if (c != s.charAt(i))
c++;
c++;
System.out.println(s + "," + i + "," + c + "," + b);
}
|
public static boolean g(int x, int y, int z) {
if (x < y) {
int temp = x;
x = y;
y = temp;
}
if (y >= z)
return (x == z);
else if (z >= x)
return (y == z);
else
return (x == y);
}
import javax.swing.*;
import java.awt.*;
class MysteryAnimation1 extends JComponentWithEvents {
private int rx = 0;
private int ry = 0;
public void timerFired() {
rx = ((rx < getWidth()) ? (rx+20) : 0);
ry += 20;
}
public void paint(Graphics2D page) {
int height = getHeight();
page.fillRect(rx, height-25-ry%height, 50, 50);
}
// Standard main method:
public static void main(String[] args) { launch(300, 300); }
}
import javax.swing.*;
import java.awt.*;
class MysteryAnimation2 extends JComponentWithEvents {
private double t = 0;
public void timerFired() {
t += 0.5;
}
public void paint(Graphics2D page) {
int cx = getWidth()/2, cy = getHeight()/2;
int r = 100;
int x = (int)Math.round(r*Math.cos(t)); // cosine, where t is in radians
int y = (int)Math.round(r*Math.sin(t)); // sine, where t is in radians
y = Math.abs(y);
page.fillOval(cx + x, cy - y, 20, 20);
}
// Standard main method:
public static void main(String[] args) { launch(300, 300); }
}
b. Ball Around Square
In the file Lab3BallAroundSquare.java, write the animation in
this jar file:
BallAroundSquare.jar (also
available in a zip file:
BallAroundSquare-jarfile.zip)
Notes:
Carpe diem!