To Find the Origin of a Point using a Function in Java

# Objective

Write a program to find the origin of a point using a function in Java.

# Theory

Here's an example implementation:

1. public class Point{
2.  private int x;
3.  private int y;
4.  Point() {
5.   this.x = 0;
6.   this.y = 0;
7.  }
8.  Point(int xInit, int yInit) {
9.   this.x = xInit;
10.   this.y = yInit;
11.  }
12.  private void setX(int xCoord) {
13.   this.x = xCoord;
14.  }
15.  private void setY(int yCoord) {
16.   this.y = yCoord;
17.  }
18.  private int getX() {
19.   return this.x;
20.  }
21.  private int getY() {
22.   return this.y;
23.  }
24.  private void print() {
25.   System.out.println("(" + this.x + "," + this.y+ ")");
26.  }
27.  private boolean isOrigin() {
28.   if((this.x==0)&&(this.y==0))
29.    return true;
30.   else
31.    return false;
32.  }
33. public class Driver {
34.  public static void main(String[]){
35.   Point p = new Point(4,7);
36.   System.out.println(p.isOrigin());
37.   Point q = new Point();
38.   System.out.println(q.isOrigin());
39.  }

The given code consists of two classes: "Point" and "Driver". Let's explain the code step by step:

Class Point:

• Attributes: The Point class has two private integer attributes "x" and "y" that represent the coordinates of a point.
• Constructors: The Point class has two constructors.
• The default constructor "Point()" initializes the "x" and "y" attributes to 0.
• The parameterized constructor "Point(int xInit, int yInit)" takes initial values for "x" and "y" as arguments and assigns them to the corresponding attributes.
• Private methods:
• "setX(int xCoord)" and "setY(int yCoord)" are private methods that allow modifying the values of "x" and "y" attributes, respectively.
• "getX()" and "getY()" are private methods that retrieve the values of "x" and "y" attributes, respectively.
• "print()" is a private method that prints the coordinates of the point in the format "(x, y)" using the "System.out.println()" method.
• "isOrigin()" is a private method that checks if the point is at the origin (0, 0). It returns true if both "x" and "y" are zero; otherwise, it returns false.

Class Driver:

• The "Driver" class contains the main method, which serves as the entry point of the program.
• Inside the "main" method an instance of the "Point" class named "p" is created using the parameterized constructor with coordinates (4, 7).
• The "isOrigin()" method is called on "p", which checks if the point "p" is at the origin (0, 0). The result (false) is printed using "System.out.println()".
• Another instance of the "Point" class named "q" is created using the default constructor, which initializes "x" and "y" to 0.
• The "isOrigin()" method is called on "q", which checks if the point "q" is at the origin (0, 0). The result (true) is printed.

The program demonstrates the usage of the "Point" class to create point objects and check if they are at the origin. In the "Driver" class, two instances of the "Point" class are created: "p" with coordinates (4, 7) and "q" with coordinates (0, 0). The "isOrigin()" method is called on each point object to determine if it is at the origin, and the results are printed.

When you run the program, the expected output will be: This indicates that the point "p" with coordinates (4, 7) is not at the origin, while the point "q" with coordinates (0, 0) is at the origin.

# Learning Outcomes

• Understanding classes and objects: You should have gained an understanding of how classes are used to define objects and their behavior in object-oriented programming.
• Creating a class: You should be familiar with the process of creating a class, including defining attributes (data) and methods (functions) within the class.
• Defining class methods: You have learned how to define methods within a class, which are functions that operate on objects created from that class.