0% found this document useful (0 votes)
6 views3 pages

Point

The document contains a Java class named 'Point' that represents a point in a 2D space with methods for calculating distances, reflections, and midpoints. It includes constructors for initializing points, a method to read coordinates from user input, and a main method that allows users to interact with the class. The class also demonstrates functionality through a simple console interface for distance calculation, reflection, and midpoint determination.

Uploaded by

golyanpanav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Point

The document contains a Java class named 'Point' that represents a point in a 2D space with methods for calculating distances, reflections, and midpoints. It includes constructors for initializing points, a method to read coordinates from user input, and a main method that allows users to interact with the class. The class also demonstrates functionality through a simple console interface for distance calculation, reflection, and midpoint determination.

Uploaded by

golyanpanav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

File: Untitled Document 1 Page 1 of 3

import java.util.Scanner;
/**
* Write a description of class Podouble here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Point
{
double x;//This variable stores the numeric value in the x axis
double y;//This variable stores the numeric value in the y axis

//This is a default constructor


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

//This is a parameterised constructor


public Point(double x , double y)
{
this.x = x;
this.y = y;
}

//This function finds the distance from origin to point


double distanceOrigin ()
{
double xFinal = x * x;
double yFinal = y * y;
return Math.sqrt((xFinal + yFinal));
}

//This function finds the distance from a point to other point


double distance (double x2 , double y2)
{
double xFinal = Math.pow((x2-x) , 2);
double yFinal = Math.pow((y2-y) , 2);
return Math.sqrt((xFinal + yFinal));
}

//This reads the coordinates from the user


void readCoordinates()
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter the coordinates of x axis\t");


x = sc.nextInt();
System.out.print("Enter the coordinates of y axis\t");
y = sc.nextInt();
}

void get(int x , int y)


{
this.x = x;
this.y = y;
}

//This function finds the reflection from origin


Point reflectionOrigin ()
{
Point ans = new Point();

ans.x = -x;
ans.y = -y;
File: Untitled Document 1 Page 2 of 3

return ans;
}

//This function finds the midpoint of two points


Point midPoint (Point P)
{
Point ans = new Point();

ans.x = (x + P.x) / 2;
ans.y = (y + P.y)/2;

return ans;
}

//This function prints the coordinates


void printCoordinates()
{
System.out.print("The coordinates of the points are :" + x + "," + y);
}

public static void main(String [] args)


{
Scanner sc = new Scanner(System.in);
int option;

Point p1;
p1 = new Point();

Point p2;
p2 = new Point(3 , 5);

p1.readCoordinates();
System.out.println();

p1.printCoordinates();
System.out.println();
p2.printCoordinates();
System.out.println();

System.out.println("Enter 1. to use Distance function");


System.out.println("Enter 2. to use Reflection in origin function");
System.out.println("Enter 3. to use Midpoint function");
option = sc.nextInt();

switch(option)
{
case 1:
System.out.print("The Distance between the two points are:" +
p1.distance(p2.x , p2.y));
break;
case 2:
System.out.print("The reflection in origin of point 1 is:" +
p1.reflectionOrigin());
System.out.print("The reflection in origin of point 2 is:" +
p2.reflectionOrigin());
break;
case 3:
System.out.print("The midpoint of the two points are:" + p1.midPoint(p2));
break;
}
/**
* Output
*
* Enter the coordinates of x axis 3
Enter the coordinates of y axis 4
File: Untitled Document 1 Page 3 of 3

The coordinates of the points are :3.0,4.0


The coordinates of the points are :3.0,5.0
Enter 1. to use Distance function
Enter 2. to use Reflection in origin function
Enter 3. to use Midpoint function
1
The Distance between the two points are:1.0
*/
}
}

You might also like