SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Conditional Execution
Exercise:
Exercises
Question:16 | ISBN:9780136091813 | Edition: 2

Question

Write a method called quadrant that accepts as parameters a pair of double values representing an (x, y) point and 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 counterclockwise fashion:

Notice that the quadrant is determined by whether the x and y coordinates are positive or negative numbers. Return 0 if the point lies on the x- or y-axis. For example, the call of quadrant(-2.3, 3.5) should return 2 and the call of quadrant(4.5, -4.5) should return 4.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package chap456;

public class Quadrent {

    public int quadrent(double a, double b) {

        // set quadrent to zero incase if it falls on x axis or y axis
        int quadrent = 0;

        if (a > 0 && b > 0)
            quadrent = 1;

        if (a < 0 && b > 0)
            quadrent = 2;

        if (a < 0 && b < 0)
            quadrent = 3;

        if (a > 0 && b < 0)
            quadrent = 4;

        
        return quadrent;
    }

    public static void main(String[] args) {

        Quadrent que = new Quadrent();
        int quadrentNumber = que.quadrent(4.5,-4.5);
        System.out.println("the point lies in the " + quadrentNumber + " quadrent");

    }

}
Output:

the point lies in the 4 quadrent

 

0 0

Discussions

Post the discussion to improve the above solution.