Question

Calculates the distance between two points of N dimensional space. If the two points are in...

Calculates the distance between two points of N dimensional space.

If the two points are in different dimensions, print the distance as -1.

Use Euclid Distance and Manhattan Distance.

Thanks!

**Use the code below and complete the rest.**

public class Distance2 {
public static void main(String[] args) {
Point p1 = new Point(new double[] {1.0, 2.0, 3.0});
Point p2 = new Point(new double[] {4.0, 5.0, 6.0});
System.out.println("Euclidean Distance: " + EuclidDistance.getDist(p1, p2));
System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p1, p2));
Point p3 = new Point(new double[] {1.0, 2.0, 3.0});
Point p4 = new Point(new double[] {4.0, 5.0});
System.out.println("Euclidean Distance: " + EuclidDistance.getDist(p3, p4));
System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p3, p4));
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a new class Point.java and copy the following code

class Point
{
    private double []coords;

    Point ( double[] coords)
    {
        this.coords = coords;
    }

    double[] getCoords()
    {
        return this.coords;
    }
}

Create a new class EuclidDistance.java and copy the following code

class EuclidDistance
{
   static double getDist(Point pt1, Point pt2)
   {
     if( pt1.getCoords().length == pt2.getCoords().length)
     {
       double sum =0;
       for (int i= 0 ;i< pt1.getCoords().length;i++)
         sum = sum + Math.pow((pt2.getCoords()[i] - pt1.getCoords()[i]), 2);
            return Math.pow(sum,0.5);
     }
     else
       return -1;
   }
}

Create a new class ManhattanDistance.java and copy the following code

class ManhattanDistance
{
    static double getDist(Point pt1, Point pt2)
    {
        if( pt1.getCoords().length == pt2.getCoords().length)
        {
            double sum =0;
            for (int i= 0 ;i< pt1.getCoords().length;i++)
              sum = sum + Math.abs(pt2.getCoords()[i] - pt1.getCoords()[i]);
            return sum;
        }
        else
            return -1;
    }
}
Add a comment
Know the answer?
Add Answer to:
Calculates the distance between two points of N dimensional space. If the two points are in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. The below table shows four different processors P1, P2, P3, and P4 executing the same...

    4. The below table shows four different processors P1, P2, P3, and P4 executing the same program with clock rates and average CPls as shown below. Answer the following showing all the steps. Processor Clock rate CP 1.0 GHz 4.0 GHz 3.0 GHz 2.0 GHz P1 3.0 P2 2.0 P3 1.5 P4 2.5 a. Which processor has the best performance in terms of execution time? (8 Points) b. If the program has 5000 instructions, what is the time spent on...

  • Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

    Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...

  • Q.1 Chloroform (1) and n-heptane (2) forms a 2 phase systems for which the following plot...

    Q.1 Chloroform (1) and n-heptane (2) forms a 2 phase systems for which the following plot is givern: 6.0 5.5 5.0 4.5 4.0 3.5 3.0 2.5 2.0 1.5 1.0 0.5 0.0 6.0 5.5 5.0 4.5 4.0 3.5 2.5- 2.0- 1.5 1.0 0.5 0.0 0.0 0.2 0.4 0.6 0.8 1.0 (a) Use Van Laar equation to calculate y?, y2 and P for x1-02. Given P1 at-44.51 kPa; P2 at-65.54kPa (b) Do the activity coefficients show positive or negative deviation from activity...

  • Q1. Consider these four points: P [,,5| , P2 = 2], P3 = [H]. Plot these...

    Q1. Consider these four points: P [,,5| , P2 = 2], P3 = [H]. Plot these three points. (a) Find the Manhattan distance between Pi and P2 (b) Find the Manhattan distance between P1 and P3. (e) Find the Manhattan distance between P2 and P3. Q2. Consider the same points in Q1 and find the Euclidean distances between the points specified in parts (a), (b), and (e). In other words, you will be doing the above question again but now...

  • Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic...

    Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...

  • INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the...

    INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the API Documentation is the code I submitted. However, the output is different for what they are asking. I am looking for someone to fix the code to print out the correct output and to add comments so I can have an idea in how the code works. PLEASE AND THANK YOU. API DOCUMENTATION: public class Point extends java.lang.Object The Point class is used to...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • Figure 8-3 The vertical distance between points A and C represents a tax in the market....

    Figure 8-3 The vertical distance between points A and C represents a tax in the market. ​ Refer to Figure 8-3. The price that buyers effectively pay after the tax is imposed is Group of answer choices P3. P1. P4. P2. We were unable to transcribe this imagehome / study / business/ economics / economics questions and answers/ figure 8-3 the w Question: Figure 8-3 The vertical distance beti Figure 8-3 The vertical distance between points A and represents a...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT