For the following class What would be output by running this

For the following class What would be output by running this program? Public class point {Private double x; private double y; public point (double_x, double_y) {x = _x; y = _y;} 1.0 2.0 3.0 4.0 1.0 4.0 3.0 6.0

Solution

yes you are right...

Point p1 = new Point(1.0, 2.0);

so p1 hold values

x => 1.0

y => 2.0

p1.moveVertically(2.0);

add y + 2.0

2.0 + 2.0

4.0

System.out.println(p1.x + \" \" + p1.y);

p1.x => 1.0

p1.y => 4.0

Here is code with explaination in comments:

public class Point {
private double x;
private double y;
public Point(double _x, double _y)
{
x = _x;
y = _y;
}
public void moveVertically(double offset)
{
y += offset; //y hold the value of p1 (i.e 2.0 + 2.0 => 4.0)
}
public static void main(String[] args)
{
Point p1 = new Point(1.0, 2.0); // call Point call constructor with 1.0 and 2.0 assign to p1
Point p2 = new Point(3.0, 4.0);// call Point call constructor with 3.0 and 4.0 assign to p2
p1.moveVertically(2.0); // call moveVertically()
System.out.println(p1.x + \" \" + p1.y); // print 1.0 and 2.0
}
}

Answer is :

1.0 4.0

 For the following class What would be output by running this program? Public class point {Private double x; private double y; public point (double_x, double_y)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site