Given the class \'Point\' which has two member variables x, and y. What will the
ID: 3747168 • Letter: G
Question
Given the class 'Point' which has two member variables x, and y. What will the output of the first "System.out.println" statement in the main method ?
True
False
Won't print anything
Error
True
False
Won't print anything
Error
public class Point f private int x; private int y; public void setX(int x) this.x -x; public void setY(int y) this.yey; public void setXY(int x, int y,) x-y; public boolean IsEqual (Point p) public void DisplayPoint() System.out.println("("+x "," + y ")"); public void movePoint(int deltax, int deltay) (// 'x' is increased by deltax and y is increased by deltay public void SetLocation (Point P) (//make point to have the specified location P. public static void main(String[] args) t /I TODO Auto-generated method stub Point P1 new Point(); Point P2 -new Point(); P1.setx(5); P1.setY (6); P2.setx(5); P2.setY (6); System.out.println(P1.IsEqual(P2)); P2.setXY(3,4); System.out.println(P1.IsEqual(P2));Explanation / Answer
Answer : (D)
The variables x and y are private to the class. Now, in the function IsEqual(), we are accessing the variables x and y directly i.e:
p.x
p.y
So, it will cause an error as they are not public.
In order to correctly run the code, we can create method
getX()
and
getY()
which return the values of x and y and then modify the IsEqual function like
public boolean IsEqual(Point p)
{
return ( ( p.getX() == x ) && ( p.getY() == y ) );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.