Finding the Quadrant of a Point in Java
๐๐ป ๐๐๐ฉ๐ ๐ณ๐ผ๐๐ป๐ฑ ๐๐ต๐ถ๐ ๐ฝ๐ฟ๐ผ๐ฏ๐น๐ฒ๐บ ๐ถ๐ป๐๐ฒ๐ฟ๐ฒ๐๐๐ถ๐ป๐ด ๐๐ผ ๐ฏ๐ฒ ๐๐ผ๐น๐๐ฒ๐ฑ ,so i did and learnt something-
๐ฃ๐ฟ๐ผ๐ฏ๐น๐ฒ๐บ:
The task was to identify whether the coordinate given by the user ,falls in the 1st, 2nd, 3rd, or 4th quadrant. Additionally, I needed to handle edge cases where the point might lie on the x-axis or y-axis.
๐๐ผ๐ ๐ถ ๐ฑ๐ถ๐ฑ ๐ถ๐ :
One interesting aspect of this project was managing the input. The user enters coordinates in the format x, y (e.g., 1, 2). Using the split() method in Java, I was able to ignore the comma and correctly parse the coordinates as integers, actually what id did was to store entire coordinate in an array and then separate it with comma and then store in 2 elements of the array . This approach ensures that even with spaces around the comma, the program processes the input smoothly.
๐๐ผ๐ป๐ฑ๐ถ๐๐ถ๐ผ๐ป๐ฎ๐น ๐๐ผ๐ด๐ถ๐ฐ: the heart of the program lies in the if-else statements, which determine the quadrant based on the signs of x and y, and then i made all possible combinations
๐ช๐ต๐ฎ๐ ๐ ๐๐ฒ๐ฎ๐ฟ๐ป๐ฒ๐ฑ ๐จ๐ป๐ถ๐พ๐๐ฒ (this is the only thing i learned unique because the if-else thing was cakewalk for me already)
Input Parsing: Handling user input effectively is crucial, especially when it involves common delimiters like commas. This approach helped me better understand how to clean and parse input data.
Subscribe to my newsletter
Read articles from Faizan Firdousi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by