Computer Science 15-100 (Sections T & U), Spring 2008
Homework 7b
Due: Fri 29-Feb-2008 at 10:00am (online submission) and at recitation
(physical copy)
(no late submissions accepted).
class MyCode {
public static void myMysteryA() {
int x=1;
do
x++;
while (x++ < 3);
System.out.format("%d\n",x);
}
public static void myMysteryB() {
int x=1, y=2;
for (x=3; x==3; x++) {
x -= 3/y;
y++;
}
System.out.format("%d,%d\n",x,y);
}
public static void myMysteryC() {
int x=1, y=3;
if ((++x < y) || (++y < x)) x += 10;
System.out.format("%d,%d\n",x,y);
}
public static void myMysteryD() {
int x=1, y=8;
String z = "";
while (x < y) {
if (x % 2 == 1)
if (y % 2 == 1)
x++;
else
y--;
else
y /= 2;
z += y;
}
System.out.format("%d,%d,%s\n",x,y,z);
}
public static void myMysteryE() {
int x=1, y=6;
while (x <= y) {
x += ((y % x > 0) ? 2 : 1);
y--;
}
System.out.format("%d,%d\n",x,y);
}
public static void myMysteryF() {
int x=0,y=0;
for (int i=0; i<5; i++)
switch (i/2) {
case 1: x++;
break;
case 2: y++;
// note: no break!
default: x++;
}
System.out.format("%d,%d\n",x,y);
}
public static void myMysteryG() {
int x=1, y=3, z=5;
while (x < y*z) {
x += 1;
y -= 2;
z += 3;
}
System.out.format("%d,%d,%d\n",x,y,z);
}
public static void myMysteryH() {
int x=0,y=0;
for (int i=10; i>0; i--) {
x++;
if (i % 3 == 1)
continue;
y++;
if (i % 4 == 2)
break;
x++;
}
System.out.format("%d,%d\n",x,y);
}
public static void myMysteryI() {
for (int i=0; i<100; i++) {
int x = i % 10, y = i / 10;
if (x + y > 16)
System.out.format("%d,%d\n",x,y);
}
}
public static void myMysteryJ() {
int x=0;
for (int i=0; i<10; i++) {
for (int j=0; j<5; j++)
x++;
x++;
}
System.out.format("%d\n",x);
}
public static void main(String[] args) {
System.out.println("A:"); myMysteryA();
System.out.println("B:"); myMysteryB();
System.out.println("C:"); myMysteryC();
System.out.println("D:"); myMysteryD();
System.out.println("E:"); myMysteryE();
System.out.println("F:"); myMysteryF();
System.out.println("G:"); myMysteryG();
System.out.println("H:"); myMysteryH();
System.out.println("I:"); myMysteryI();
System.out.println("J:"); myMysteryJ();
}
}
Carpe diem!