COSC 135B After Exam 2 Practice Problems
Tom Linton, Fall 2000, Central College
  1. Consider a new class named Money. This class will represent monetary objects using two fields, dollars and cents. Since monetary exchanges need to be exact, these will be int variables. In addition to these two fields, our class should have each of the following methods:
  2. Write the Money class and each of its methods.
  3. Write a method that takes an array of ints as input and returns an integer that equals the number of entries in the array that are odd and positive. For example, if the array is {3, -3, 4, -12, 18, 1, 7, 789, -453}, your method should return 4, since 3, 1, 7 and 789 are all odd and positive.
  4. Write a method that takes a positive integer as input, say n, and returns an array that contains the first n powers of 2. For example, on input 5, your method should return the array {2, 4, 8, 16, 32}.
  5. One method to encrypt (make into a secret code) a message is to write the letters of the message that occur in odd indexes on one line, and the even indexed letters on the next line, and then concatenate the two lines. For example, if the message is "here is my secret message", we get:
        odd  characters: hr sm ertmsae
      even characters: eei ysce rma
      encrypted message: hr sm ertmsaeeei ysce rma
    Write a method to code an input message, and another method to decode an encrypted message.
  6. How do you write each of the following in JAVA?
    1. Swap the elements (doubles) in positions 0 and 5 of the array named a[].
    2. Test if the last element (int) of an array named a[] is even, or the first element is odd.
    3. Calculate the number of characters (no restrictions on what denotes a character) in the String called str1.
    4. Test whether the int variable index1 is a legal index for the array a[] (that is, whether or not the statement a[index1] will cause an indexOutOfBounds error).
    5. Test whether the String "easy" appears in the String variable named myString.
    6. Test whether string1 and string2 are equal (i.e. contain the same "word").
    7. Extract the sub string of the String variable named sentence, which starts at index 3 and ends at index 7. You want to include the character at index 7.
    8. Check if the Checkbox named dealCards is selected?
  7. If the array a[] has the values (the bottom row just lists the indices for your convenience)
    25 46 37 9 46 29 -6 16
    0 1 2 3 4 5 6 7
    What are the contents of the array after the following code is executed?
    int left=0, right=7, p=0, temp;
    while (left <= right) {
       while( a[left] <= a[p] )
          left++;
       while ( a[right] >= a[p] )
          right--;
       if (left <= right) {
          temp = a[left];
          a[left] = a[right];
          a[right] = temp;
          left++;
          right--;
       }
    }
  1. Assume that a[][] is a (rectangular) 2-dimensional array of ints.
    1. How do you figure out the number of rows in a[][]?
    2. What JAVA statement will tell you the number of columns of a[][]?
    3. Assuming that a[][] has 7 rows and 5 columns, write JAVA code to print out rows 0, 2, 4 and 6 of a[][] in the TextArea named output.