Write a Java program to find the projection of a point on the X and Y axes about the topic Defining Methods in Java.
In object-oriented programming, the concept of projecting a point onto the 'x-axis' and 'y-axis' is typically related to 2D geometry or graphics. The projection involves extracting or isolating the 'x' and 'y' components of a given point. Here's a theoretical representation of how you could implement this concept in an object-oriented program.
Let's start by defining a Point class that represents a point in 2D space:
Now, we can add methods to the 'Point' class for projecting the point onto the 'x-axis' and 'y-axis':
class Point:
The project_onto_x_axis method returns a new 'Point' object with the same x-coordinate as the original point but with a y-coordinate of 0, effectively projecting the point onto the x-axis. Similarly, the project_onto_y_axis method creates a new Point object with a y-coordinate of the original point and an x-coordinate of 0, projecting the point onto the y-axis.
Here's an example usage of the Point class and its projection methods:
Output:
In this example, we create a Point object 'p' with coordinates (3, 4). We then call the projection methods to obtain the projections of 'p' onto the x-axis and y-axis, which are (3, 0) and (0, 4) respectively.