1. Create a method that accepts two points p and q, which can be any real (decim
ID: 3678387 • Letter: 1
Question
1. Create a method that accepts two points p and q, which can be any real (decimal) number. Each
Coordinate should be a separate parameter (i.e., you will have four parameters, xp, yp, xq, and yq).
2. The method should identify and return which quadrant or axes that p and q are located. There are
Seven possible outcomes: Quadrants I through IV , x-axis, y-axis, and the origin. For now, your
Method can return this information as a String.
3. Create another method that returns the Euclidean distance from p to q:
(xq - xp)^2 + (yq - yp)^2
This method will accept the same number, types, and order of parameters as the _rst method, but
with return a double.
4. Create another method that returns the slope of the line pq formed by connecting p and q. The slope
is expressed as m = (yp - yq)=(xp - xq), but it must be robust to errors caused by certain inputs:
5. If the slope is undefined, do not perform the calculation due to a divide-by-zero error. Instead,
print the error message Slope is undefined." inside the method and call System.exit(1) to end
your program.
Otherwise, return the calculated slope.
6. Create a main() method that:
1. Calls each of the above methods with various test cases (NOTE: be sure to test the divide-by-zero
prevention you coded above).
2. Prints the result of each of the method calls.
THIS IS ONE QUESTION AS A WHOLE>
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Points
{
/* Method to find the quadrant of both the points p and q*/
public String quadrant(double xp, double yp, double xq, double yq){
String p_quadrant=getQuadrant(xp,yp);
String q_quadrant=getQuadrant(xq,yq);
return "Point p is at "+p_quadrant+" and Point q is at "+q_quadrant;
}
/* Method to get the quadrant of each passed point*/
public String getQuadrant(double x, double y){
if(x==0 && y==0){
return "Origin";
}
else if(x==0){
return "Y-axis";
}
else if(y==0){
return "X-axis";
}
if (x >= 0) {
return (y >= 0 ? "1st Quadrant":"4th Quadrant");
} else {
return (y >= 0 ? "2nd Quadrant":"3rd Quadrant");
}
}
/* Method to get the euclidean distance between p and q */
public double euclidean(double xp, double yp, double xq, double yq){
double euc_distance = 0.0;
double x_square=Math.pow((xq-xp), 2);
double y_square=Math.pow((yq-yp), 2);
euc_distance= Math.sqrt(x_square+y_square);
return euc_distance;
}
/* Method to calculate the slope */
public double slope(double xp, double yp, double xq, double yq){
double x_diff= xp-xq;
double slope=0.0;
/* Check applied to avoid a divide by zero error */
if(x_diff == 0){
System.out.println("Slope is undefined");
System.exit(1);
}
else{
slope=(yp-yq)/x_diff;
}
return slope;
}
public static void main (String[] args) throws java.lang.Exception
{
/* Creating an object of Points and calling each method individually and printing the value*/
Points p = new Points();
double euc=p.euclidean(2.3, 5.6,0.5,9);
String quad=p.quadrant(0, -5.6,0,0);
double slop=p.slope(0,0.5,0.6,9);
System.out.print("Euclidean:"+euc+" Quadrant:"+quad+" Slope:"+slop);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.