Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the program. there is no error in programming question. CUSTOMER SERVICE

ID: 3626948 • Letter: T

Question

This is the program. there is no error in programming question. CUSTOMER SERVICE people dont cancel this one ..bcoz someone has answer for this one. please let them post their answer.

Add compare and cloning capabilities to your Point class as follows:
Add a new method, equals( ), which takes a parameter of type Object and returns a boolean: true if equal, false otherwise.
Modify the TestPoint application to verify that your equals() method works.
Indicate that the Point class implements Cloneable.
Add a new method, copy(), which takes no parameters and returns an object of type Point.
Modify the TestPoint application to verify that your copy() method works.
Modify the up(), down(), left(), and right() methods of the Point class to return an object of type Point.
Modify the TestPoint application to verify that your new versions of the up(), down(), left(), and right() methods return objects of type Point

Explanation / Answer

please rate - thanks

been waiting for you to repost



public class Point implements Cloneable
{
private int x, y;
public Point()
{setPoint( 0, 0 );
}
public Point(int x, int y )
{setPoint(x,y);
}
public void setPoint(int x, int y)
{setX(x);
setY(y);
}
public int getX()
{return x;
}
public int getY()
{return y;
}
public void setX(int newX)
{x=newX;
}
public void setY(int newY)
{y=newY;
}

public String toString()
{return "("+x+","+y+")";
   }
public Point up(int i)
{Point t=new Point();
t=this.copy();
t.y+=i;
return this;
}
public Point down(int i)
{Point t=new Point();
t=this.copy();
t.y-=i;
return this;
}
public Point left(int i)
{Point t=new Point();
t=this.copy();
t.x-=i;
return this;
}
public Point right(int i)
{Point t=new Point();
t=this.copy();
t.x+=i;
return this;
}
public boolean equals(Point p)
{if(x==p.x&&y==p.y)
     return true;
    return false;
    }
public Point copy()
{return this;
}
protected Object clone() throws CloneNotSupportedException {

    Point clone=(Point)super.clone();

      return clone;

}   
}

------------------------------------------

import java.util.*;
public class TestPoint
{public static void main(String[] args)
   {Point p1=new Point();
    Point p2=new Point(2,1);
    Point p3=new Point(2,1);
    System.out.println("p1="+p1.toString());
    System.out.println("p2="+p2.toString());
    System.out.println("p3="+p3.toString());
   
    if(p1.equals(p2))
        System.out.println("p1 and p2 are equal");
    else
        System.out.println("p1 and p2 are not equal");
   if(p3.equals(p2))
        System.out.println("p3 and p2 are equal");
    else
        System.out.println("p3 and p2 are not equal");
   p3=p1.copy();
        System.out.println("copy p1 to p3: p3="+p3.toString());

    p3=p2.up(2);
    System.out.println("after up(2): p3="+p3.toString());
   p3=p2.left(5);
    System.out.println("after left(5): p3="+p3.toString());
   p3=p2.down(1);
    System.out.println("after down(1): p3="+p3.toString());
p3=p2.down(4);
    System.out.println("after down(4): p3="+p3.toString());
p3=p2.right(1);
    System.out.println("after right(1): p3="+p3.toString());
p3=p2.right(4);
    System.out.println("after right(4): p3="+p3.toString());
p3=p2.up(2);
    System.out.println("after up(2): p3="+p3.toString());
p3=p2.left(2);
    System.out.println("after left(2):p3="+p3.toString());

try
{
    Point p4=(Point)p3.clone();
    System.out.println("p4=p3 cloned="+p4.toString());

    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
}
}
    }

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote