CSC 145 Sample Test 2

1.a Write a Java program that uses an ArrayList to hold a collection of randomly generated Doubles. The size of the list should be determined by the number of random values generated before a value > 0.999 is obtained using Math.random(). Once collected the program should display the size of the list.

1.b In your own words, describe the difference between double and Double in the Java programming language.

2. How do you convert all uppercase characters in a string to lower case?

str = str.toLowerCase();

3. Write a method that accepts a string and returns the string with blanks and punctuation removed.

4. In a standard deck of playing cards there are 4 suits (Hearts, Spades, Diamonds, Clubs) and 13 ranks in each suit (Ace, 2, 3, . . . , 10, Jack, Queen, King). Write methods that return the suit and the rank of a card encoded in the integer index 0 through 51 using the following encoding scheme:

suitnum = index / 13

ranknum = index % 13

The suit method should return the string that is the name of the suit, while the rank method should return the string that is the name of the rank.

5. Write a method that accepts a string and returns the string with the characters in revers order.

6. Write a method that converts a base 10 integer into a string representing an octal (base 8) value.

7. Give the output for the following statements:

____ System.out.println("hello".compareTo("Hello"));
____ System.out.println("a".compareTo("aa"));
____ System.out.println("zebra".compareTo("ZEBRA".toLowerCase()));
____ System.out.println("a".compareTo("Z"));
____ System.out.println("Z".compareTo("a"));

8. In your own words explain the difference between instance and static variables and methods.

9. Write a class for creating instances of regular polygons. Include properties for number of sides, length of side, perimeter, and area. Some properties should be defined by user, other should be derived. The user should be permitted to set/change some properties, but not others. Provide the proper level of access for each property in your class definition.

10. Write a test program to create a listarray of 100 regular polygons with random side lengths between 1 and 10 units, and random number of sides between 3 and 12 inclusive.

11. Write a recursive method to compute foob(n) where foob(n) = 1 if n <=1, and foob(n) = foob(n-1) + foob(n-1) for n>1.

12. Write a recursive method to compute feeb(n) where feeb(n) = 1 if n<=1, and feeb(n) = 2*feeb(n/2) for n>1.