Lab Tutorial 2 Write a program that takes the x - y coordinates of a point in th
ID: 3757372 • Letter: L
Question
Lab Tutorial 2 Write a program that takes the x - y coordinates of a point in the Cartesian plane and prints a message telling either an axis on which the point lies or the quadrant in which it is found. The program will allow user to continue enter the point values with appropriate prompt message (Do you want to continue next entry [YIN]?) Q1I Qll aIV Sample lines of output: Please enter x and y coordinates: -1.0 -2.5 (-1.0, -2.5) is in quadrant III Do you want to continue [Y/N] : Y Please enter x and y coordinates: 0.0 4.8 (0.0, 4.8) is on the y-axis Do you want to continue [Y/N]: NExplanation / Answer
Implemented in JAVA
import java.util.Scanner;
// Create main class CartersianDemo
public class CartesianDemo
{
public static void main(String [] args)
{
Scanner scr=new Scanner(System.in); // create scr object of Scanner
char ch='y'; // declare constant character ch=y
while(true) // create infinite while loop until press 'n' or 'N'
{
// read two coordinates x and y
System.out.print("Please enter x and y coordinates: ");
double x=scr.nextDouble();
double y=scr.nextDouble();
System.out.print("("+x+","+y+")"); // display x and y values
if(x==0) // check x=0 (outer condition)
if(y==0) // check y=0 (inner condition)
System.out.println(" is Origin"); // display the point is origin
else
System.out.println(" is on the y-axis"); // y!=0 then display point i on th y-axis
else
if(y==0) // else, check y=0
System.out.println(" is on the x-axis"); // display is on the x -axis
else
if(x>0) // check x > 0 (outer condition)
if(y>0) // check y > 0 (inner condition)
System.out.println(" is in quadrant I"); // then display is in quadrant I
else
System.out.println(" is in quadrant IV"); // else, y<0, then display is in quadrant IV
else
if(y>0) // check y > 0
System.out.println(" is in quadrant II"); // then, display is in quadrant II
else
System.out.println(" is in quadrant III"); // else, display is in quadrant III
System.out.print("Do you want to continue [Y/N] :"); // System display message
ch=scr.next().charAt(0); // read character
if(ch=='n'||ch=='N') break; // check condition ch=n or ch=N then terminate while loop
}
}
}
OUTPUT
F:>javac CartesianDemo.java
F:>java CartesianDemo
Please enter x and y coordinates: -1.0 -2.5
(-1.0,-2.5) is in quadrant III
Do you want to continue [Y/N] :y
Please enter x and y coordinates: 0.0 4.8
(0.0,4.8) is on the y-axis
Do you want to continue [Y/N] :y
Please enter x and y coordinates:
4.8 0.0
(4.8,0.0) is on the x-axis
Do you want to continue [Y/N] :n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.