CSE142 Sample Final handout #32 1. Simulation, 10 points. You are to simulate the execution of a method that manipulates an array of integers. Consider the following method: public static void mystery(int[] list) { for (int i = 0; i < list.length; i++) { list[i] += 2; list[i] *= i; } } In the left-hand column below are specific lists of integers. You are to indicate in the right-hand column what values would be stored in the list after method mystery executes if the integer list in the left-hand column is passed as a parameter to mystery. Original List Final List ---------------------------------------------------------- (3) __________________________________ (7, 8) __________________________________ (5, 4, 3) __________________________________ (10, 11, 12, 13) __________________________________ (2, 4, 6, 8, 10) __________________________________ 2. Polymorphism, 6 points. Assume that the following classes have been defined: public class George extends Sally { public void method2() { System.out.println("george 2"); } } public class Fred { public void method1() { System.out.println("fred 1"); } public void method2() { System.out.println("fred 2"); } public String toString() { return "fred"; } } public class Harold extends Sally { public String toString() { return "harold"; } } public class Sally extends Fred { public void method1() { System.out.println("sally 1"); } public String toString() { return "sally"; } } Consider the following code fragment: Fred[] elements = {new Sally(), new Fred(), new George(), new Harold()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); } What output is produced by this code? 3. File Processing, 9 points. Write a static method processData that takes a Scanner holding a series of integers as a parameter and that reports various statistics about the integers. It should report the total number of numbers, the sum of the numbers, the total number of evens and the percent of evens. For example, if the Scanner contains the following integers: 5 7 2 8 9 10 12 98 7 14 20 22 Your method should produce the following output. Total numbers = 12 Sum of numbers = 214 Total evens = 8 Percent evens = 66.66666666666667 You are to exactly reproduce the format of this output. Write your solution to processData below. 4. File Processing, 10 points. Write a static method processFile that takes a Scanner containing an input file as a parameter and that reports various statistics about the file. In particular, your method should report the number of lines in the file, the total number of characters (not counting any newline characters) in the file and the length and text of the longest line. You may assume that the input file is not empty (i.e., that it has at least one line of input). For example, if the Scanner is processing this input file: Twas brillig and the slithy toves did gyre and gimble in the wabe. All mimsey were the borogroves, and the mome raths outgrabe. "Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch." The program would find that there are 9 lines (blank lines count), that there are a total of 254 characters in the file (33 on the first line, 32 on the next line, 31 on the next line, 28 on the next, 0 on the next, etc) and that the longest line is the one that begins with "the jaws that bite". For this file, the method should produce the following output: Total lines = 9 Total chars = 254 Length of longest line = 41 Longest line = the jaws that bite, the claws that catch, You are to exactly reproduce the format of this output. Write your solution to processFile below. 5. Arrays, 10 points. Write a static method numDuplicates that returns the number of duplicates in a sorted array of integers. For example, suppose a variable called "list" is a sorted array of integers that stores the following sequence of values. (1, 1, 1, 3, 3, 6, 9, 15, 15, 23, 23, 23, 40, 40) then the call: numDuplicates(list) should return 7 (two duplicates of 1, 1 duplicate of 3, one duplicate of 15, two duplicates of 23 and one duplicate of 40). Remember that the list of integers will be in sorted order, so all of the duplicates will be grouped together. Write your solution to numDuplicates below. 6. ArrayList, 10 points. Write a static method replaceStrings that takes as parameters an ArrayList of Strings, a target String and a replacement String and that replaces all occurrences of the target String with the replacement. For example, suppose that an ArrayList called "list" contains the following values: (this, is, lots, of, fun, for, this, problem, and, this, class) And you make the following call: replaceStrings(list, "this", "that"); Then list should store the following values after the call: (that, is, lots, of, fun, for, that, problem, and, that, class) You may assume that the ArrayList contains only String values, but it might be empty. Write your solution to replaceStrings below. 7. Critters, 15 points. Write a class Robot that implements the Critter interface. The instances of the Robot class should alternate between moving east and moving west. Each robot should first move east once, then move west twice, then move east 3 times, then move west 4 times, then move east 5 times, then move west 6 times and so on. Your robots should return different display characters depending upon whether they are moving east or west. If on the next move your robot is going to move east, it should return the greater-than character (">"). If on the next move it is going to move west, it should return the less-than character ("<"). Remember that the Critter interface is defined as follows: public interface Critter { public char getChar(); public int getMove(); } Write your solution to the Robot class below. 8. Classes, 20 points. Write a class BonusPlan that keeps track of a bonus program for a video rental company in which a customer earns various credits good towards free rentals. The customer can decide when to use these credits and when to pay for a rental. The rules of the program are as follows. 1) At the beginning of every month the customer gets one rental credit, but this credit must be used during the month or it expires and the customer loses it. 2) Whenever the customer pays for 3 rentals in a single month he earns an additional credit that has no expiration. This rule applies only to rentals within a single month. For example, if a customer rents 2 movies every month he never earns a free rental under this rule. A customer can earn multiple free rentals as more movies are rented in a single month (1 free rental for 3, 2 free rentals for 6, 3 free rentals for 9 and so on). 3) For each rental the customer decides whether to pay or whether to use a credit to get the rental for free. If the customer uses a credit, the once-a-month credit described in rule 1, if not already used, is applied before any non-expiring credits earned in rule 2. Your class should be called BonusPlan and should have the following public methods: * a constructor BonusPlan() that constructs a BonusPlan object with its first month's credit * method rent that takes a boolean value indicating whether or not the rental is free (true for a free rental, false for a paid rental) * method getCredit() that returns the total number of current credits (includes credits earned under both rules 1 and 2 above) * method endMonth() that ends one month and begins another, resetting the monthly credits appropriately A typical manipulation would look like: BonusPlan plan = new BonusPlan(); plan.rent(false); // paid rental plan.rent(false); // paid rental plan.rent(true); // free rental, uses up this month's credit plan.rent(false); // paid rental, earns a free rental plan.rent(false); // paid rental plan.endMonth(); // end of month; user gets a new monthly credit plan.rent(true); // free rental, uses monthly credit System.out.println(plan.getCredit()); In this case, the final println would print the value 1 because the customer would still have one rental earned under rule 2. It is possible that the rent method will be called with the value true to request a free rental at a time when the customer has no credits. In this case, the method should print the message "no credits" to System.out. Provide your definition for class BonusPlan below. 9. Arrays, 10 points. Write a method evenOdd that rearranges an integer list so that all the evens appear before all of the odds. For example, if the list stores this sequence of integers when the method is called: (1, 3, 2, 9, 6, 7, 12, 4, 28, 17) It should store something like this when the method is done executing: (2, 6, 12, 4, 28, 1, 3, 9, 7, 17) It matters only that all of the evens come before all of the odds. For example, the following would also be a reasonable ending state for the list, even though the evens and odds appear in a different order. (28, 4, 2, 12, 6, 7, 9, 3, 1, 17) This problem is easier to solve if you can use a temporary array. That way you can create the new list in the temporary and then copy it back to the original. You will not receive full credit if you solve the problem this way. To get full credit, you must solve the problem without the use of a temporary array. But you can receive up to 5 of the 10 points for solving it with a temporary. Assume that you are writing a method for the IntList class from handout #31. Recall that it has the following private data fields: public class IntList { private int[] myList; // array of integers private int myLength; // current length of list // method definitions } Method evenOdd has no parameters. Write its definition below.
Stuart Reges
Last modified: Tue Dec 7 11:37:29 PST 2004