CSCI 1583Test No. 2

Name ______

Section ______

Test No. 2

Thursday, April 5, 2001

Part I.Mini-essays. Answer the questions as completely as you can without being too wordy. The space provided should be a good indication of the length of answer required.

  1. Describe what is meant by the term “programming by contract.” In your answer, discuss the responsibilities of client and server objects in this software development philosophy. Why is this approach appropriate in an object-oriented software system? (10 pts.)

  1. Discuss the following two large categories of relationships between objects in a system: “has-a” and “is-a”. How are these relationships implemented in Java? Give examples. (8 pts.)

  1. Briefly describe the following two stages in the development of software: specification and implementation. (7 pts.)

Part II.Matching. Indicate the choice from the second column that best matches the entry in the first column. Record the answers in the supplied scantron sheet. (1 pt. each)

Page 1

CSCI 1583Test No. 2

1.preconditions

2.postconditions

3.conditional statement

4.true, false

5.boolean expression

  1. guarantee of server to client of method
  2. checked through Require.condition()
  3. needed in ifand if-else statements
  4. literal values of type boolean
  5. executed when condition evaluates to true

Page 1

CSCI 1583Test No. 2

6.binary boolean operators

7.unary Boolean operator

8.compound statement

9.composite statement

10.relational operators

  1. has another statement within it
  2. !
  3. , , >=, <=, ==, !=
  4. || and
  5. several statements inside a {} pair

Page 1

CSCI 1583Test No. 2

11.this

12.this()

13.class Object

14.class String

15.class Math

  1. class that models text messages
  2. concrete class that is “mother of all classes”
  3. class where sqrt(…)is defined
  4. refers to the current class’ constructor
  5. refers to the current object

Page 1

CSCI 1583Test No. 2

16.constructor

17.private components

18.method parameters and arguments

19.instance variables

20.automatic variables

  1. used to instantiate objects of a class
  2. implement “has-a” relationship
  3. each object has its own set of these
  4. how client passes information to server
  5. exist only while method is active

Page 1

CSCI 1583Test No. 2

Page 1

CSCI 1583Test No. 2

Part III.True or False. If the statement is true, bubble A; otherwise bubble B. Record the answers in the supplied scantron sheet. (1 pt. each)

21.Programming by contract is a style of programming used only by contractual consultants. false

22.Properties of an object are usually implemented as component variables of its class. true

23.In an object-oriented software system, objects cooperate in order to achieve the intended functionality. true

24.Classes can have at most one constructor. false

25.Require.notNull(…) checks whether a certain variable has value other than 0.0. false

26.javadoc produces html documentation of java classes, packages, and interfaces. true

27.Client and server are object labels that do not change over time. false

28.The class Object, being an abstract class, can only supply abstract objects to potential users.

29.Query methods must have at least one return statement. true

30.Command methods return the void value and therefore do not have to be implemented. false

Part IV.Short coding. Supply the answers that are requested based on the short piece of Java code provided.

Consider the following query method in the class Tsk that returns an int value:

publicint hmmm( int m, int n ) {

int result;

if ( m > n )

result = m – n;

else

result = m + n;

return result;

}

Suppose there is a variable tsk which has been instantiated as a Tskobject.
  1. Indicate below what the value of the int expression

tsk.hmmm(11,-7)

would be: (5 pts.)

Value of expression is:

  1. Indicate below what the value of the int expression

tsk.hmmm(-7,11)

would be: (5 pts.)

Value of expression is:

  1. Indicate below what the value of the int expression

tsk.hmmm(-1,-1)

would be: (5 pts.)

Value of expression is:

Part V.Class implementation. A closed line segment on the Cartesian plane is completely determined by its two endpoints:

Consider the specification of a class that models such a line segment:

package geometry;

/**

* Class that models closed line segments on the Cartesian plane.

*/

publicclass LineSegment {

//constructors

/**

* Creates a unit line segment whose endpoints are (0,0) and (1,0)

* require:

* none

* ensure:

* this.endPoint1() == (0,0) & this.endPoint2() == (1,0)

*/

public LineSegment() { … }

/**

* Creates a line segment whose endpoints are given

* require:

* p and q are duly instantiated Point objects

* ensure:

* this.endPoint1() == p & this.endPoint2() == q

*/

public LineSegment( Point p, Point q ) { … }

//queries

/**

* What is one endpoint of the current line segment?

* none

* ensure:

* result is one of the endpoints of the current line segment

*/

public Point endPoint1() { … }

/**

* What is the other endpoint of the current line segment?

* require:

* none

* ensure:

* result is the other endpoint of the current line segment

*/

public Point endPoint2() { … }

//more

/**

* What is the length of the current line segment?

* require:

* none

* ensure:

* result is the length of the segment

*/

publicdouble length() { … }

/**

* Does the current line segment intersect the given line segment?

* require:

* s is a duly instantiated LineSegment object

* ensure:

* result accurately depicts whether the two segments intersect

*/

publicboolean intersects( LineSegment s ) { … }

/**

* What is the midpoint of the line segment?

* require:

* none

* ensure:

* result is equidistant from the two endpoints

*/

public Point midpoint() { … }

// commands

/**

* Set one endpoint to the given Point!

* require:

* p is a duly instantiated Point object

* ensure:

* this.endPoint1() == p

*/

public void setEndPoint1( Point p ) { … }

/**

* Set the other endpoint to the given Point!

* require:

* q is a duly instantiated Point object

* ensure:

* this.endPoint2() == q

*/

public void setEndPoint2( Point q ) { … }

/**

* Expand the line segment by the given scalar! retaining

* endpoint1 as “anchor”

* require:

* s > 0.0

* ensure:

* this.length() == s*old.length()

* this.endPoint1() == old.endPoint1()

*/

public void scale( double s ) { … }

} //end of class LineSegment //more next page

Since this class will make use of the class Point, we supply here an abbreviated specification of the Point class:

/**

* Creates a point at the origin

*/

public Point() { … }

/**

* Creates a point with given x- and y-coordinates

*/

public Point( double xCoordinate, double yCoordinate ) { … }

/**

* What is the x-coordinate?

*/

publicdouble xCoordinate() { … }

/**

* What is the y-coordinate?

*/

publicdouble yCoordinate() { … }

/**

* What is the String representation of the current Point?

* If current Point object was the origin, then ( 0.0, 0.0 ) would be

* the String representation of that point.

*/

public String toString() { … }

/**

* Change x-coordinate to given value!

*/

publicvoid setXCoordinate( double newXCoordinate ) { … }

/**

* Change y-coordinate to given value!

*/

public void setYCoordinate( double newYCoordinate ) { … }

  1. Obviously, we need to represent the properties of line segments (the two endpoints) as Point components. In the space below, write the declaration of two private components that implement these properties of LineSegment objects: (5 pts.)

//private components

private Point endPoint1; //one endpoint of the line segment

private Point endPoint2; //other endpoint of the line segment

  1. Implement the “lazy” constructor for class LineSegment according to the specification given earlier. Make sure that your code is consistent with your answer to (a) above, i.e., use the same private components. We start you off with the constructor’s documentation and specification; supply the body. (5 pts.)

/**

* Creates a unit line segment whose endpoints are (0,0) and (1,0)

* require:

* none

* ensure:

* this.endPoint1() == (0,0) & this.endPoint2() == (1,0)

*/

public LineSegment() {

  1. Implement the query midpoint() according to the specification given earlier. Again, be consistent with your answers in (a) and (b). (10 pts.)

/**

* What is the midpoint of the line segment?

* require:

* none

* ensure:

* result is the Point that is equidistant from the two endpoints

*/

public Point midpoint() {

  1. We propose a strategy for testing the implementation of the query midpoint(). Below, we supply this strategy. Supply the expected values by filling out the blanks. (5 pts.)

Simple test plan for the query midpoint():

ActionExpected ResultActual result

1.Create a unit line segment.

2.Display one endpoint.______

3.Display the other endpoint.______

4.Display its midpoint.______

  1. Supply the body of the tester. We assume the tester will reside in the geometry package. You are given the framework; supply the missing statements in the implementation of the test plan inform (d) above. (5 pts.)

/**

* Simple, throw-away tester of the class LineSegment’s midpoint() method.

*/

publicclass MidPointTester {

public static void main ( String [] args ) {

LineSegment ls = new LineSegment(); //No. 1 in plan

//display one endpoint; No. 2 in plan

______

//displaying the other endpoint; No. 3 in plan

System.out.println( “Endpoint No. 2: “ + ls.endPoint2() );

//display the midpoint; No. 4 in plan

______

} //end of main

} //end of class MidPointTester

Happy Semester Break!!!

Page 1