CS211 Homework 1

 

Due Tuesday, Sep. 12, at the beginning of class

You may work singly, or in pairs.  No larger groups will be permitted.

Your name and NetID (as well as your partner’s, if a pair) must be on all items submitted.

 

Exercise 1

 

Write a class called TwoBuffer that implements the following abstraction.  It stores two items (of type Object) and provides two methods, put() and get().  put() should insert some item into the buffer unless the buffer already contains two items, in which case put() does nothing.  put() should also return a boolean that indicates whether or not the buffer was modified.  get() should take no arguments, but return the oldest item in the buffer and remove it from the buffer. You may want to write a small client program that makes use of your TwoBuffer to test its correctness.

 

 

Exercise 2

 

a)  Suppose you have a combination lock with a three letter combination.  In a combination, all the letters need not be unique.  Write a program that computes all the combinations of such a lock and prints the first combination and every 1000th combination thereafter (i.e. 1, 1001, 2001, ...).  How many different combinations exist for an N-letter combination lock?

 

b)  Write a program the computes all the permutations for a 3-letter combination lock, printing the first permutation and every 1000th permutation thereafter (i.e. 1, 1001, 2001, ...).  In a permutation, all the digits are unique.  How many permutations exist for an N-letter combination lock?

 

 

Exercise 3

 

The program below has several errors.  Identify, explain, and correct the errors.  No code lines should be removed, and no new code lines should be added.  The program when compiled and run successfully should print the following result on the terminal:

45cm = 1.5ft

 

1.  import java.io.*

2. 

3.  class MetricToEnglish {

4.   

5.    private double CentimetersToInches(double cent)

6.    {

7.          return (cent / 2.5);

8.    }

9.  }

10.

11. CLASS LengthConverter implements MetricToEnglish {

12.

13.   PUBLIC void Main(string args)

14.   {

15.         LengthConverter   lc;

16.         double            centimeters = 45.0;

17.         int         inches, feet;

18.        

19.         */ Convert /*

20.         inches      = lc.CentimetersToInches(centimeters);

21.         feet  = lc.InchesToFeet(inches);

22.        

23.         System.out.println(centimeters + 'cm = ' + feet + 'ft')

24.   }

25.  

26.   private double InchesToFeet(double inch)

27.   {

28.        

29.         RETURN (inch / 12);

30.   }

31. }