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

1. Write an interface called Moveable with four void methods moveUp(), moveDown(

ID: 3790432 • Letter: 1

Question

1. Write an interface called Moveable with four void methods moveUp(), moveDown(), moveLeft() and moveRight().

2. Write a class called MoveablePoint that implements the Moveable interface above. This class contains two int fields x and y. Write the constructors, accessors and mutators, toString method and an “equals” (inherited from Object class) method that compares the states of its objects.

3. Write another class called MoveableCircle that extends the MoveablePoint. The x and y fields represent the center of the circle and have an int variable called radius. Write the constructors, accessors and mutators, toString method, “equals” method and a method called area that prints the area of the circle.

4. Write a client class that creates one object of MoveablePoint and one object of MoveableCircle class. Test the moveUp(), moveDown(), moveLeft(), moveRight() methods for each object and print the object with each move. Also use the “equals” method to compare different objects and print out the results of the comparison. Call the area method on the objects of class MoveableCircle.

Explanation / Answer

   /*------Java Interface-------------*/
   interface Moveable
   {
   /* All the methods are public abstract by default
       * Note down that these methods are not having body
       */
   public void moveUp();
   public void moveDown();
   public void moveLeft();
   public void moveRight();
   }
   /*-------mplementing inerface--------*/
   class MoveablePoint implements Moveable
   {
   private int x,y;
   public void moveUp() //override methods of interface
   {
       y--;
   }
   public void moveDown() {
   y++;
   }
   public void moveLeft() {
   x--;
   }
   public void moveRight() {
   x++;
   }
   MoveablePoint (int x,int y){ //constructor
   this.x=x;
   this.y=y;
   }
   //Accessors methods
   public int getX(){
   return this.x;
   }
   public int getY(){
   return this.y;
   }
   //Mutator methods
   public int setX(int x){
   this.x =x;
   }
   public int setY(int y){
   this.y =y;
   }
   //toString method
   public String toString() {
   return "Point at (" + x + "," + y + ")";
   }
   public boolean equals(Object obj) {
           if (obj instanceof MoveablePoint)
               return x.equals((MoveablePoint)obj.getX());
           else
               return false;
       }

   }
   class MovableCircle implements Moveable {
   // instance variables
   private MovablePoint center; // can use center.x, center.y directly
                                     
       private int radius;
       public MovableCircle(int x, int y,int radius) {
       // Call the MovablePoint's constructor to allocate the center instance.
       center = new MovablePoint(x, y);
       radius =this.radius;
       }
       public void moveUp()
       { center.y = y--;
       }
       public void moveDown()
       { center.y = y++;
       }
       public void moveLeft()
       { center.x =x-- ;
       }
       public void moveRight()
       { center.x = x++;
       }
   //Mutator methods
   public int setX(int x){
   center.x =x;
   }
   public int setY(int y){
   center.y =y;
   }
   public int getX(){
   return center.x;
   }
   public int getY(){
   return center.y;
   }
   //toString method
   public String toString() {
   return "Point at (" + center.x + "," + center.y + ")";
   }  
     
   public void Area(int radius)
   {
   float pi=3.14;
   float area = pi*radius*radius;
   System.out.println("Area of circle is "+area );
   }
       }

   public class TestMovable {
   public static void main(String[] args) {
   Movable m1 = new MovablePoint(5, 5); // upcast
   System.out.println(m1);
   m1.moveDown();
   System.out.println(m1);
   m1.moveRight();
   System.out.println(m1);
   Movable m2 = new MovableCircle(2, 1, 20); // upcast
   System.out.println(m2);
   m2.moveRight();
   System.out.println(m2);
      
   }
   }