Name _______________________________ NetId ____________________________
This lab gives you practice with writing a class, using a JUnit tester, and working with static methods.
Task 1. Download. Download file ThreeDimPoint.java from here:
Save it in a new directory. When you are done with this lab, you may want to save this file —email it to yourself or put it on a USB storage key.
ThreeDimPointis a 3-dimensional point (x, y, z). The class has 3 fields,
z; a constructor; and 3 getter functions. Understand what those four methods are supposed to do FROM THEIR SPECIFICATIONS. Don't look at the bodies yet.
New JUnit Test Case.... This will create a second file; call it
DimTester.java. Save it to the same directory that your
ThreeDimPoint.javafile is in.
testConstructor, and change the javadoc comments appropriately.
testConstructor, write Java statements that:
(a) Create an instance of
ThreeDimPointand save its name in a variable. Note that the constructor call in the new expression requires three integer arguments —the x, y, and z coordinates of the point.
(b) Insert tests to verify that the constructor sets the fields properly. Refer to this partial example of testing a constructor (http://www.cs.cornell.edu/courses/cs1110/2010fa/handouts/labs/lab03junit-constructorTest.pdf) for help (the first page is a sample class; the second page shows a partial test method for that class's constructor). Remember, inherited procedure
assertEquals(expected, computed)stops all testing and outputs an error message if
expecteddoes not equal
Repeat steps 4, 5, and 6 until no error message results.
Congratulations! You have used a JUnit tester to debug your first program.
Task 4. Test function hasAZero.
This function should return true if at least one of the x-,
y-, and z- coordinates is 0. If none of them are zero, it returns
false. That's what the specification says. Do not make any changes to the
function before testing it. In class
make up another test function,
that will test function
hasAZero. A possible
test case is any set of values (x, y, z). Think about it: how many test cases
do you need in order to be sure that the method is correct? Perhaps 6 or 7
or 8? Below, write down a list of test cases that you think will suffice to
provide some assurance that the function is correct:
In test procedure
all the test cases that you think you need. The test procedure may have to create
more than one instance of
ThreeDimPoint in order
to implement all your test cases.
Now compile the test program (click button
Compile) and run it (click button
If you get error messages, look at the program and fix errors. Continue testing
and debugging in this fashion until running the test program does not produce
Task 5. Trying to make a method static.
The length of the line from point (0, 0, 0) to point (x, y, z) is the square root of x*x + y*y + z*z. In Java, function call
finds the square root of
n, as you can see from method
the length of the line from the origin given by the instance in which it appears. Try it out
using these statements in the Interactions pane:
d= new ThreeDimPoint(3, 4, 5);
d.length() // Write the answer here:
Now do the same thing using this single statement.
(new ThreeDimPoint(3,4,5)).length() // Write the answer here:
The second way, just above, contains a new-expression that is NOT assigned to
a variable. This is legal. There is nothing wrong with it. The new folder (object) is created
and stored in file-drawer
ThreeDimPoint, and the name of the folder is the result
of the new-expression; then, function
length() of that folder is called.
Now, make the function static by placing the word
public and compile. What happens?
Write here the error message that is printed:
What is the problem? Explain in your own words what the problem is:
Then remove the word
static and compile
Task 6. Making a method static. Now consider
length1. It has the heading
public double length1(ThreeDimPoint c)
so when one calls it, one has to give it an argument that is the name of a
ThreeDimPoint folder. We'll show you this
in a minute.
Make this method
static, by placing keyword
public, and compile. It works! Can you explain
Yes, the method does not refer directly to any fields of the object in which
the method occurs, so it does not have to be in each folder of the class, so
it can be static. There is ONE copy of it, and the copy is in file-drawer
Now do these lines in the Interactions pane, one at a time. The first creates
ThreeDimPoint folder and stores its name in variable f. The second gets its
length by calling function length in object
f. The third gets its length by calling
ThreeDimPoint f= new ThreeDimPoint(3,4,5);
This illustrates how one can use a static method. If a method does not refer to any fields, place it in the file-drawer by making it static. If you still have questions about this, ASK THE TA OR CONSULTANT!
Task 7. Class Math. Class
Math has lots
of static functions that are mathematical in nature. Class
Math, which is
always available without using an import statement, is used mainly as a file-drawer for
static functions and variables. It has no fields that you can change. It has
no instance methods. Everything in it is static. Below are some functions and what
they mean; later, we will tell you where to find their descriptions.
|Math.abs(x)||absolute value of x||Math.abs(-4) is 4|
|Math.sqrt(x)||square root of x||Math.sqrt(25) is 5.0|
|Math.min(b,c)||minimum of b and c||Math.min(5, 4) is 4|
|Math.max(b,c)||maximum of b and c||Math.max(6,4) is 4|
|Math.floor(x)||largest integer that is not larger than x||Math.floor(3.2) is 4.0
Math.floor(3.2) is 3.0
|Math.ceil(x)||smallest integer that is not smaller than x||Math.ceil(3.2) is 3.0
Math.ceil(3.2) is 4.0
|Math.PI||best double approximation to pi, the ratio of the diameter of a circle to its circumference|
Fill in the following table by placing the calls in the Interactions pane and seeing what value DrJava gives: