CSE1030 Test 1T

Tue Oct 8
5:00PM - 6:20PM

Introduction

This test consists of a short programming question and some short answer written questions. I recommend budgeting 50 minutes for the programming question and 30 minutes for the written questions.

Please follow the instructions given in the test.

Setting Up

In eclipse:

  1. Create a new Java Project (perhaps called test1)
  2. In your project, create a new Package called cse1030.test1
  3. In the package you just created, create an empty text file named answers.txt for your answers to the written questions:
    1. select the File menu
    2. select New
    3. select File
    4. type answers.txt for the file name and press Finish
  4. Download this jar file and add it to your project:
    1. select the Project menu
    2. select Properties
    3. on the left side of the dialog that appears, select Java Build Path
    4. on the right, select the Libraries tab
    5. click the Add External JARs... button
    6. add the jar file you downloaded
    This jar file contains the class cse1030.drawing.IPoint2D whose API can be found here:

Programming Question

It is expected that your program compiles, meets the specifications given in the API, and follows the style guidelines for CSE1030. Javadoc comments are not required.

Complete the immutable class Circle so that it implements the following API:

This means that you must create and complete the following fields, constructors, and methods:

With the exception of equals, all of your constructors and methods should be very short (4 lines of code or less).

A small test program is available to help you debug your program:

The test program generates the following output for a correct program:

default constructor c1: (0.0, 0.0), 1.0
constructor         c2: (0.0, 0.0), 2.0
constructor         c3: (-1.0, 1.0), 3.0
copy constructor    c4: (-1.0, 1.0), 3.0
constructor         c5: caught an exception
c1.compareTo(c2)      : -1
c2.compareTo(c1)      : 1
c3.compareTo(c4)      : 0
c1.equals(c2)         : false
c2.equals(c1)         : false
c3.equals(c4)         : true
withDiameter        c6: (-2.0, 2.0), 5.0

Written Questions

Type the answers to the written questions into the file answers.txt that you were instructed to create earlier. Make sure that you number your answers to match the questions.

1. Consider the following Java program (not including any import statements):

public class X {

  private static IPoint2D point = null;
  private Circle circle;
  
  public X() {
    if (this.point == null) {
      this.point = new IPoint2D();
    }
    this.circle = new Circle();
  }
  
  public static void main(String[] args) {
    X x1 = new X();
    X x2 = new X();
    X x3 = new X();  // ???
    
    // more code here not shown
  }
}

(a) [2 marks] The program compiles and runs as expected, but there is a misleading style error in the code; what is the style error that was made, and how would you correct the error?

The field point is static and should be accessed using the name of the class instead of using this; i.e.,

  public X() {
    if (X.point == null) {
      X.point = new IPoint2D();
    }
    //...

After the line marked ??? in the main method runs:

(b) [2 marks] How many objects of type IPoint2D are in memory? Explain your answer.

The field point is static; thus, there is only 1 IPoint2D object in memory.

(c) [2 marks] How many objects of type Circle are in memory? Explain your answer.

The field circle is not static; thus, there is only 1 Circle object in memory for each X object in memory. There are 3 X objects in memory; thus, there are 3 Circle object in memory.

(d) [2 marks] What technique that we studied in the lectures (and in the notes) could you use to verify your answers to parts (b) and (c)? You may assume that you have access to the source code of IPoint2D and Circle.

We could add a static counter to the IPoint2D and Circle classes that increments by 1 each time a constructor is used (and provide a public method that returns the count).



2. Suppose that you made the field that stores the radius of the circle public in your implementation of the Circle class. We say that such an implementation has poor encapsulation.

(a) [2 marks] In one or two sentences, explain what you think the term poor encapsulation means.

Poor encapsulation means that the implementation details of the class are visible to clients.

(b) [4 marks] State two negative consequences of making the radius field public in the Circle class.

Clients can get the value of the radius of the circle without asking the Circle object.
Clients can modify the radius of the circle without notifying the Circle object.
Clients can modify the radius of the circle to any value; thus, Circle cannot guarantee class invariants involving the radius (e.g., the radius is always positive).
Clients can see how the radius is implemented.
The field cannot be made private without breaking all existing client code that currently uses the public field.



3. [4 marks] Is the compareTo method of Circle consistent with the equals method of Circle? Explain your answer, and give an example that illustrates your explanation.

No. compareTo uses only the radius to compare two circles whereas equals uses both the radius and position to compare two circles. In the example below, c.compareTo(d) == 0 is true but c.equals(d) is false.

  Circle c = new Circle(new IPoint2D(0., 0.), 5.);
  Circle d = new Circle(new IPoint2D(1., 1.), 5.);



4. [2 marks] The method withDiameter looks like a constructor in that it returns a reference to a new Circle object. Why is it not a constructor, and what is the name given to such a method?

This question should not have been asked because it was not covered in the lectures being tested.

Making it a constructor would require the signature Circle(IPoint2D, double) which already exists in the class. Methods such as withDiameter are called static factory methods.