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

1. Add the following method to the Point class: public double distance(Point oth

ID: 3560651 • Letter: 1

Question

1.

Add the following method to the Point class:

public double distance(Point other)

Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of their x- and y-coordinates. In other words, the distance between two points (x1, y1) and (x2, y2) can be expressed as the square root of (x2 - x1)2 + (y2 - y1)2. Two points with the same (x, y) coordinates should return a distance of 0.0.

2.

Create a class called Name that represents a person's name. The class should have fields named firstName representing the person's first name, lastName representing their last name, and middleInitial representing their middle initial (a single character). Your class should contain only fields for now.

3.

Add two new methods to the Name class:

Returns the person's name in normal order, with the first name followed by the middle initial and last name. For example, if the first name is "John", the middle initial is 'Q', and the last name is "Public", this method returns "John Q. Public".

Returns the person's name in reverse order, with the last name preceding the first name and middle initial. For example, if the first name is "John", the middle initial is 'Q', and the last name is "Public", this method returns "Public, John Q.".

4.

Add the following method to the Point class:

public int quadrant()

Returns which quadrant of the x/y plane this Point object falls in. Quadrant 1 contains all points whose x and y values are both positive. Quadrant 2 contains all points with negative x but positive y. Quadrant 3 contains all points with negative x and y values. Quadrant 4 contains all points with positive x but negative y. If the point lies directly on the x and/or y axis, return 0.

Explanation / Answer

2)

Point.java


public class Point
{
   public int x;
   public int y;
   Point(int x,int y)
   {
       this.x=x;
       this.y=y;
   }
   public static void main(String[] args)
   {
       Point p1=new Point(-2,3);
       Point p2=new Point(3,-4);
       System.out.println("distance:"+getDistance(p1,p2));
       System.out.println("p1 quadrant:"+p1.quadrant());
       System.out.println("p2 quadrant:"+p2.quadrant());
   }
   private static double getDistance(Point p1,Point p2)
   {
       return Math.sqrt(Math.pow(p2.x-p1.x, 2)+Math.pow(p2.y-p1.y, 2));
   }
   public int quadrant()
   {
       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;
   }
  
}

sample outPut:

distance:8.602325267042627
p1 quadrant:2
p2 quadrant:4

Name.java


public class Name
{
   public String firstName;
   public String lastName;
   public char middleInitial;
   Name(String firstName,String lastName,char middleInitial)
   {
       this.firstName=firstName;
       this.lastName=lastName;
       this.middleInitial=middleInitial;
   }
   public String getNormalOrder()
   {
       return this.firstName+" "+this.middleInitial+"."+this.lastName;
   }
   public String getReverseOrder()
   {
       return this.lastName+","+this.middleInitial+" "+this.firstName;
   }
   public static void main(String[] args)
   {
       Name name1=new Name("John","Public",'C');
       System.out.println(name1.getNormalOrder());
       System.out.println(name1.getReverseOrder());
   }

}