icse-promo

Question

The coordinates of a point P on a two dimensional plane can be represented by P(x,y) with x as the x-coordinate and y as the y-coordinate.The coordinates of midpoint of two points P1(x1,yI) and P2(x2,y2) can be calculated as P(x,y) where:

 

x=(xl+x2)/2,y=(y1+y2)/2

Design a class Point with the following details:

 

Classname : Point

 

DataMembers / instancevariables:


x: stores the x-coordinate
y : stores the y-coordinate

 

Member functions:
Point():constructor to initialize x=0,y=0
void readpoint():accepts the coordinates x and y of a point
Point midpoint(PointA,PointB):calculates and returns the midpoint of the two
points A and B
void displaypoint():displays the coordinates of a point

 

Specify the class Point giving details of the constructor() , member functions void readpoint() ,Point midpoint(Point,Point) and void displaypoint() along with the main() function to create an object and call the functions accordingly to calculate the midpoint between any two given points.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Point
{
    double x,y;
    Scanner sc = new Scanner(System.in);

    public Point()
    {
        x=0.0;
        y=0.0;
    }

    public void readpoint()
    {
        System.out.println("enter x");
        x=sc.nextDouble();
        System.out.println("enter y");
        y=sc.nextDouble();
    }

    public void displaypoint()
    {
        System.out.println(x);
        System.out.println(y);
    }

    public Point midpoint(Point A,Point B)
    {
        Point C=new Point();
        C.x=(A.x+B.x)/2;
        C.y =(A.y+B.y)/2;
        return C;
    }

    public static void main()
    {
        Point p=new Point();
        Point q=new Point();
        Point r=new Point();
        p.readpoint();
        q.readpoint();
        r=r.midpoint(p,q);
        p.displaypoint();
        q.displaypoint();
        r.displaypoint();
    }
}



				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *