CS 101/101-E, Fall 2004

Lab quiz 2

Week of 25 Oct 2004

 

General notes

A couple of notes on the lab quiz:

 

Purpose

For this lab quiz, you are to submit a single file named OrderedPair.java which contains a OrderedPair class.  No template is being given for this lab quiz.  A OrderedPair represents two values, such as a 2-dimensional coordinate: (1,2), for example.

 

Instance Variables

The OrderedPair class is to have only two int member variables, x and y, which represent the values of the ordered pair.

 

Methods

The class should have the following 6 methods, one of which we provide:

  1. Default constructor: this constructor initializes both integers in the ordered pair to 1.
     
  2. Facilitator toString(): this method returns a String representation of the OrderedPair.  This method MUST be copied exactly as follows.

    public String toString() {
        return "(" + this.x + ", " + this.y + ")";
    }
     
  3. Accessor get(): this method takes one int parameter, and returns an int value.  If the parameter is 1, the method returns the first integer of the ordered pair; otherwise, it returns the second integer of the ordered pair.  For example, if the ordered pair was (6,7), passing in 2 to method get() would return the second value (i.e. 7).
     
  4. Mutator set():  this method takes two int parameters, a and b, and does not return a value.  Parameter a specifies which element of the ordered pair to set, and parameter b specifics the value to which it is set.  If parameter a is 1, then the first integer of the ordered pair is set to the value of b; else, the second integer of the ordered pair is set to the value of b.  For example, if the ordered pair is (5, 6), and set() was called with parameters 2 and 8, then the ordered pair would be changed to be (5, 8).
     
  5. Mutator zero(): this method sets both integers in the ordered pair to zero.
     
  6. Facilitator distance(): this method returns (as a double) the distance between the point the ordered pair represents and the origin.  This distance can be determined by the following formula: .  Note that the Math.sqrt() method will return (as a double) the square root of its passed value.

 

Grading

Make sure each of the methods are named EXACTLY as specified.  Also, make sure the toString() method is copied EXACTLY as provided.  The toString() method, as well as the names of the other methods, are used by the automatic grading routine.  If they are not exactly as specified, the automatic grading routine will give you a zero for pretty much all of the lab quiz.

 

main() method

There is no main() method required in the OrderedPair class - you are welcome to include one for testing purposes (it will not affect your grade one way or the other if it is included).  You can include this main() method in either the OrderedPair.java class, or another class, such as was done with Strand.java and StrandDemonstration.java in HWs J3 and J4.  A sample main() method is shown below with the corresponding output.

 

Sample usage

The following main method will produce the following output

Sample main() method:

public static void main (String args[]) {
    OrderedPair p = new OrderedPair ();

    System.out.println (p);
    p.zero();
    System.out.println (p);
    p.set (1, 3);
    System.out.println (p);
    p.set (2, 4);
    System.out.println (p);
    System.out.println ("Distance: " + p.distance());
    System.out.println ("Test of get(): " + p + " = (" + p.get(1) +
                        ", " + p.get(2) + ")");
}



Output:

(1, 1)
(0, 0)
(3, 0)
(3, 4)
Distance: 5.0
Test of get(): (3, 4) = (3, 4)


Submission

Submit only the OrderedPair.java file here.