简体   繁体   中英

Call a method from another class(not main class)

How to call distanceTo(Point p) of Point.java into Point2.java under a method takes no parameter? There should be a way but I cannot find from my materials. Could anybody help me? It has been doing 2 days. Please help...

---------------------Point.java---------------------------------

public class Point{
    private int x;
    private int y;

    //x and y coordinates as parameters
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }

    //I want to call this method by calling a method which taken no parameter in Point2.java.
    public double distanceTo(Point p){
          return Math.sqrt(((x - p.x) * (x - p.x)) + ((y - p.y) * (y - p.y)));
    }
}

---------------------ClonePoint.java---------------------------------

public class ClonePoint{
    private int a;
    private int b;

    //x and y coordinates as parameters
    public ClonePoint(int a, int b){
        this.a = a;
        this.b = b;
    }

    //I failed with this way.  Can anybody correct me?
    public double measureDistance(){//it should be takes no parameter.
        return distanceTo(ClonePoint p)
    }

}

----------------------PointDriver.java-----------------------------

public class PointDriver {
    public static void main(String [] args) {

        Point2 nn = new Point2(11, 22);
        Point2 mm = new Point2(33, 44);


        System.out.println(nn.distanceTo(mm)); //I succeeded with this!
        System.out.println(nn.measureDistance(mm)); //But I got an error illegal start of expression
    }
}

@Evan a class is a generalized container for your things. A car, a person, a point (in your case).

Everytime you want to "create" one or more object of your defined class, you instantiate them:

Person evan = new Person();
Person rob = new Person();

both of us are person, you don't really need to define class Person1 and Person2 !

And in a class you should define the methods used to "relate" to other similar objects. For example:

// In Person.java
public void greet(Person p) {
    System.out.println("My name is "+this.name+". Nice to meet you +"p.getName());
}
// In main
rob.greet(evan); // it now gives compile errors of course but take the point :P

What you want to achieve is to create a better and more complete Point class with all the methods you want to use. In the end, just initialize more Point objects (same class!) in your main and play with them.

Hope it helps :)

EDIT

Ok, perhaps I've got what your homework wants you to perform.

A "parameter-less" method measureDistance() should make you wonder one important thing: "distance FROM which point????".

Obviously, if the function takes no parameters all the information needed to that calculus must be in the object which calls it. Don't you think?

So, you probably want to achieve a secondary class (if you really need to define it as Point2 it's ok, but change that name because it's confusing) which can take a Point in its constructor (saving this information in itself) and then use that Point to measure distance from it.

Example

public class Point2{
    private int a;
    private int b;
    private Point startingPoint;

    public Point2(int a, int b, Point p){
        this.a = a;
        this.b = b;
        startingPoint = p;
    }

    // Computes the distance from starting point to this
    public double measureDistance(){//it takes no parameter.
        return startingPoint.distanceTo(a, b);
    }

    /*
    if you can't edit distanceTo() it gets a little verbose but you must create a
 Point with Point2 coordinates - remember this example when you will study Inheritance

    public double measureDistance() {
        Point endingPoint = new Point(a, b);
        return startingPoint.distanceTo(endingPoint);
    }
    */

}

First, it is not good idea to duplicate a class that does the same thing because you are doing extra unneeded work. Second, if you make various point types, you are loosing the advantage of seamless compatibility between them.

Then, if you want to call method from other class you can do it like this:

NameOfOtherClass.SomeMethod()

But you have to declare the SomeMethod in the other class as static...

public static double SomeMethod() { ... };

But then you can't use the method to access the data of your concrete points you have created in your code, so any data should be put into parameters.

If you want to do it your way, you have to just add a parameter to public double measureDistance() function so the function has access to another point to measure distance to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM