lab7package in the labs source folder.
Update by right-clicking (control-click on Mac) the main project in the package explorer, drag down to Team... and click Update.
You should see a lab7 package in the labs source folder.
There will be many red flags in VectorAndPointTest because you have not yet implemented the Vector class. That's OK.
This is the last lab in which you will not be penalized greatly for style problems.
Thus it would behoove you to practice good style in this lab, ask style questions of the TA, so as to be prepared to exhibit good style on future labs.
You are encouraged to work on Parts I and II using test-driven development.
Test-driven development means that the code you write is governed by the tests your code has to pass. For this lab, you are provided a
VectorAndPointTestJUnit test case. You will use the test methods in that file to drive your implementation of
You will follow this same methodology as you develop your Point class.
If you think about them this way, you can see how vectors can
be added, as shown below.
You are to proceed with the development of your Vector class by considering these tests one at a time. For example, the init() test will require you to write the constructor for Vector.You will have red flags because Vector is not yet complete, but you can run the JUnit test nonetheless. If you expand the lab7.VectorAndPointTest in the JUnit window, you can see which tests are passing or not. Clicking on a given test shows you the detail below in the Failure Trace window.After that, the arith() test would have you implement Vector's plus and minus methods.
A green checkmark indicates success; a dark X indicates failure; a red X indicates compilation problems not yet addressed.
As you follow the instructions below, develop and then test each method one at a time! Read through the instructions before and during your develpment to guide your writing of the Vector class.
To accomplish this, declare two instance variables in the Vector class, and name them deltaX and deltaY. Both should be of type double.
Naming conventions: By convention, names of instance variables and methods should begin with lower case letters, to distinguish them from class names, which begin with upper case letters. If a variable has multi-word name, we usually capitalize subsequent words. For example, thisIsAVeryLongVariableName.
Encapsulation: Objects usually contain data, and it is good design practice to make sure that this data can't be "messed with" by other classes. Other classes should call methods on the object to access the information. That way, each class can control what is seen and, more importantly, how it is modified.
Make both of the instance variables in your Vector class private by typing the keyword private at the beginning of the declaration, before the type of variable.When you make the variables private, Java will make sure that the only way that code in other classes can see or modify their values is by calling methods of the Vector class.
Tip: You can type the constructor yourself if you want, but Eclipse provides some tools for generating these kinds of constructors automatically. To create the constructor automatically, first position your text cursor on the class name at the beginning of the file. Then open the Source menu and select "Generate constructor using fields." Finally, select the boxes for deltaX and deltaY so that the constructor will have parameters that are used to initialize those instance variables.Name masking: A method or constructor may have a parameter whose name is the same as the name of an instance variable. For example, you might have a parameter and instance variable both with the name "deltaX." When a name is used in a program, it refers to the "closest" declaration. So any use of the name "deltaX" would refer to the parameter. In this case, we say that the parameter masks the instance variable. But inside the method or constructor, we may still want to use or change the value of the instance variable. Within a method, the keyword this always refers to the object on which the method has been invoked (i.e., "this" object). When an instance variable is masked, you can still refer to it by preceding its name with "this." For example, inside the method, this.deltaX = deltaX will assign the value of the parameter deltaX to the instance variable named deltaX inside "this" object.
Implicit targets: Normally, when you call a method on a target
object, you identify the target, and then identify the method and its
actual parameters. For example,
calls the deposit method on the object to which alice refers. In other words, alice is the target. If you don't identify a target, then it is assumed that the target is the same object that is currently executing a method. For example, if we call the deposit method on alice and inside of the deposit method there is an expression getBalance(), then it is understood that the method will be called on alice since that is the object in which the deposit method is executing. In such cases, it is not necessary (and considered bad style) to use the word "this" because it is already understood that this object is the target.
Tip: You can type the methods yourself, or you can open the Eclipse Source menu and select "Generate Getters and Setters." Check the boxes for "getDeltaX" and "getDeltaY." Study the methods after creating them.
At this point, your VectorAndPointTest JUnit test should pass its init() test.
Since Vector objects will be immutable, the rest of these methods will create new vectors as their return values. They'll do this by first computing the desired deltaX and deltaY values, and then using the Java keyword new to call the constructor you wrote earlier. You can define local variables inside the methods whenever it's convenient, but remember that the final result of each method will be a new vector. Don't modify the object on which the method was called.
Note: You may want to add the word final to the declaration of your instance variables. This indicates that the instance variable's value should be established by the constructor and not be changed by any other method. Adding final will prevent you from accidentally changing the value in the methods you write for the Vector class.
Hint: The parameter type and return type of this method are both Vector. When you create the new vector to be returned, you will need to supply parameter values. To compute those parameter values, you can use both "this" vector (the one on which the method was called) and the vector that was passed in as a parameter.
Challenge: Write minus in terms of methods you have already defined for the Vector class. Which computer science principle are you applying by doing that?
At this point, your VectorAndPointTest JUnit test should also pass its arith() test.
Recall: Scaling a vector by some factor can be accomplished by scaling its deltaX and deltaY components by that same factor. Be sure to return a new vector, and don't change the one on which this method was called.
At this point, your VectorAndPointTest JUnit test should also pass the testScaleAndDeflect() test.
Hint: First call the magnitude method to find this vector's magnitude and save it in a local variable. Use this to compute a scale factor, and then let the scale method do the rest of the work.NOTE: If the target of the rescale method has a zero magnitude, no particular direction is defined for the resulting vector. One could consider this an error condition, but for the purposes of this assignment, if the original magnitude is zero, let the resulting vector have deltaX equal to the given magnitude, and deltaY equal zero.
At this point, make sure your code passes all the VectorAndPointTest cases provided with this lab. Do NOT change the test code (we are watching and will know if the revision changes on that file).
Note: You are encouraged to develop Point as you did Vector, using test-driven development.
Write a minus method of the Point class that takes another Point as its parameter and returns the appropriate vector.
Try to implement this by reducing this problem to calls of methods you have already written
When you done with this lab, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so