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.
- Match the terms with their definitions, by writing
the letter of the definition next to the term.
- Array _______
- Object _______
- Factory _______
- ArrayList _______
- equals _______
- Arrays _______
- Math _______
- length _______
- Primitive _______
- Integer _______
|
- Something that is constructed, and belongs to a class
- A method often required in place of ==.
- A class for objects that contain an int
- A class containing static methods on numbers
- An object containing a length and an indexed sequence of values
- A method that returns an object (possibly, but not necessarily a new one)
- A class whose instances are resizable sequences of values
- A type that is not a reference
- A class containing a bunch of static methods operating on arrays
- A read-only property of arrays
|
- 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.)
- 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.
- In a class called Account, a method to deposit
a certain amount of money.
- 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).
- In a class of string utilities, a method to squeeze
out any excess space in the middle of a string.
- 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.)
- In a class representing a playing card, a method to return
the card with the given suit and rank.
- 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.
- 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;
}
- 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);
- 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?
- 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 {
- 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) {
- 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"].