complete the code in the two classes : Finish the Point class. The class should
ID: 3596315 • Letter: C
Question
complete the code in the two classes :
Finish the Point class. The class should have:
An instance variable for the x-coordinate and an instance variable for the y-coordinate, both ints (THIS IS DONE)
A constructor that takes values for the x- and y-coordinates, and initializes the instance variables (THIS IS DONE)
A print method, that prints the x-and y-coordinates in the form: (x,y). (YOU DO THIS)
A quadrant method, that returns an int for which quadrant that point is in. In the case of points on an axis, return any quadrant that matches (YOU DO THIS)
Quadrant 1: Upper-right
Quadrant 2: Upper-left
Quadrant 3: Lower-left
Quadrant 4: Lower-right
Finish the Lab7 class. Part of this class is already done. In the end, you should:
Create two Point objects: one that represents (3,5) and one that represents (-2,7)
Call the print method on each point
Call the quadrant method for each point, and print the result
public class Lab7 {
public static void main(String[] args) {
Point point1 = new Point(3,5);
//You do this: create another point that represents (-2, 7)
//prints the information for the first point
point1.print();
//You do this: print the information for your second point
//You do this: call the quadrant method for each point, and print each result
}
}
public class Point {
private int x;
private int y;
public Point(int newX, int newY) {
x = newX;
y = newY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Explanation / Answer
Please find my implementation.
public class Point {
private int x;
private int y;
public Point(int newX, int newY) {
x = newX;
y = newY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void print() {
System.out.println("("+x+","+y+")");
}
public int quadrant() {
if(x >= 0 && y >= 0)
return 1;
else if(x < 0 && y > 0)
return 2;
else if(x < 0 && y < 0)
return 3;
else
return 4;
}
}
############
public class Lab7 {
public static void main(String[] args) {
Point point1 = new Point(3,5);
//You do this: create another point that represents (-2, 7)
Point point2 = new Point(-2,7);
//prints the information for the first point
point1.print();
//You do this: print the information for your second point
point2.print();
//You do this: call the quadrant method for each point, and print each result
int q = point1.quadrant();
System.out.println("Point 1 lies in quadarant: "+q);
q = point2.quadrant();
System.out.println("Point 2 lies in quadarant: "+q);
}
}
/*
Sample run:
(3,5)
(-2,7)
Point 1 lies in quadarant: 1
Point 2 lies in quadarant: 2
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.