------------------------------------------------------------------------------- CS100J FALL 2001 MAKEUP EXAM SOLUTIONS Note: you might find some differences here. The actual exam solutions are slightly different in some places. You might also be able to come up with some more compact solutions, especially in Problem 3. ------------------------------------------------------------------------------- public class Problem1 { private static final int MAXCYCLES = 25; public static void main(String[] args) { Person p1 = new Person(); Person p2 = new Person(); int cycles = 0; while ( !p1.decideToStop() && !p2.decideToStop() && cycles < MAXCYCLES) { p1.addCalories(); p2.addCalories(); cycles++; } System.out.println(p1.getCalories() + p2.getCalories()); } } // Class Problem1 class Person { private int calories; private final int CALS_PER_CYCLE=10; public boolean decideToStop() { return MyMath.myRandom(1,100)<=5; } public void addCalories() { calories+=CALS_PER_CYCLE; } public int getCalories() { return calories; } } class MyMath { public static int myRandom(int low, int high) { return (int) (Math.random()*(high-low+1)) + (int) low; } } // Class MyMath ------------------------------------------------------------------------------- public class Problem2 { public static void main(String[] args) { final int MAXCARTS = 12; Cart prevCart = null; for (int count = 1; count <= MAXCARTS; count++) { prevCart = new Cart(prevCart); System.out.print(prevCart); } System.out.println("\n"+Cart.findMaxLength(prevCart)); } } class Cart { public Cart prevCart; private final int CHANCE = 25; // 25% chance to be empty private boolean empty; // false for filled! private String label = "X"; // X means filled, O means empty public Cart(Cart prevCart) { this.prevCart=prevCart; loadCart(); } public boolean noMoreCarts() { return prevCart==null; } private void loadCart() { if (MyMath.myRandom(0,100) <= CHANCE) { empty = true; label = " "; } } public boolean isEmpty() { return empty; } public String toString() { return label; } public static int findMaxLength(Cart c) { int maxLength = 0; // maxLength so far int currentLength = 0; // currentLength so far Cart currentCart = c; while(currentCart != null) { if (!currentCart.isEmpty()) { currentLength++; maxLength = Math.max(currentLength,maxLength); } else currentLength = 0; currentCart = currentCart.prevCart; } // should really output for no carts, 0 length.... return maxLength; } } class MyMath { public static int myRandom(int low, int high) { return (int) (Math.random()*(high-low+1)) + (int) low; } } ------------------------------------------------------------------------------- public class Problem3 { public static final int SIZE = 3; public static final int DAYS = SIZE; public static final int STACKS = SIZE; public static final int MAXHEIGHT = SIZE; public static final int MINITEMS = 1; public static final int MAXITEMS = 9; public static int count=0; private static int[][][] array; public static void main(String[] args) { createArray(); printArray(); } private static void createArray() { array = new int[DAYS][][]; // On a given day, create each stack // (there are $day$ number of stacks): for (int day=0; day=0; row--) { // Print blank for "empty" column: for (int col=0; col < STACKS-day-1; col++) System.out.print(" "); // Print element of col, if row exists: for (int col=0; col < array[day].length; col++) { if (row >= array[day][col].length ) System.out.print(" "); else System.out.print(array[day][col][row]); System.out.print(" "); // separate columns } // Skip to next line: System.out.println(); } // end printing a row // Skip a line: System.out.println(); } // end all printing } // Method printArray } // Class Problem3 class MyMath { public static int myRandom(int low, int high) { return (int) (Math.random()*(high-low+1)) + (int) low; } } -------------------------------------------------------------------------------