1. Create a method that accepts two points p and q, which can be any real (decim
ID: 3678654 • 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).
Instead of hard-coding the arguments to the methods in main(), have the main() method prompt the user for input using a Scanner.
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.Scanner;
public class Coordinate {
public static String quadp(double xp, double yp)
{
String quadrant;
if(xp == 0 && yp == 0) return "Origin";
else if(xp != 0 && yp == 0) return "X-axis";
else if(xp == 0 && yp != 0) return "Y-axis";
else if(xp > 0 && yp > 0) return "1st Quadrant";
else if(xp < 0 && yp > 0) return "2nd Quadrant";
else if(xp < 0 && yp < 0) return "3rd Quadrant";
else if(xp > 0 && yp < 0) return "4th Quadrant";
return "0";
}
public static String quadq(double xq, double yq)
{
String quadrant;
if(xq == 0 && yq == 0) return "Origin";
else if(xq != 0 && yq == 0) return "X-axis";
else if(xq == 0 && yq != 0) return "Y-axis";
else if(xq > 0 && yq > 0) return "1st Quadrant";
else if(xq < 0 && yq > 0) return "2nd Quadrant";
else if(xq < 0 && yq < 0) return "3rd Quadrant";
else if(xq > 0 && yq < 0) return "4th Quadrant";
return "0";
}
public static double distance (double xp, double yp, double xq, double yq)
{
return Math.sqrt((xp-xq)*(xp-xq)+ (yp-yq)*(yp-yq));
}
public static double slope (double xp, double yp, double xq, double yq)
{
if(xp-xq == 0) {
System.out.println("Slope is undefined");
System.exit(1);
return 0;
}
else return ((yq-yp)/(xq-xp));
}
public static void main (String args[])
{
Scanner userInput = new Scanner (System.in);
System.out.println("Enter xp: ");
double xp = userInput.nextDouble();
System.out.println("Enter yp: ");
double yp = userInput.nextDouble();
System.out.println("Enter xq: ");
double xq = userInput.nextDouble();
System.out.println("Enter yq: ");
double yq = userInput.nextDouble();
System.out.println("1st point lies in "+ quadq(xp,yp));
System.out.println("2nd Point lies in "+ quadq(xq,yq));
System.out.println("distance between the two points is "+ distance(xp,yp,xq,yq));
System.out.println("Slope of the line formed by two points is "+ slope(xp,yp,xq,yq));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.