Full Question
Write a static method called quadrant that takes as parameters a pair of real numbers representing an (x, y) point and that returns the quadrant number for that point.
Recall that quadrants are numbered as integers from 1 to 4 with the upper-right quadrant numbered 1 and the subsequent quadrants numbered in a counter-clockwise fashion. Notice that the quadrant is determined by whether the x and y coordinates are positive or negative numbers. If a point falls on the x-axis or the y-axis, then the method should return 0.
Answer
public int quadrant(double x, double y) // Line 1
{
if(x > 0 && y > 0){ //Line 2
return 1;
}
if(x < 0 && y > 0){ // Line 3
return 2;
}
if(x < 0 && y < 0) {// Line 4
return 3;
}
if(x > 0 && y < 0) {// Line 5
return 4;
}
return 0;
}
Explanation:
Line 1 defines the static method.
The method is named quadrant and its of type integer.
Along with the method definition, two variables x and y of type double are also declared
Line 2 checks if x and y are greater than 0.
If yes then the program returns 1
Line 3 checks if x is less than 0 and y is greater than 0
If yes then the program returns 2
Line 4 checks if x and y are less than 0
If yes then the program returns 3
Line 5 checks is x is greater than 0 and y is less than 0
If yes then the program returns 4