Create a method that accepts two points p and q, which can be any real (decimal)
ID: 3678256 • Letter: C
Question
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).
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.
Create another method that returns the Euclidean distance from p to q: q (xq xp)^ 2 + (yq yp)^ 2
This method will accept the same number, types, and order of parameters as the first method, but with return a double.
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: 1
2 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.1 2 Otherwise, return the calculated slope
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. I suggest that you work out the answers for many cases by hand (or by using Wolfram Alpha) then compare those results to your program’s output. To evaluate your program, I will be using many test cases for p and q.
( https://www.dropbox.com/s/ljexghhrq3y40or/coordinates.pdf?dl=0 ) the link to the full question
Explanation / Answer
import java.util.Scanner;
public class coordinates {
public static String checkP(int xp,int yp)
{
/****Quadrant Checking for p*****/
if(xp>0&&yp>0)
{
return "in first quadrant....";
}
else if(xp>0&&yp<0)
{
return "in fourth quadrant....";
}
else if(xp<0&&yp>0)
{
return "in second quadrant....";
}
else if(xp<0&&yp<0)
{
return "in Third quadrant....";
}
else if(xp==0&&yp==0)
{
return "on Origin....";
}
else if(xp>0&&yp==0)
{
return "on x axis....";
}
else if(xp==0&&yp>0)
{
return "on Y axis....";
}
return null;
}
public static String checkQ(int xq,int yq)
{
/*******************Quadrant Checking for Q************************/
if(xq>0&&yq>0)
{
return "in first quadrant....";
}
else if(xq>0&&yq<0)
{
return "in fourth quadrant....";
}
else if(xq<0&&yq>0)
{
return "in second quadrant....";
}
else if(xq<0&&yq<0)
{
return "in Third quadrant....";
}
else if(xq==0&&yq==0)
{
return "on Origin....";
}
else if(xq>0&&yq==0)
{
return "on x axis....";
}
else if(xq==0&&yq>0)
{
return "on Y axis....";
}
return null;
}
public static double eclidian(int px,int py,int qx,int qy)
{
/**********Eclidian Distance*************************************/
return Math.sqrt((px-qx)*(px-qx)+(qy-py)*(qy-py));
}
public static double CheckSlope(int px,int py,int qx,int qy)
{
/*************Slope*******************************/
try {
double m=(qy-py)/(qx-px);
return m;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Divide by zero error.......Try again.");
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int xp,yp,xq,yq;
xp=sc.nextInt();
yp=sc.nextInt();
xq=sc.nextInt();
yq=sc.nextInt();
System.out.println(checkP(xp,yp));
System.out.println(checkP(xq,yq));
System.out.println(eclidian(xp, yp, xq, yq));
System.out.println(CheckSlope(xp, yp, xq, yq));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.