Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code: import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.StdOu

ID: 3583003 • Letter: C

Question

Code:

import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdOut;

public class EuclideanEdge implements Comparable<EuclideanEdge> {
private Point2D v;
private Point2D w;

// Construct an Euclidean edge given the end points.
public EuclideanEdge(Point2D v, Point2D w) {
...
}
  
// Either endpoint of this edge.
public Point2D either() {
...
}

// Endpoint of this edge that is different from the given vertex.
public Point2D other(Point2D vertex) {
...
}
  
// Weight of this edge, ie, the Euclidean distance between the endpoints.
public double weight() {
...
}

// A string representation of this edge.
public String toString() {
return String.format("%s-%s %.5f", v, w, weight());
}

// Compare two edges by their weights.
public int compareTo(EuclideanEdge that) {
...
}

// Test client. [DO NOT EDIT]
public static void main(String[] args) {
Point2D w = new Point2D(0, 0);
Point2D x = new Point2D(1, 1);
Point2D y = new Point2D(1, 0);
Point2D z = new Point2D(0, 1);
EuclideanEdge e1 = new EuclideanEdge(w, x);
EuclideanEdge e2 = new EuclideanEdge(y, z);
StdOut.println(e1);
StdOut.println(e2);
StdOut.println(e1.compareTo(e2));
}
}

I have to replace the "...". Please help. Thank you!

We are using algs4.jar -> http://algs4.cs.princeton.edu/code/

Problem 3. (Euclidean Edge) Implement a comparable data type Euclidean that represents an edge in an undirected Edge graph and whose end points are points in the plane, represented as Point2D objects. The data type must support the following API method description construct an Euclidean edge given the end points Euclidean Edge (Point2D v, Point2D w) either endpoint of this edge Point2D either endpoint of this edge that is different from the given vertex Point2D other (Point2D vertex weight of this edge, ie, the Euclidean distance between the endpoints double weight a string representation of this edge String toString compare two edges by the weights int compareTo (EuclideanE that dge java Euclidean Edge (0.0, 0.0) (1.0, 1.0) 1.41421 (1.0, 0.0) (0.0, 1.0) 1.41421

Explanation / Answer

PROGRAM CODE:

EuclideanEdge.java

package euclidean;

import edu.princeton.cs.algs4.Point2D;

public class EuclideanEdge implements Comparable<EuclideanEdge> {
private Point2D v;
private Point2D w;
// Construct an Euclidean edge given the end points.
public EuclideanEdge(Point2D v, Point2D w) {
this.v = v;
this.w = w;
}
  
// Either endpoint of this edge.
public Point2D either() {
       return v;
  
}
// Endpoint of this edge that is different from the given vertex.
public Point2D other(Point2D vertex) {
   if(vertex.compareTo(v) == 0)
       return w;
   else
       return v;
  
}
  
// Weight of this edge, ie, the Euclidean distance between the endpoints.
public double weight() {
       return v.distanceTo(w);
  
}
// A string representation of this edge.
public String toString() {
return String.format("%s-%s %.5f", v, w, weight());
}
// Compare two edges by their weights.
public int compareTo(EuclideanEdge that) {
   double thisDistance= this.v.distanceTo(this.w);
   double thatDistance = that.v.distanceTo(that.w);
  
       return Double.compare(thisDistance, thatDistance);

}
}

Tester.java

package euclidean;

import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdOut;

public class Tester {

   /**
   * @param args
   */
   public static void main(String[] args) {
       Point2D w = new Point2D(0, 0);
Point2D x = new Point2D(1, 1);
Point2D y = new Point2D(1, 0);
Point2D z = new Point2D(0, 1);
EuclideanEdge e1 = new EuclideanEdge(w, x);
EuclideanEdge e2 = new EuclideanEdge(y, z);
StdOut.println(e1);
StdOut.println(e2);
StdOut.println(e1.compareTo(e2));

   }

}

OUTPUT:

(0.0, 0.0)-(1.0, 1.0) 1.41421
(1.0, 0.0)-(0.0, 1.0) 1.41421
0