COMP 401 Fall 2012 Midterm

Monday, Oct15, 2012, 11:00-12:15

Instructions

  1. Please spread out and try and sit in alternate seats.
  2. This is a closed book exam. You will not be penalized for errors in Java syntax.
  3. Write on the exam itself.
  4. Write on a blank page if there is not enough space to solve a problem.
  5. There are:
  • 12 numbered pages including this one, andany marked blank pages.
  • 5questions.
  • 75 possible points. Point values appear in brackets next to each question.
  1. You have 75 minutes.
  2. You are not required to comment any code you write, but may get partial credit if you write appropriate comments but incorrect code.
  3. If you need to make any assumptions to clarify a problem, write your assumptions down. Only reasonable assumptions get full credit.
  4. Please inform the proctor of anything in the exam that you think is a mistake.
  5. Your code will be evaluated not only for correctness, but also for time and space efficiency and style.
  6. You cannot use any Java capabilities not covered in class.
  7. To answer questions about some piece of code given here, you can mark the code directly.
  8. If you do not understand some English word, do not hesitate to ask the proctor. Naturally, you are expected to know the computer science terms defined in class.

Name (in Capitals)

______

Pledge: I have neither given nor received unauthorized aid on this exam.

(signed)______

For survey purposes, please indicate the time at which you turned in the exam.

______

Please do not write below

1. _____/202. _____/12 3._____/104._____/13. 5. _____/20.

Total: _____/ 75

  1. [20 pts.]CONCEPT ILLUSTRATION

Consider the following classes and interfaces for describing points and lines. All of the code related to this problem is located in the same package.

a)[2 pts] Identify a constructor in the code.

b)[2 pts] Identify an instance variable in the code.

c)[2 pts] Identify an editable property in the code.

d)[2 pts] Identify a read-only property in the code.

e)[2 pts] Identify a computed (non-stored) property in the code.

f)[2 pts] Identify an overridden constructor or method in the code.

g)[2 pts] Identify an overloaded constructor or method in the code.

h)[6pts] Explain, using the object“ newADiagonalLine(30)”,how an instance of ADiagonalLine is stored in memory. You can draw the memory representation below or on the page on the left. As we saw in class, the memory representation of an object shows the memory“slots” allocated to the object and the name, value and type of each slot.

  1. [10 pts.] UNDERSTANDING CODE

The point and line code is reproduced belowand is exactly the same as the code in 1.

Assume that the following code has been executed:

Point point = newADiagonalPoint();

Line line = newADiagonalLine(30);

line.setX(20);

Give the output of each of the print statementsbelow. If an exception is thrown, simply describe the exception – you do not have to print the exact message Java gives to identify the error. You can write at the left or at the last page, if there is not enough space here. You do not need to give the reason for the output; but if you give the right reason and wrong output, you will get partial credit.

a)[2 pts] System.out.println(point.getX());

b)[2 pts] System.out.println(point.getY());

c)[4pts] System.out.println(line.getX());

d)[2pts] System.out.println(line.getWidth());

  1. [12 pts.] COMPILE TIME TYPE CHECKING

The line/point code is reproduced once again,andis exactly the same as the code in 1

Assume the following variable declarations:

Point point;

Line line;

ADiagonalPointdiagonalPoint;

ADiagonalLinediagonalLine;

ArrayListarrayList;

List list;

For each of the following assignments below, indicate if it will give a compile time error and give a brief reason for your answer that shows you are not simply guessing it. As we discussed in class, List is an interface and ArrayList is a class implementing this interface. These are described in more depth in the last question.

a)[2 pts] line = point;

b)[2 pts] point = line;

c)[2 pts] point = diagonalPoint;

d)[2 pts] diagonalLine = (ADiagonalLine) diagonalPoint;

e)[2 pts] diagonalLine = (ADiagonalLine) arrayList;

f)[2 pts] line = (Line) list;

  1. [13pts] FINDING AND FIXING FLAWS

The line/point code is reproduced once again,and is exactly the same as the code in 1

The goal of this code is to (a) define an interface that describe a mathematical point, (b) define an interface that describes a mathematical line (segment), (c) create an implementation of an immutable diagonal point, that is, a point in which the x and y coordinates are the same and cannot be changed, (d) create an implementation of an immutablemoving diagonal line, that is a line in which (i) the upper left corner is a movable diagonal point, and (ii) the width and height of the line bounding box are immutable and equal to each other.

Given these goals, this code has several correctness/stylistic/efficiency flaws. Point out these flaws and defend your answer. In your answers regarding stylistic flaws, you can cite general principles, but you will get more points if you also explain what could go wrong in this specific example if these principles are violated. You may want to revisit this problem after you have completed the others. Explain also how this code can be fixed by either marking on the page or writing new code.The more details you provide, the more points you will get.

  1. [20pts] WRITING CODE

The line/point interfaces are reproduced once again. The classes are not reproduced as they are irrelevant to this problem.

In this problem, you will use the predefined interface, List, and its implementation ArrayList. The following methods of List are relevant here:

publicfinalint size(); // the size of the list

publicfinal Object get(int index); // the element at position index

publicfinalvoid add(Object obj) ; // append obj to the list

public void clear(); // empties the list, making the size 0

Write a class to implement the following interface, which is defined in terms of List:

An instance of this type provides a setter that takes as a parameter a List of arbitrary objects. Assume that this list consists only of Point and Line instances, and that the caller of this method will ensure that this constraint is met. You should not make any assumptions about the classes of these instances though you can assume that these classes do correctly implement mathematical points or (segments of) lines.You should assume the definitions of Point and List on this page.

Let us call the list passed to the setter the complete list. The getPoints() method should return a list of those objects in the complete list that represent mathematical points, and getLines() should return a list of those objects in the complete list that represent (segments of) mathematical lines. The order of elements in these lists does not matter.

The outline of the class you have to implement is given below; fill out the methods, adding new ones if necessary. As mentioned earlier, your code will be evaluated not only for correctness, but also for time and space efficiency and style. This question has a large number of points allocated to it as many things can go wrong when you write code. You will get partial credit; so do write down parts of the solution even if you cannot complete it. In particular, even if you cannot correctly implement the setter, do write down the getters and variable declarations.

importjava.util.List;

importjava.util.ArrayList;

publicclassALineAndPointCollectionimplementsLineAndPointCollection {

// instance variables

publicvoid setCompleteList(ListaList) { // complete list

}

publicListgetPoints() { //list of mathematical points

}

publicListgetLines() { // list of (segments of) mathematical lines

}

}

1