Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to convert Dart program into Java
    primarykey
    data
    text
    <p>I've been having a few issues trying to convert the Point program on the Dart website into Java. The Dart program is displayed below: </p> <pre><code>class Point { num x, y; Point(num this.x, num this.y); Point scale(num factor) =&gt; new Point(x*factor, y*factor); num distance() =&gt; Math.sqrt(x*x + y*y); } void main() { Point a = new Point(2,3).scale(10); print(a.distance()); } </code></pre> <p>Here's what I've come up with so far: </p> <pre><code>public class PointJava { int x, y; public PointJava(int x, int y){ int a = x; int b = y; } public PointJava scale(int factor){ PointJava j = new PointJava(factor*x, factor*y); return j; } public double distance(){ double result = Math.sqrt(x*x+y*y); return result; } public static void main(String[] args) { PointJava a = new PointJava(2, 3).scale(10); System.out.println(a.distance()); } } </code></pre> <p>It outputs the double type number <code>0.0</code>, but the Point program in Dart outputs <code>36.05551275463989</code>.</p> <p>The main issue is trying to convert these statements to Java:</p> <pre><code>Point scale(num factor) =&gt; new Point(x*factor, y*factor); num distance() =&gt; Math.sqrt(x*x + y*y); </code></pre> <p>I've seen this type of syntax in C++, at least what little I've actually studied of it (I stopped when I was introduced to pointers and references). However, I asked someone else, and they told me these statements were actually used to define functions. I'm here to see if this person's correct and how to understand what these statements mean in terms of Java.</p> <p>Any aid will help. Thanks.</p>
    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