Question
JAVA I'm developing a class that calculates Hemming distance. We are given starter code but I'm running into some roadblocks. Any advice or tips you
JAVA
I'm developing a class that calculates Hemming distance. We are given starter code but I'm running into some roadblocks. Any advice or tips you can also give in regards to coding in general is much appreciated as well
Code to copy for our testing:
public class Workbench{ public static void main(String[] args) {
Distance strHammDist = new Hamming("cat", "sat"); String strHammDistAns = "Hamming distance between cat and sat: 1"; 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)"); }
}
Our starter code to copy with my failed code included:
public class Hamming { 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"); } try { int counter = 0; for(int i = s1.length(); i <= s1.length(); i--) { for(int j = s2.length(); j <= s2.length(); j--) { if (s1.charAt(i) != s2.charAt(j)) counter++; } } distance = Integer.toString(counter); } catch (Exception e) { System.out.println("For loop error"); } } public Hamming(int x, int y) {
// TODO: Convert x and y to binary. // TODO: Initialize the description field using the format shown in the // Tests class. // TODO: Calculate the Hamming distance between the binary // representations of x and y. Use the rules given above for bit // Strings of unequal length. } }
Terminal:
For loop error Incorrect Hamming(int, int) Expected: Hamming distance between 100 and 101: 1 Returned: No argument: 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started