YCP Logo Problem Set 1

Assigned Wednesday, Sept 2nd

Do the following problems from Weiss:

  • 1.16
  • 1.17

Solutions

1.16

public class Prob1_16 {
        public static void main(String[] args) {
                System.out.printf("         --- Addition ---\n");
                System.out.print("   ");
                for (int i = 1; i <= 9; i++) {
                        System.out.printf("%3d", i);
                }
                System.out.printf("\n");

                for (int j = 1; j <= 9; j++) {
                        System.out.printf("%3d", j);
                        for (int i = 1; i <= 9; i++) {
                                System.out.printf("%3d", i + j);
                        }
                        System.out.printf("\n");
                }

                System.out.printf("\n");

                System.out.printf("         --- Multiplication ---\n");
                System.out.print("   ");
                for (int i = 1; i <= 9; i++) {
                        System.out.printf("%3d", i);
                }
                System.out.printf("\n");

                for (int j = 1; j <= 9; j++) {
                        System.out.printf("%3d", j);
                        for (int i = 1; i <= 9; i++) {
                                System.out.printf("%3d", i * j);
                        }
                        System.out.printf("\n");
                }
        }
}

1.17

public class Prob1_17 extends TestCase {
        public static int maxOf3(int a, int b, int c) {
                return maxOf4(a, b, c, c);
        }

        public static int maxOf4(int a, int b, int c, int d) {
                int max = a;
                if (b > max) {
                        max = b;
                }
                if (c > max) {
                        max = c;
                }
                if (d > max) {
                        max = d;
                }
                return max;
        }

        public void testMaxOf3() throws Exception {
                assertEquals(49, maxOf3(13, 49, 8));
                assertEquals(-10, maxOf3(-10, -100, -53));
        }

        public void testMaxOf4() throws Exception {
                assertEquals(81, maxOf4(52, 66, 81, 8));
                assertEquals(1001, maxOf4(11, -4, 121, 1001));
        }
}