CMSI 185
Quiz 2

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
 
15
2
 
15
3
 
10
4
 
10
5
 
15
6
 
15
7
 
20
TOTAL
 
100
  1. Match the terms with their definitions, by writing the letter of the definition next to the term.
    1. Class _______
    2. Object _______
    3. Method _______
    4. Type _______
    5. Reference _______
    6. Variable _______
    7. Constructor _______
    8. null _______
    9. Field _______
    10. Getter _______
    11. Immutable _______
    12. Static _______
    13. Instance _______
    14. toString _______
    15. new _______
    1. An expression meaning "no object, nope, none, nothin' here"
    2. Something that is constructed, and belongs to a class
    3. A modifier for fields and methods that makes them belong to a whole class, rather than to an object of their class
    4. The value of a variable of a reference type
    5. A method that returns the value of a property of an object
    6. An operator used in the creation of an object
    7. A term describing an object that cannot be changed
    8. A method used whenever an object is used when a string is expected
    9. That which indicates the values that a variable may have
    10. A member of a class that holds a value
    11. A specification for a bunch of similar objects, or a collection of utility methods
    12. A term describing the relationship of an object to its class
    13. A container for a value
    14. A member of a class that is used to create objects
    15. Code representing the behavior of an object
  2. Here is a lame 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 void setX() {this.x = x;}
        public double getY() {return y;}
        public void setY() {this.y = y;}
    }
    
    Suppose your friend made a point....
        Point home = new Point(10.62, 345.1);
    
    Later your friend needed to move the point, but wanted to "save" the old position first. So she does this:
        Point oldHome = home;     // save ("back up") the current position
        home.setX(33.4);
        home.setY(-810.6);        // now home is (33.4,-810.6)
    
    How can your friend move her home back to its old position? Should she say home = oldHome;? Or home.setX(oldHome.getX()); home.setY(oldHome.getY());? Back up your answer with object pictures like we saw in class.
  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 TermPaper, a method to determine how many words are in the paper.


    2. In a class of math utilities, a method to tell how many ways you can pick k items out of a collection of n items.


    3. 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.)


    4. 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).


    5. In a class representing a user interface for a tool like Google Maps, a method that can turn "Street View" on or off.


  4. What does the following code fragment print, and why?
        String s = "    a  b  c    ";
        s.trim();
        System.out.println(s);
    
  5. Everyone knows that in Java, strings are immutable. So why is this legal?
    String s = "hello";
    s = "world";
    
    (For full credit your answer has to be given in complete English sentences and you must use any technical jargon correctly.)
  6. Your little brother has been trying to write code again, but he doesn't spend time to understand everything he is writing; instead he just copies code off the web and tries to tweak it. He ends up making a Player class for a multi-player game like this:
    class Player {
        public static String name;
        private boolean isComputerPlayer;
        public static double score;
        double latitude;
        double longitude;
    }
    
    Make a list of problems with this class. Your list must include a discussion of what happens when trying to give each player his or her own name.
  7. Write a class called Range, which represents a range (contiguous sequence) of integers. Range objects are constructed by specifying the first and last integers in the range. Supply methods that get the first value, get the last value, and get the sum of all integers in the range, Also supply a toString method which returns a string containing all of the values (in order), separated by commas. (Extra credit: add a method to get the median.)