Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Problem Sheet 3, Question 1.
- *
- * Write a short implementation for a Point class with the following operations:
- *
- * public double getX() // get the x value
- * public double getY() // get the y value
- * public void movePoint(double x, double y) // move the point by x and y, relative to current position
- * public void setPoint(double x, double y) // set the point to x and y
- * public String toString() // write out the value of the point in the form "(x, y)"
- * public boolean equals(Point p) // compare if two points are equal
- *
- * Your class should have 2 constructors, one taking an x and y value, the other setting default values to 0.0.
- *
- * Additionally, a method called "distance" that returns the euclidian distance between itself and another point,
- * passed as a parameter.
- *
- * Finally, a method called "findMidpoint" that returns a point that is exactly halfway between itself and another point.
- * i.e.,
- * Point p1 = new Point(0.0,0.0);
- * Point p2 = new Point(4.0,4.0);
- * Point p3 = p1.findMidpoint(p2);
- *
- * Your Point class should fit this specification exactly.
- */
- public class Point{
- Point p1, p2;
- double x, y;
- public Point (double x, double y){
- this.x = x;
- this.y = y;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public void setX(double x){
- this.x = x;
- }
- public void setY(double y){
- this.y = y;
- }
- public void setPoint(double newX, double newY){
- x = newX;
- y = newY;
- }
- public void movePoint(double newX, double newY){
- x = x + newX;
- y = y +newY;
- }
- public String toString() {
- return );
- }
- public boolean equals(Point P) {
- return (p1.getX() == p2.getX() && p1.getY() == p2.getY());
- }
- public double distance(Point p) {
- double lengthA = (double)(this.getX() - p.getX());
- double lengthB = (double)(this.getY() - p.getY());
- double distt = Math.sqrt(((lengthA * lengthA) + (lengthB * lengthB)));
- return distt;
- }
- public Point midPoint(Point p) {
- double mx = (p.x + x)/2;
- double my = (p.y + y)/2;
- return new Point(mx,my);
- }
- public static void main(String[] args) {
- Point p1 = new Point(2,4);
- Point p2 = new Point(3,-6);
- double distance = p1.distance(p2);
- Point p3 = p1.midPoint(p2);
- System.out.print(distance); //just to test
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement