Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 3: Write an interface called Movable that defines an abstract method mov

ID: 649414 • Letter: P

Question

Problem 3: Write an interface called Movable that defines an abstract method move(). Write a class called Point which has two instance variables indicating its x and y co-ordinates. Write another class called Circle which has three instance variables indicating the co-ordinates of its center and its radius. Both Point and Circle should implement the Movable interface so that when the following piece of code is executed, the output is as shown below.

Code snippet:

Point p = new Point( 2, 3 );

p.print();

p.move( 4, 5 );

p.print();

Circle c = new Circle( 3, 4, 2 );

c.print();

c.move( 2, 5 );

c.print();

Output:

Point is at (2, 3).

Point is at (6, 8).

Circle center is at (3, 4) and radius is 2.

Circle center is at (5, 9) and radius is 2.

Explanation / Answer

abstract class A

{

abstract void move(int, int)

}

interface movable extends A

{

int x, y;

void move(a, b)

{

a = x + a;

b = x + b;

}

class point implements movable

{

int x, y;

void print()

{

System.out.println("point is at", x ,y);

}

}

class circle implements movable

{

int x, y, radius;

void print()

{

System.out.println("point is at", x ,y, "and radius is", 2);

}

public static void main(String arg[])

{

point p = new point(2,3);

p.print();

p.move(3,4);

p.print();

circle c = new circle(2, 4, 5);

c.print();

c.move(2,1);

c.print();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote