-3

I have 3 classes: Point, Polygon and ClientePolygon. Point creates an object, Point(double x, double y) with many nonstatic methods. Polygon creates an array of Point with non-static methods, i.e., write, move, and return the Point closest to the origin in the array. ClientePolygon executes Polygon. The problem is that when I run ClientePolygon it gives me Point class executed... and I want to run Polygon. Any suggestions? Thanks a lot.

ClientePolygon:

package k;
public class ClientePolygon {
    public void main(String [] args){
        Point origem = new Point (0,0);
        Point [] vertices = new Point[4];
        vertices[0] = new Point (1.0,1.0);
        vertices[1] = new Point (1.0,5.0);
        vertices[2] = new Point (2.0,5.0);
        vertices[3] = new Point (2.0,1.0);

        for (int i = 0; i < vertices.length; i++) {

        vertices[i].translate(5.0,7.5);

        }
    }
}

Point:

package k;

public class Point {

    private double x;
    private double y;

    public Point(double x, double y){

        this.x = x;

        this.y = y;

    }

    public double getX(){

        return x;

    }

    public String toString(){

        return "("+ this.x+", "+this.y+")";
    }

    public Point copy(){

        Point copia = new Point(x, y);

        return copia;

    }


     public static double distance(Point ponto1, Point ponto2){

         double distX = ponto1.x + ponto2.x;

         double distY = ponto1.y + ponto2.y;

         return Math.sqrt(distX*distX + distY*distY);

     }

     public void translate(double dx, double dy){

         double transX = this.x + dx;

         double transY = this.y + dy;

         System.out.println(toString()+" fica "+ transX +" e "+ transY);

     }

     public static void main(String[] args) {

         Point ponto1 = new Point(2,1);

         Point ponto2 = new Point(3,9);

         System.out.println(ponto1.toString());

         System.out.println(ponto2.toString());

         System.out.println("A distancia entre os pontos eh: " +distance(ponto1, ponto2));

         System.out.println("O ponto copiado eh: "+ponto2.copy());

         ponto2.copy().translate(2, 3);

    }

}

Polygon:

package k;

public class Polygon {

    private Point [] vertices;

    public Polygon(Point [] vertices){

        this.vertices = vertices;

    }

    public String toString(){

        return "Ponto 1 em: "+vertices;

    }

    public Point closestToOrigin(){

        Point proximo = new Point(0, 0);
        Point origem = new Point(0,0);

        for (int i = 0; i < vertices.length; i++) {

            double distancia = Point.distance(vertices[i], origem);

            if(distancia<Point.distance(vertices[i-1], origem)){

                proximo = vertices[i];

            }

        }

        return proximo;

    }

    public void translate(double dx, double dy){

        Point Trans = new Point(dx, dy);

        System.out.println("Ponto 1 fica: "+vertices[0]+Trans+", Ponto 2 fica: "+vertices[1]+Trans+" e Ponto 3 fica: "+vertices[2]+Trans);

    }

}
RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Madeira
  • 1
  • 1
  • 1
  • 3
    non static methods can only be called from another class, if you have an instance of the class with the method you want to call. – Tom K Jan 13 '17 at 18:32
  • 1
    _"array of Point with non-static methods"_ and _"when i run ClientePolygon it gives me Point class executed"_ - these statements don't make sense and seem to indicate a misunderstanding of basic concepts. You need to clarify what you are asking. – Jim Garrison Jan 13 '17 at 18:35
  • You want to "run Polygon" on *what*, exactly? – David R Tribble Jan 13 '17 at 18:36
  • I dont know why but if i run ClientePolygon, it runs another program. And if i put on comments (the other program), when i run ClientePolygon it says he cant find the main class of the other program. weird... – Madeira Jan 13 '17 at 20:11

2 Answers2

0

You must instantiate the Polygon class before calling a nonstatic method:

Polygon poly = new Polygon(vertices);

Then you can do:

poly.translate(5.0,7.5);
PMARINA
  • 304
  • 4
  • 17
JFPicard
  • 5,029
  • 3
  • 19
  • 43
0

To call a nonstatic method you need to instantiate the other class by doing

Class object = new Class(parameters);

and then call the method on that object:

object.method();
PMARINA
  • 304
  • 4
  • 17