Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hamming Distance in Java. I'm still trying to calculate the hamming distance between two strings. The program works, granted they're of equal length but the

Hamming Distance in Java.

I'm still trying to calculate the hamming distance between two strings. The program works, granted they're of equal length but the problem arises when the two strings are of unequal length. I just need some help coming up with a solution to compare strings with unequal lenghts. I'll provide everything you may need below, and please let me know if there's any other information you may need.

CODE TO COPY(Hamming.java):

public class Hamming extends Distance { public Hamming(String s1, String s2) {

// TODO: Initialize the description field using the format shown in the // Tests class. description = "Hamming distance between " + s1 + " and " + s2; distance = "1"; // TODO: Calculate the Hamming distance between two Strings. If the // Strings have different lengths, add 1 for every extra character of // the longer String. Compare the Strings starting with the rightmost // character. Assign the result to the distance field. int comparison = 0; try { if (s1.length() != s2.length()) { if (s1.length() > s2.length()) { comparison = s1.length() - s2.length(); } else if (s1.length() < s2.length()) { comparison = s2.length() - s1.length(); } } }catch (Exception e) { System.out.println("String comparison error"); } //distance int counter = 0; try { for(int i = 0; i < s1.length(); i++) { if (s1.charAt(i) != s2.charAt(i)) counter++; } distance = Integer.toString(counter); } catch (Exception e) { for (int i = 0; i < s2.length(); i++){ if (s1.charAt(i) != s2.charAt(i) && s2.charAt(i) == ' ') { counter++; } System.out.println("s1_elem" + i + " " + s1.charAt(i)); System.out.println("s2_elem" + i + " " + s2.charAt(i)); distance = Integer.toString(counter); } } }

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)"); // Note that a Hamming object can be assigned to a Distance variable. Distance strHammDist = new Hamming("hippopotamus", "potamus"); String strHammDistAns = "Hamming distance between hippopotamus and potamus: 5"; test(strHammDist, strHammDistAns, "Hamming(String, String)"); Distance intHammDist = new Hamming(4, 5); String intHammDistAns = "Hamming distance between 100 and 101: 1"; test(intHammDist, intHammDistAns, "Hamming(int, int)"); } /** * A helper method for main that tests the toString output of a Distance * object. The method prints an error message if the output is incorrect. * @param obj a Distance object. * @param ans the correct toString output. * @param testName a description of the constructor used to create obj. */ public static void test(Distance obj, String ans, String testName) { if (!obj.toString().equals(ans)) { System.out.println("Incorrect " + testName); System.out.println("Expected: " + ans); System.out.println("Returned: " + obj.toString()); System.out.println(); } } }

Here's my distance class to help out with the inherited fields:

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; } }

TERMINAL OUTPUT:

Incorrect Hamming(String, String) Expected: Hamming distance between hippopotamus and potamus: 5 Returned: Hamming distance between hippopotamus and potamus: 7

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

Students also viewed these Databases questions

Question

Describe the Indian constitution and political system.

Answered: 1 week ago

Question

Explain in detail the developing and developed economy of India

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = X 52+7 - 1)(x+2) dx

Answered: 1 week ago

Question

What is gravity?

Answered: 1 week ago

Question

What is the Big Bang Theory?

Answered: 1 week ago