Note that there are some explanatory texts on larger screens.

plurals
  1. POCall a method from another class(not main class)
    primarykey
    data
    text
    <p>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...</p> <p><strong>---------------------Point.java---------------------------------</strong></p> <pre><code>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))); } } </code></pre> <p><strong>---------------------ClonePoint.java---------------------------------</strong></p> <pre><code>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) } } </code></pre> <p><strong>----------------------PointDriver.java-----------------------------</strong></p> <pre><code>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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload