CS42 Review Submission 4 MOCK AP COMPUTER SCIENCE A FREE-RESPONSE SOLUTIONS Michael Yao CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 Question 1: Climbing Stairs This question involves reasoning about a simulation of a person named Alice attempting to climb a set of stairs. The staircase has a total of n steps to climb, and Alice can either climb 1 stair per step at a time or 2 stairs per step at a time. Alice can take some combination of 1 stair- and 2 stair- steps to climb up the n steps. This simulation is encapsulated in the following ClimbingStairs class. You will write two of the methods in this class. 1 public class ClimbingStairs { 2 /** Number of stair steps to climb. **/ 3 private int n; 4 5 /** Constructs a ClimbingStairs where n is the number of steps to climb. 6 * Precondition: n > 0 7 **/ 8 public ClimbingStairs(int numSteps) { 9 n = numSteps; 10 } 11 12 /** Counts the number of different ways that Alice can climb the stair case. **/ 13 public int numWays() { 14 /* Implementation not shown. To be implemented in part (a) */ 15 } 16 17 /** Counts the mean number of steps that Alice will take, averaged over 18 * all of the possible ways to climb the stair case. 19 **/ 20 public double meanStepNumber() { 21 /* Implementation not shown. To be implemented in part (b) */ 22 } 23 } GO ON TO THE NEXT PAGE. 1 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 (a) Write the numWays method, which counts the number of different ways that Alice can climb the stair case, given that she can only climb either 1 or 2 stairs per step. The method returns an int that represents the number of different ways that Alice can climb the stair case with n stair steps subject to the constraints of the problem. For example, if there are n = 3 stairs on the stair case, then the numWays method should return 3. This is because there are three different ways to climb the n = 3 stairs: (1) each step covers 1 step at a time, and do this 3 times, (2) the first step covers 1 step, and the second step covers 2 steps, and (3) the first step covers 2 steps, and the second step covers 1 step. Class information for this question public class ClimbingStairs private int n public int numWays() public double meanStepNumber() 1 /** Counts the number of different ways that Alice can climb the stair case. **/ 2 public int numWays() { 3 return numWays(n); 4 } 5 6 private int numWays(int n) { 7 if (n < 0) { return 0; } 8 else if (n == 0) { return 1; } 9 return numWays(n - 1) + numWays(n - 2); 10 } GO ON TO THE NEXT PAGE. 2 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 (b) Write the meanStepNumber method, which counts the mean number of steps that Alice will take to climb the n stairs. The mean is calculated by averaging the number of steps taken over all of the possible ways to climb the stair case. This method returns a double that represents that mean number of steps that Alice will take to climb n stair steps subject to the constraints of the problem. For example, if there are n = 3 stairs on the stair case, then the meanStepNumber method should return 7/3. This is because there are three different ways to climb the n = 3 stairs: (1) each step covers 1 step at a time, and do this 3 times, leading to a total of 3 steps taken; (2) the first step covers 1 step, and the second step covers 2 steps, leading to a total of 2 steps taken; (3) the first step covers 2 steps, and the second step covers 1 step, leading to a total of 2 steps taken. Therefore, the mean step number (MSN) is given by (3 steps) + (2 steps) + (2 steps) 7 MSN = = steps (1) 3 different ways 3 1 /** Counts the mean number of steps that Alice will take, averaged over 2 * all of the possible ways to climb the stair case. 3 **/ 4 public double meanStepNumber() { 5 public class ClimbSteps { 6 public int steps = 0; 7 8 public void addSteps(int n) { 9 if (n <= 0) { return; } 10 steps += 1; 11 addSteps(n - 1); 12 addSteps(n - 2); 13 if (n > 1) { 14 steps += 1; 15 addSteps(n - 2); 16 } 17 } 18 } 19 20 ClimbSteps climb = new ClimbSteps(); 21 22 return (double)climb.addSteps(n) / numWays(); 23 } GO ON TO THE NEXT PAGE. 3 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 Question 2: Hungry Kittens This question discusses a group of kittens that happen to be very hungry and want different amounts of food for dinner. There are a total of n kittens, each numbered from the i = 0 kitten to the i = n − 1 kitten. The i-th kitten needs fi milliliters of food. The owner of the kittens has a total of f milliliters of food to give to his kittens for dinner. He doesn’t know whether he has enough food to feed all of the kittens, and so he will feed them in order of the kittens’ individual “cuteness levels.” Kitten i with a cuteness level pi will be completely fed before a kitten j with a cuteness level pj only if pi > pj . It is given that no two kittens have the same cuteness level. Each kitten is represented by the Kitten class. The public methods available to call are shown below. The simulation of the owner feeding the n kittens for dinner is encapsulated in the following DinnerFeeding class. You will write three of the methods in this DinnerFeeding class. 1 public class Kitten { 2 /** Constructs a Kitten with cuteness level cuteness that 3 * needs to each diet milliliters of food for dinner. 4 * Precondition: cuteness > 0; diet > 0 5 **/ 6 public Kitten(int cuteness, int diet) { 7 /* Implementation not shown. */ 8 } 9 10 /** Returns the milliliters of food that the kitten needs to eat for dinner. **/ 11 public int getDiet() { 12 /* Implementation not shown. */ 13 } 14 15 /** Returns the cuteness level of the kitten. **/ 16 public int getCuteness() { 17 /* Implementation not shown. */ 18 } 19 } GO ON TO THE NEXT PAGE. 4 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 1 public class DinnerFeeding { 2 /** List of kittens. The i-th element of the list is kitten numbered as i. **/ 3 List<Kitten> kittens; 4 5 /** Constructs a DinnerFeeding simulation involving numCats kittens. 6 * Initializes kitten as an ArrayList with numCats kittens. The cuteness 7 * levels of each of the kittens is given in cutenessLevels, and the 8 * amount of food (in milliliters) that each of the kittens need is given 9 * in dietLevels. 10 * Precondition: numCats > 0; all elements in cutenessLevels are 11 * positive; all elements in dietLevels are positive 12 **/ 13 public Kitten(int numCats, int[] cutenessLevels, int[] dietLevels) { 14 /* Implementation not shown. */ 15 } 16 17 /** Returns how many kittens will be fed with a total of fTotal 18 * milliliters of food for all of the cats. 19 * Precondition: fTotal > 0 20 **/ 21 public int kittensFed(int fTotal) { 22 /* Implementation not shown. To be implemented in part (a) */ 23 } 24 25 /** Returns the amount of food (in milliliters) remaining after all of 26 * the kittens that can be completely fed with fTotal total milliliters of 27 * food are fed. 28 * Precondition: fTotal > 0 29 **/ 30 public int remainingFood(int fTotal) { 31 /* Implementation not shown. To be implemented in part (b) */ 32 } 33 34 /** Returns a List of Kitten’s that were not fed fully. 35 * Precondition: fTotal > 0 36 **/ 37 public List<Kitten> notFed(int fTotal) { 38 /* Implementation not shown. To be implemented in part (c) */ 39 } 40 } GO ON TO THE NEXT PAGE. 5 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 (a) Write the kittensFed method, which takes in the amount of food (in milliliters) that all of the kittens will be fed in total, and determines the number of kittens that are able to be fed with the food available. If a kitten will only be partially fed instead of fully fed, the kitten should not receive any food and should be counted as unfed. Remember that kittens can only be fed in order of decreasing cuteness level. For example, consider the case of when there are n = 3 kittens that need to be fed. Kitten 0 needs 100 mL of food and has cuteness level 7, kitten 1 needs 200 mL of food and has cuteness level 3, and kitten 2 needs 150 mL of food and has cuteness level 8. If there is 400 mL of dinner food total available, then kittensFed should return 2, because only kittens 2 and 0 can be fed, with 150 mL of food left over. Class information for this question public class Kitten public int getDiet() public int getCuteness() public class DinnerFeeding List<Kitten> kittens public int kittensFed(int fTotal) public int remainingFood(int fTotal) public List<Kitten> notFed(int fTotal) 1 /** Returns how many kittens will be fed with a total of fTotal 2 * milliliters of food for all of the cats. 3 * Precondition: fTotal > 0 4 **/ 5 public int kittensFed(int fTotal) { 6 int numFed = 0; 7 List<Kitten> fedKittens = new ArrayList<>(); 8 9 while (true) { 10 if (fedKittens.size() == kittens.size()) { return numFed; } 11 Kitten toFeed; 12 for (Kitten cat : kittens) { 13 if (toFeed == null && !fedKittens.contains(cat)) { toFeed = cat; } 14 else if (toFeed == null) { continue; } 15 16 if (toFeed.getCuteness() < cat.getCuteness() && !fedKittens.contains(cat)) 17 { 18 toFeed = cat; 19 } 20 } 21 if (fTotal < toFeed.getDiet()) { return numFed; } 22 else { 23 fTotal -= toFeed.getDiet(); 24 numFed++; 25 fedKittens.add(toFeed); 26 } 27 } 28 return numFed; 29 } GO ON TO THE NEXT PAGE. 6 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 (b) Write the remainingFood method, which takes in the amount of food (in milliliters) that all of the kittens will be fed in total, and determines the food (in milliliters) remaining after all of that kittens that can be fed are fed. If a kitten will only be partially fed instead of fully fed, the kitten should not receive any food and should be counted as unfed. Remember that kittens can only be fed in order of decreasing cuteness level. For example, consider the case of when there are n = 3 kittens that need to be fed. Kitten 0 needs 100 mL of food and has cuteness level 7, kitten 1 needs 200 mL of food and has cuteness level 3, and kitten 2 needs 150 mL of food and has cuteness level 8. If there is 400 mL of dinner food total available, then kittensFed should return 150, because only kittens 2 and 0 can be fed, with 150 mL of food left over. 1 /** Returns the amount of food (in milliliters) remaining after all of 2 * the kittens that can be completely fed with fTotal total milliliters of 3 * food are fed. 4 * Precondition: fTotal > 0 5 **/ 6 public int remainingFood(int fTotal) { 7 List<Kitten> fedKittens = new ArrayList<>(); 8 9 while (true) { 10 if (fedKittens.size() == kittens.size()) { return numFed; } 11 Kitten toFeed; 12 for (Kitten cat : kittens) { 13 if (toFeed == null && !fedKittens.contains(cat)) { toFeed = cat; } 14 else if (toFeed == null) { continue; } 15 16 if (toFeed.getCuteness() < cat.getCuteness() && !fedKittens.contains(cat)) 17 { 18 toFeed = cat; 19 } 20 } 21 if (fTotal < toFeed.getDiet()) { return numFed; } 22 else { 23 fTotal -= toFeed.getDiet(); 24 fedKittens.add(toFeed); 25 } 26 } 27 return fTotal; 28 } GO ON TO THE NEXT PAGE. 7 CS42, Review Submission 4 (SOLUTIONS) Michael Yao, Student UID: 2077088 (c) Write the notFed method, which takes in the amount of food (in milliliters) that all of the kittens will be fed in total, and returns a List<Kitten> of the kittens that were not fed fully. If a kitten will only be partially fed instead of fully fed, the kitten should not receive any food and should be counted as unfed. The kittens do not have to be in any particular order in the returned list. For example, consider the case of when there are n = 3 kittens that need to be fed. Kitten 0 needs 100 mL of food and has cuteness level 7, kitten 1 needs 200 mL of food and has cuteness level 3, and kitten 2 needs 150 mL of food and has cuteness level 8. If there is 400 mL of dinner food total available, then notFed should return a List<Kitten> with only one element: the Kitten object representing kitten 1. This is because only kittens 2 and 0 can be fed, with 150 mL of food left over. 1 /** Returns a List of Kitten’s that were not fed fully. 2 * Precondition: fTotal > 0 3 **/ 4 public List<Kitten> notFed(int fTotal) { 5 List<Kitten> fedKittens = new ArrayList<>(); 6 7 while (true) { 8 if (fedKittens.size() == kittens.size()) { return numFed; } 9 Kitten toFeed; 10 for (Kitten cat : kittens) { 11 if (toFeed == null && !fedKittens.contains(cat)) { toFeed = cat; } 12 else if (toFeed == null) { continue; } 13 14 if (toFeed.getCuteness() < cat.getCuteness() && !fedKittens.contains(cat)) 15 { 16 toFeed = cat; 17 } 18 } 19 if (fTotal < toFeed.getDiet()) { return numFed; } 20 else { 21 fTotal -= toFeed.getDiet(); 22 numFed++; 23 fedKittens.add(toFeed); 24 } 25 } 26 27 List<Kitten> notFeed = new ArrayList<>(); 28 29 for (Kitten cat : kittens) { 30 if (!fedKittens.contains(cat)) { notFeed.add(cat); } 31 } 32 return notFeed; 33 } STOP END OF EXAM 8
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-