Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to develop a class that calculates euclidean distance in Java. However I'm not getting the desired output and would like help on the

I'm trying to develop a class that calculates euclidean distance in Java. However I'm not getting the desired output and would like help on the formula!

Code to Copy (Tests.java):

public class Tests { public static void main(String[] args) { Distance emptyDist = new Distance(); String emptyDistAns = "No argument: 0"; test(emptyDist, emptyDistAns, "Distance()"); Distance intDist = new Distance(5); String intDistAns = "Number of 1s in binary 5 (101): 2"; test(intDist, intDistAns, "Distance(int)"); Distance strDist = new Distance("Hi!"); String strDistAns = "Sum of chars in \"Hi!\": 210"; test(strDist, strDistAns, "Distance(String)"); // Note that a Euclidean object can be assigned to a Distance variable. Distance eucDist = new Euclidean(-12, 0, 0, -5); String eucDistAns = "Euclidean distance between (-12, 0) and (0, -5): 13.0"; test(eucDist, eucDistAns, "Euclidean(int, int, int, int)");

}

}

}

Code to Copy(Distance.Java) [Euclidean inherits from this class]:

public class Distance {

protected String description; protected String distance; public Distance() { // TODO: Initialize the fields using the format shown in the Tests // class. description = "No argument"; distance = "0"; } public Distance(int x) { // TODO: Convert x to binary. See the Integer class documentation. String binary = Integer.toBinaryString(x); description = "Number of 1s in binary " + x + " (" + binary + ")";

// TODO: Calculate the number of 1s in the binary representation of x, // and assign the result to the distance field. int counter = 0; for (int i = 0; i < binary.length(); i++) { if(binary.charAt(i) == '1') { counter++; } distance = counter+""; } } public Distance(String s) { description = "Sum of chars in \"" + s + "\"";

// TODO: Calculate the sum of the ASCII codes of the characters in the // given String. Assign the sum to the distance field. int sum = 0; for(int i = 0; i < s.length(); i++) { sum += s.charAt(i); } distance = sum+""; }

@Override public String toString() { return description + ": " + distance; } }

Code to Copy(Euclidean.java):

public class Euclidean extends Distance { public Euclidean(int x1, int y1, int x2, int y2) { // TODO: Initialize the description field using the format shown in the // Tests class. description = "Euclidean distance between " + "(" + x1 + ", " + y1 + ")" + " and " + "(" + x2 + ", " + y2 + ")"; distance = "0"; // TODO: Calculate the Euclidean distance of the two points and assign // it to the distance field. int p = Math.abs(x2-x1); int q = Math.abs(y2-y1); double sum = Math.round((Math.sqrt((p)*(q) + (p)*(q)))); distance = Double.toString(sum); } }

OUTPUT:

Incorrect Euclidean(int, int, int, int) Expected: Euclidean distance between (-12, 0) and (0, -5): 13.0 Returned: Euclidean distance between (-12, 0) and (0, -5): 11.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions