Problem Statement: Create Java Application for the following . Create Point class Point2D in package "geom" : for representing a point in x-y co-ordinate system. Create a parameterized constructor to accept x & y co-ords. Add show method to Point2D class : to return the x & y coords .(public String show())
Answer:
-------------------------------------------------------------------------------------------------------------------------
package geom;
public class Point2D {
private int x, y;
public Point2D(int p, int q) {
x = p;
y = q;
}
public String show() {
return ("(" + x + "," + y + ")");
}
public boolean isEqual(Point2D p2) {
return x==p2.x && y==p2.y;
}
public Point2D createNewPoint(int xoffset, int yoffset) {
return new Point2D(x + xoffset, y + yoffset);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
Answer:
-------------------------------------------------------------------------------------------------------------------------
package geom;
public class Point2D {
private int x, y;
public Point2D(int p, int q) {
x = p;
y = q;
}
public String show() {
return ("(" + x + "," + y + ")");
}
public boolean isEqual(Point2D p2) {
return x==p2.x && y==p2.y;
}
public Point2D createNewPoint(int xoffset, int yoffset) {
return new Point2D(x + xoffset, y + yoffset);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
-------------------------------------------------------------------------------------------------------------------------
package tester;
import java.util.Scanner;
import geom.Point2D;
class TestPoint
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter x,y for 1st point");
Point2D p1=new Point2D(sc.nextInt(),sc.nextInt());
System.out.println("Enter x,y for 2nd point");
Point2D p2=new Point2D(sc.nextInt(),sc.nextInt());
System.out.println("p1 "+p1.show());
System.out.println("p2 "+p2.show());
System.out.println("Enter x_offset & y_offset");
Point2D p3=p1.createNewPoint(sc.nextInt(),sc.nextInt());
System.out.println("p3 "+p3.show());
System.out.println("p1 & p2 are "+(p1.isEqual(p2)?"equal":"unequal"));
System.out.println("p1 & p3 are "+(p1.isEqual(p3)?"equal":"unequal"));
}
}
-------------------------------------------------------------------------------------------------------------------------
Output:
Enter x,y for 1st point
2 3
Enter x,y for 2nd point
4 5
p1 (2,3)
p2 (4,5)
Enter x_offset & y_offset
5 9
p3 (7,12)
p1 & p2 are unequal
p1 & p3 are unequal
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
Sign up here with your email
ConversionConversion EmoticonEmoticon