CMSI 185
Final Exam

The test is open-everything with the sole limitation that you neither solicit nor give help while the exam is in progress.

Since this is a timed, in-class, exam, you do not have to write comments in your code. Other aspects of good style, such as nice formatting and naming are still required, though.

ProblemYou gotOut of
1
 
10
2
 
10
3
 
10
4
 
10
5
 
10
6
 
10
7
 
10
8
 
10
9
 
10
10
 
10
TOTAL
 
100
  1. Match the terms with their definitions, by writing the letter of the definition next to the term.
    1. Array _______
    2. Object _______
    3. Factory _______
    4. ArrayList _______
    5. equals _______
    6. Arrays _______
    7. Math _______
    8. length _______
    9. Primitive _______
    10. Integer _______
    1. Something that is constructed, and belongs to a class
    2. A method often required in place of ==.
    3. A class for objects that contain an int
    4. A class containing static methods on numbers
    5. An object containing a length and an indexed sequence of values
    6. A method that returns an object (possibly, but not necessarily a new one)
    7. A class whose instances are resizable sequences of values
    8. A type that is not a reference
    9. A class containing a bunch of static methods operating on arrays
    10. A read-only property of arrays
  2. Here is an immutable point class:
    public class Point {
        private double x; private double y;
        public Point(double x, double y) {this.x = x; this.y = y;}
        public double getX() {return x;}
        public double getY() {return y;}
    }
    
    Add two methods to this class. The first should be a non-static method called copy, which returns a copy of the point on which it was called. The second should be a static method called copyOf, which returns a copy of its argument. (Remember, by "copy" we mean a new object with the same field values as the object being copied.)
  3. Write method signatures for each of the following. Please note that this problem is asking for only the signatures. If you try and write the method bodies you'll get a zero on this problem. For full credit, you must get exactly the right return type, exactly the right number of and types of parameters, and you must choose good names for the methods and their parameters. You must also determine whether each method should be public and/or static.
    1. In a class called Account, a method to deposit a certain amount of money.


    2. In a class representing a movie, a method to delete all frames between two points in time (for example, between 0:36 and 1:25).


    3. In a class of string utilities, a method to squeeze out any excess space in the middle of a string.


    4. In a class for a deck of cards, a method to return the position of a given card. (You should know that "given" means an argument. And yes, assume someone has already written a Card class.)


    5. In a class representing a playing card, a method to return the card with the given suit and rank.


  4. Suppose we have the following Circle class:
        public class Circle {
            private double radius;
            public Circle(double radius) {this.radius = radius;}
            public double getRadius() {return radius;}
            public void setRadius(double r) {radius = r;}
        }
    
    You ask your little brother to write a method to stretch a circle by a given factor, so he writes the following:
        public static void stretch(Circle c, double factor) {
            c = new Circle(factor * c.getRadius());
        }
    
    and puts it in a class called GeometryUtilities. You create your own circle of radius 10, and call his method like this:
        Circle c = new Circle(10.0);
        GeometryUtilities.stretch(c, 4);
    
    After calling his method, what is the radius of your circle? Justify your answer with pictures.
  5. Why is the following method considered horrible and how would you rewrite it to make it better? You must identify at least one major design flaw, and point out at least one way in which the code doesn't even work.
        public static int[][] createMultiplicationTable() {
            int[][] table = new int[12][12];
            for (int i = 1; i <= 12; i++) {
                for (int j = 1; j <= 12; j++) {
                    table[i][j] = i * j;
                }
            }
            return table;
        }
    
  6. Write a class called Employee, with fields called name, title, and supervisor. The name and title should be Strings, but the supervisor should be an Employee. Include a constructor that takes in initial values for all three fields. Supply getters for all three fields, but setters only for title and supervisor. Then draw a picture of these objects:
        Employee e1 = new Employee("Alice", "CEO", null);
        Employee e2 = new Employee("Bob", "CTO", e1);
        Employee e3 = new Employee("Carol", "CFO", e1);
        Employee e4 = new Employee("David", "Marketing Director", e3);
        Employee e5 = new Employee("Eloise", "Sr. Software Engineer", e2);
        Employee e6 = new Employee("Fred", "Programming Intern", e5);
        Employee e7 = new Employee("Gretchen", "Sr. Accountant", e3);
    
  7. Explain, in complete sentences, why we chose, in Homework 6, to hide the Card constructor and use a static factory method to supply cards to clients of this class. Would using an enum have been better? Why or why not?
  8. The FiveDice class we saw in an earlier homework assignment used five fields to store the values of the dice. Give an alternate version of the class using an array to store the values, but include only the field, a constructor, a roll method, and the isYahtzee method.
    public class FiveDice {
    
  9. Fill in the body of the following method, which takes in a list of boolean objects and returns whether the number of occurrences of the value true is even.
    public static boolean hasEvenParity(List a) {
    
  10. Write a method that takes in an array of strings and "removes the English vowels" from each of the strings. For example, if the method was passed in the array ["Rats", "Are", "Very", "Intelligent"], then the method would modify the array so that it would have the value ["Rts", "r", "Vry", "ntllgnt"].