Q:

Java program to read coordinate points and determine its quadrant

belongs to collection: Java Basic Programs

0

Java program to read coordinate points and determine its quadrant

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will read XY coordinate points from the user and determine the quadrant.

Program/Source Code:

The source code to read coordinate points and determine their quadrant is given below. The given program is compiled and executed successfully.

// Java program to read coordinate points 
// and determine its quadrant

import java.util.Scanner;

public class Main {
  static int determineQuadrant(int X, int Y) {
    if (X == 0 && Y == 0)
      return 0;
    else if (X > 0 && Y > 0)
      return 1;
    else if (X < 0 && Y > 0)
      return 2;
    else if (X < 0 && Y < 0)
      return 3;
    else if (X > 0 && Y < 0)
      return 4;
    return -1;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int X;
    int Y;

    int ret = 0;

    System.out.printf("Enter the value of X: ");
    X = SC.nextInt();

    System.out.printf("Enter the value of Y: ");
    Y = SC.nextInt();

    ret = determineQuadrant(X, Y);

    if (ret == 0)
      System.out.printf("Point (%d, %d) lies at the origin\n", X, Y);
    else if (ret == 1)
      System.out.printf("Point (%d, %d) lies in the First quadrant\n", X, Y);
    else if (ret == 2)
      System.out.printf("Point (%d, %d) lies in the Second quadrant\n", X, Y);
    else if (ret == 3)
      System.out.printf("Point (%d, %d) lies in the Third quadrant\n", X, Y);
    else if (ret == 4)
      System.out.printf("Point (%d, %d) lies in the Fourth quadrant\n", X, Y);
  }
}

Output:

Enter the value of X: 20
Enter the value of Y: 30
Point (20, 30) lies in the First quadrant

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains two static methods determineQuadrant() and main().

The determineQuadrant() method is used to determine the quadrant based on given coordinates.

The main() method is an entry point for the program. Here, we read coordinates XY from the user using the Scanner class. Then we determined the quadrant and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate the value of nCr... >>
<< Java program to calculate the mean, variance, and ...