-3

Kindly help me to to fix this code for a Java program for:

In the 2- dimensional plane, a point is described by its two coordinates x and y. It supports these operations :

  • A constructor allowing initialization of both coordinates
  • Accessors and mutators to its coordinates
  • Translation of a point

a. Write a Java class, called MyPoint, that corresponds to such an abstraction of points in the dimensional plane.

b. Provide a tester class that creates one point, then translates and displays its new coordinates.

Here is the code:

import java.io.*;
class MyPoint
{
    private int x,y;
     MyPoint(int x, int y)
       {
           this.x=x;
           this.y=y;
       }
     int getx()
       {
           return this.x;
        }
       int gety()
       {
           return this.y;
        }
        void setx(int x)
        {
            this.x=x;
        }
       void sety(int y)
        {
            this.y=y;
        }
       void translate(int x,int y)
       {
           this.x=x;
           this.y=y;
        }  
    public static void main(String args[])
    {
        MyPoint P1=new MyPoint(2,3);
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jony mark
  • 1
  • 4

1 Answers1

0

Well this is not exactly an answer but longer than a comment, so read it as such.

the translate method should be like this:

void /*MyPoint*/ translate(int dx,int dy)
       {
           this.x += dx;
           this.y += dy;

          // you can add a "return this;" to your methods
          // to allow for chaining methods
          // e.g point = new Point(0,0).translate(1,1);
          //return this;
        } 
Nikos M.
  • 8,033
  • 4
  • 36
  • 43