526,021 views
42 votes
42 votes
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.
public class Point {
int x;
int y;
// your code goes here
}
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:
public String getNormalOrder()
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".
public String getReverseOrder()
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.
public class Point {
private int x;
private int y;

// your code goes here
}

User Nineninesevenfour
by
2.6k points

1 Answer

7 votes
7 votes

Answer:

Step-by-step explanation:

The following code is written in Java and creates all of the methods that were requested in the question. There is no main method in any of these classes so they will have to be called from the main method and call one of the objects created method for the code to be tested. (I have tested it and it is working perfectly.)

class Point {

private int x, y;

public void Point(int x, int y) {

this.x = x;

this.y = y;

}

public double distance (Point other) {

double distance = Math.sqrt(Math.pow((other.x - this.x), 2) + Math.pow((other.y - this.y), 2));

return distance;

}

public int quadrant() {

if (this.x > 0 && this.y > 0) {

return 1;

} else if (this.x < 0 && this.y > 0) {

return 2;

} else if (this.x < 0 && this.y < 0) {

return 3;

} else if (this.x > 0 && this.y < 0) {

return 4;

} else {

return 0;

}

}

}

class Name {

String firstName, lastName;

char middleInitial;

public String getNormalOrder() {

String fullName = firstName + " " + middleInitial + " " + lastName;

return fullName;

}

public String getReverseOrder() {

String fullName = lastName + ", " + firstName + " " + middleInitial;

return fullName;

}

}

User Shaimagz
by
2.5k points