Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that has Point.java as its file. The Point class will repre

ID: 3570579 • Letter: W

Question

Write a Java program that has Point.java as its file. The Point class will represent a point in 2-Dimensional space. The Point class will have two instance variables: x and y. Both instance variables will be of type double and private. You will create two constructors: a default no argument constructor and a constructor that takes two integers as arguments(representing x and y coordinates). You will also create accessors and muators for each instance variable. You will then implement the following instance methods:

Finally, in the main method(in the Point class), create some Point objects(two or three will be sufficient) and test your instance methods. You should print the return values of each method.

Example Output:

Explanation / Answer

//Point.java
public class Point
{

//varaibles to store x and y values of a point
   private double x;
   private double y;
  
   //default arguments
   public Point() {
      
       x=0;
       y=0;
   }
  
   //parameter arguments
   public Point(double x,double y)
   {
       // TODO Auto-generated constructor stub
       this.x=x;
       this.y=y;
   }
  
   //setter methods
   public void setX(double x)
   {
       this.x=x;
   }
   public void setY(double y)
   {
       this.y=y;
   }
  
   //getter methods
   public double getX()
   {
       return x;
   }
   public double getY()
   {
       return y;
   }
      
   //Distance from Origin, Assumed to be (0,0)
    public double distFromOrg()
    {
       return Math.sqrt(x*x+y*y);
    }
    //Distance between two Points
    public double DistFromPoint(Point P)
    {
       return Math.sqrt((P.getX()-x)*(P.getX()-x)+(P.getY()-y)*(P.getY()-y));
    }
    //Return quadrant is the point object in
    public int whatQuadrant()
    {
       if(x>0 && y>0)
           return 1;
       else if(x<0 && y>0)
           return 2;
       else if(x<0 && y<0)
           return 3;
       else
           return 4;
    }
    //Slope between two points
    public double slope(Point P)
    {
       return (P.getY()-y)/(P.getX()-x);
    }

   @Override
   public String toString()
   {
      
       return "("+x+","+y+")";
   }
  
}


--------------------------------------------------------------------------------------------------------------------------------------------

//Tester java class

import java.text.DecimalFormat;

public class PointTester
{
   public static void main(String[] args)
   {
      
       Point point1=new Point(3, 2);
       Point point2=new Point(1, 2);
       DecimalFormat formatter=new DecimalFormat("0.###");
       System.out.println("Distatce between"+point1+
               "and"+point2+" : "+point1.DistFromPoint(point2));
              
        System.out.println("Slope of a line passing through "
               +point1+" and "+point2+" : "+point1.slope(point2));
      
        System.out.println("Distatce between "+
        point1+" and the origin :"+formatter.format(point1.distFromOrg()));
      
        Point point3=new Point(2, -5);
        System.out.println("Point "+ point3+" is in quadrant : "+point3.whatQuadrant());
      
   }
}


---------------------------------------------------------------------------

Sample output:

Distatce between(3.0,2.0)and(1.0,2.0) : 2.0
Slope of a line passing through (3.0,2.0) and (1.0,2.0) : -0.0
Distatce between (3.0,2.0) and the origin :3.606
Point (2.0,-5.0) is in quadrant : 4

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote