Program Design II - Exam #2

Unless specified otherwise, write your answers on the provided answer sheets. You may refer to printouts of any source code you have written.

  1. (2 pts) Is it true that every recursive function can be written iteratively?

  2. (4 pts) Name some useful strategies when writing successful recursive functions.

  3. (6 pts) Write a recursive function called rsum that given an array of double values and the size of the array as parameters will return the sum of all the numbers.

  4. (6 pts) Consider the following famous recursive function called Ackermann's function. What value is returned by $ack(1,2)$?
    public static int ack(int m, int n) {
       if (m==0) return n+1;
       if (n==0) return ack(m-1,1);
       return ack(m-1, ack(m,n-1));
    }
    

  5. Suppose you have been asked by a cable company to create a program to track customer status for various regions. In doing so you have created the following class to represent a customer:
    public class Customer
    {
       protected String email; // customer email address
       protected int cablePackage;  // 1=basic; 2=supreme; 3=everything
       protected boolean wantsPromotions; // false= don't send promotional emails
    
       public Customer(String email)
       {
          this.email= email;
          cablePackage= 1;
          wantsPromotions= true;
       }
    
       public String toString()
       {
          if (wantsPromotions)
             return email+"/"+cablePackage+"/+";
          else
             return email+"/"+cablePackage+"/-";
       }
    
       public double calcBill()
       {
          switch(cablePackage) {
             case 1: return 19.99; break;
             case 2: return 39.99; break;
             case 3: return 64.99; break;
          }
          return 19.99;
       }
    
       public void setPackage(int val)
       {
          if (val>0 && val<4)
             cablePackage= val;
       }
    }
    

    1. (4 pts) Some customers pay for Internet service in addition to their TV services. Create a new class called ICustomer that inherits from Customer and adds an integer attribute called speed that indicates the Mbps (10, 20, or 50) the customer has requested for their Internet service.

    2. (4 pts) In the new ICustomer class create a constructor that accepts the email address of the customer and the speed of Internet service. It should invoke the constructor of it's super class.

    3. (6 pts) Draw a UML diagram depicting these two classes. When depicting attributes you should specify their type. When depicting methods you should specify their return type, name, and parameters.

    4. (6 pts) In the ICustomer class override the calcBill method so that the monthly bill includes the amount for TV services and adds the following amount for Internet service: 10Mbps=$24.99, 20Mbps=$34.99, 50Mbps= $44.99. Then apply a 10% discount as a reward to customers for purchasing both services.

    5. Write a section of code that makes use of the Customer and ICustomer classes as follows:
      1. (4 pts) Establish an array (called cust) of 10 references to customers.
      2. (4 pts) The first element of the array should refer to a cable/internet customer who has the ``everything'' cable package and 50Mbps internet service.
      3. (2 pts) The second element of the array should refer to a cable-only customer with the basic package.
      4. (2 pts) Display the monthly bill of each customer.

    6. (2 pts) In our current design would it make sense to declare the Customer class as abstract or not? Briefly explain.

  6. Imagine the game of Othello being played on a board with 10 row and 15 columns. In this game there are two players and lots of two-sided pieces with red on one side and blue on the other. One player wants to get as many blue pieces on the board as possible while the other play wants to get as many red pieces on the board as possible. For the purposes of these questions all you need to know about the game is that the player who ends up with the most pieces of their color is the winner. Suppose we want to represent an Othello board as a 2d array of chars. If an array element has a space character then the corresponding position on the board is blank. If an array element contains `R' then the board position contains a red piece (and, of course, `B' for blue).

    1. (2 pts) Create a class called OthelloBoard that contains an attribute called board that provides a 2-dimensional array representation of the pieces as described above.
    2. (4 pts) In the constructor reserve memory for the board attribute.
    3. (4 pts) Also, in the constructor write code that will initialize the entire board to be empty.
    4. (8 pts) Write a function called winner that will return `B' if the board contains more blue pieces than red. It should return `R' if there are more red pieces than blue. It should return `X' if there are the same number of red and blue pieces.

  7. (4 pts) Show something you know from this course that did not get asked on this test.

Quick Links