Ryerson University: Dept. of Computer Science

CPS209, Midterm Test 2C solutions, Winter 2006


Last Name __________________ First _______________ Number_________________
  1. (8 marks) Write an application LetterCounter reads a text file input.txt and counts how many of each lower case letter are in it from 'a' to 'z'. Print out the result, something like:
    other : 100
    a : 15
    b : 17
    ...
    z : 2
    
    Your program must compile, but you do not need to catch exceptions. Hint: 'b' - 'a' is 1 and 'c' - 'a' is 2.
  2. (1 mark) What is the difference between flow layout and border layout?
    BorderLayout has five regions NORTH, SOUTH, EAST, WEST, CENTER, whereas flow layout just places components in order left to right, top to bottom.
  3. (1 mark) Which of the following assignments compiles without error? (Assume Shoe is a superclass of Sandal).
    Answer: 1 3 4
        Shoe shoe = new Shoe() ;
        Sandal sandal = new Sandal() ;
        shoe = sandal ;          // 1
        sandal = shoe ;          // 2
        shoe = (Shoe)sandal ;    // 3
        sandal = (Sandal)shoe ;    // 4
    
  4. (10 marks) Write a subclass of BankAccount (listed below) called DonationAccount which has the following features:
    1. Every DonationAccount has a special donation, which is some real value and an integer counter.
    2. There is a default constructor which sets the balance, donation and counter to 0.
    3. There is a constructor with parameters for the initial balance and donation.
    4. For each withdrawal the counter is incremented.
    5. There is a method donate with no parameters, which is used at the end of the month. For each of the withdrawals during the month donation dollars are withdrawn from the account for the environment. If there is not enough money to cover the donations, throw an IllegalStateException, but do not catch it.
    public class BankAccount 
    {  
        private double balance ;
        public BankAccount() { balance = 0 ; }
        public BankAccount(double initialBalance) {	balance = initialBalance ; }
        public void deposit(double amount) { balance += amount ; }
        public void withdraw(double amount) { balance -= amount ; }
        public double getBalance() { return balance ; }
    }