CS 101/101-E, Fall 2004
Lab quiz 2
Week of 25 Oct 2004
General notes
A couple of notes on the lab quiz:
- The activities of this week are not to be discussed outside of
lab. The quiz is open book but nothing else.
- The lab assistants may not help you design, write, or debug the
programs.
- If the program does not compile, you will get a very, very low
grade. There is a compile button on the submitter - please use it
to make sure your submission compiles.
- To maximize your points, make sure your programs compile
successfully. If things are going bad near the end, you might want to
try commenting out errant statements (i.e., put a // in front of
them). It is better to have only part of the program working
rather than it not compiling.
- The program must follow the standard "good grading guidelines" as
discussed in lecture. In particular, the header must include your
name, e-mail ID, and whether you are in 101 or 101-E. The
commenting, whitespace, variable names, and line length should all be
appropriate. No legend or echoing of input is necessary for this
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:
- Default constructor: this constructor initializes both integers
in the ordered pair to 1.
- 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 + ")";
}
- 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).
- 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).
- Mutator zero():
this method sets both integers in the ordered pair to zero.
- 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.