Question
Creating and Using Classes Start this lab with the project lab-0111-mypoint.zip. This class uses JUnit (though that is not the focus for this lab) so
Creating and Using Classes
Start this lab with the project lab-0111-mypoint.zip. This class uses JUnit (though that is not the focus for this lab) so JUnit will have to be set up for you to get this lab working. This project contains an incomplete class, MyPoint. Complete the class to have the following attributes:
Instance fields
x and y are decimal values
Methods
Getters and setters for x and y
A method distance that returns the distance from this point to a point specified in the parameter. Here is the method header: public double distance(MyPoint otherPoint) You can find the distance function described in numerous places. There are many ways to go about implementing that method. There is even a method in class Math that does much of the work.
A method midpoint that returns the midpoint between this point and another point, specified in the parameter. Here is the method header: public MyPoint midpoint(MyPoint otherPoint) Notice that this method returns a MyPoint object. So, it will have to create that object in the method code and then return it. The coordinates of the midpoint are the averages of the corresponding coordinates of the two given points.
Be careful about method names and parameter lists. In most of your exercises (labs and assignments) you will be asked to use particular headers, it is important that you follow those requirements.
The project contains a program for you to examine. If you have correctly implemented the class MyPoint, the program should run and the messages should match the results displayed.
Create a program
In the package named program, create a program file: this is just a Java class with a public static void main in it. Name the program class TryoutMyPoint. The program should do the following:
create a MyPoint object and give it coordinates 5 and 7
Create another MyPoint object and give it coordinates 13 and 13
Compute and display the distance between the points, giving a suitable label. (the distance should be 10)
Compute the midpoint between the two points and display the x and y coordinates of the point. (they should be 9 and 10)
Run the Unit Tests
Look in the test package. You will find two test programs: TestStructure and TestPoint. Both of those should run with no errors. The file Tests.java just contains support methods.
The unit test TestPoint is a straightforward test of your implementation.
The unit test TestStructure tests the structure of your class implementation. This will point out if you have the wrong number of instance fields or if any of them are not private. The test will also check the number of methods defined.
There is a sample program named TriangleProgram.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started