Review for Final Exam

1. Answer all the questions on the review sheets for the midterms, and answer all questions on the sample midterms.

2. We have discussed in detail three methods which all classes inherit from the Object class. Give the method header for each, and describe how each works.
See class notes

3. Write a Circle class in which  you override the three methods discussed in #2. What does it mean to override a method?
Done in class

4. If class A is a subclass of class B, write the header for class A.
Done in class

5. Describe how the instanceof operator is used, and then give an example use of it.
See class notes

6. Look at your Circle class, and for each instance variable and local variable, write down the scope of each.
Done in class

7. When we declare an array of objects, e.g.
Circle[] circleArray;
how much memory have we set aside? Would your answer be different if we declared an array of ints, e.g.
int[] intArray;

We've set aside 4 bytes to store the address of an array.

8. If I now allocate memory for my circleArray, e.g.
circleArray = new Circle[50];
how much memory is set aside when this statement is executed? What value is stored now in Circle[0]?

50*4 = 200 bytes

9. Write a Card class that represents a card from a standard deck of cards. You should store each Card object's value (2-10, Ace, Jack, Queen, King) and suit (spades, clubs, hearts, diamonds). Declare class constants of type int to represent each suit, and declare integer class constants for the card values Ace, Jack, Queen and King. Include the following methods:
public Card(int suit, int value): initialize the instance variables to the specified values
getters and setters for the instance variables, with appropriate restrictions on how a Card object can be changed
over-ride toString(), equals(), and clone(). The string returned by toString() should look like these:
"Ace of Clubs"
"5 of Hearts"
etc.
public int compareValue(Card c): returns -1 if this Card's value is less than c's, 0 if the Cards have the same value, and 1 if this Card's value is greater than c's. For this method, consider Aces to have the lowest value in the deck.

See your last project

10. Write a single Java statement that creates an array of 3 Strings containing the strings "hello", "world", "hello".
String[] arr = {"hello", "world", "hello"};

11. Assume that I've created and initialized two arrays of doubles, array1 and array2. array1 has 50 entries, and array2 has 30 entries. Copy the last 10 entries of array1 to the first 10 entries of array2.
     a. Use the arraycopy() method.
     b. Use a loop.

12. I've declared the array myArray as follows:
char[] myArray = new char[25];
Assume I've initialized the entries in myArray. Write Java code that creates a new reference variable that points to this same array.

char[] arr2 = myArray;

13. Now write Java code that creates a distinct array that has the same entries as myArray (see #12).

char[] arr = new char[25];
for(int i = 0; i < arr.length; i++) {
    arr[i] = myArray[i];
}

14. Write a method that takes two arrays of doubles and returns the average of the values in both arrays.

public static double avg(double[] one, double[] two) {
    double sum = 0;
    for(int i = 0; i < one.length; i++)
        sum += one[i];
    for(int i = 0; i < two.length; i++)
        sum += two[i];

    int length = one.length + two.length;
    if(length > 0)
        return sum/length;
    else return 0;
}


15. Write a program that includes a method that takes two arrays of integers and returns the array that contains the largest entry. The program should contain a main method that calls this method.

I leave it to you to write the main method.

public static int[] maxEntry(int[] one, int[] two) {

    if(one == null || two == null) return null;
    else if(one.length == 0) return two;
    else if(two.length == 0) return one;

    int max1 = one[0];
    int max2 = two[0];

    for(int i = 1; i < one.length; i++){
        if(max1 < one[i]) max1 = one[i];
    }

    for(int i = 1; i < two.length; i++) {
        if(max2 < two[i]) max2 = two[i];
    }

    if(max1 > max2) return one;
    return two;
}

16. Write a method union that takes two arrays a and b of integers, and returns an array that contains the integers that are in one or both arrays (a and b). The returned array should not contain duplicates.

17. Write a program that prompts the user for positive integers, and stops prompting when 0 or a negative value is entered. Print the average, min and max for the positive integers.

18. Write a program that uses our DrawingPanel class to draw a smiley face.

19. Write a program that takes some number of integers as command-line arguments, and returns the sum of the integers.

20. Review your programming assignments - you may see questions about them on the final exam.

21. When can you use a for each loop in place of a for loop? Given this array:
int[] arr = new int[30];
// assume arr's entries have been initialized
a) Can you use a for each loop to print arr's entries?
b) Can you use a for each loop to set each of arr's entries to 1?

We did not cover for each loops.

22. Write a Square class that implements the following interface. You will need to include an instance variable to store the square's side length. Include constructors, getters and setters in your class, and over-ride the toString() method. Also write a Circle class that implements the Shapes interface.

You will not be asked about interfaces.

public interface Shapes {

    double perimeter(); // returns the perimeter of the shape

    double area(); // returns the area of the shape
}

23. Name two differences in abstract classes and interfaces.

24. Define polymorphism. Write a short program that creates a Shapes array of size 5, and initializes some of the entries to be Squares and some to be Circles. Call perimeter(), area() and toString() on the array entries. Then try calling getSide() on one of the array entries that refers to a Square object. What happens? Explain how polymorphism is at work in your program.

See in-class examples on polymorphism.

25. Write down the prototype of each method we have studied from the String class, and describe briefly how each is called and what it does. Give example calls for each.

26. Repeat #25 for the Character class.

27. Repeat #25 for the Random class.

28. Repeat #25 for the Scanner class.

29. Write a method that takes a 2D array of Strings, and returns the length of the shortest string in the array.

public static int shortest(String[][] arr) {
    int min = arr[0][0];

    for(int i = 0; i < arr.length; i++) {
        for(int j = 0; j < arr[i].length; j++) {
            if(min > arr[i][j].length()) min = arr[i][j].length();
        }
    }

    return min;
}



30. Consider the following method:
public static int mystery(int[] list) {
    int x = 0;
    for(int i = 1; i < list.length; i++) {
       int y = list[i] - list[0];
       if(y > x) {
          x = y;
       }
    }
    return x;
}

For each of the following arrays, indicate what this method would return if it was called on the given array.

{5}
{3, 12}
{4, 2, 10, 8}
{8, 2, 10, 4, 10, 9}

31. Write a static method isPalindrome() that takes a String as a parameter and returns true if the String is a palindrome and false otherwise. A String is a palindrome if its reverse is the same string as itself. Disregard differences in case, e.g., "Mom" and "RAdar" are palindromes.

public static boolean isPalindrome(String str) {
    if(s == null || s.length() == 0) return true;

    String s = str.toLowerCase();
    for(int i = 0; i < s.length()/2; i++) {
        if(s.charAt(i) != s.charAt(s.length()-1-i) return false;
    {
    return true;
}


32. Write a static method countLastDigits() that takes an array of integers as a parameter and determines how many of the integers end in 0, how many end in 1, how many end in 2, etc. Your method will return an array of counts - the number of integers that end in 0 should be the element at index 0 in the returned array, the number of integers that end in 1 should be the element at index 1 in the array, and so forth. If the method is called on the array {9, 29, 44, 103, 2, 52, 12, 12, 86, 35, 20}, the returned array should be {1, 0, 4, 1, 1, 1, 1, 0, 0, 2} since 1 integer ends in 0 (20), no integers end in 1, 4 integers end in 2 (2, 52, 12, 12), etc.