Question
I'm currently working on a lab and for it, we are writing a class and two subclasses that calculate various measures of distance. One of
I'm currently working on a lab and for it, we are writing a class and two subclasses that calculate various measures of "distance." One of these is Euclidean, another one is Hamming, and the rest we went and made up!
We're given some starter code which tests the other classes:
Here's the starter code for the Distance class:
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(3, 0, 0, 4); String eucDistAns = "Euclidean distance between (3, 0) and (0, 4): 5.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("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)"); }
Code To Copy (Distance.java):
public class Distance {
protected String description; protected String distance; public Distance() { // TODO: Initialize the fields using the format shown in the Tests // class. } public Distance(int x) {
// TODO: Convert x to binary. See the Integer class documentation. String bits = ""; description = "Number of 1s in binary " + x + " (" + bits + ")";
// TODO: Calculate the number of 1s in the binary representation of x, // and assign the result to the distance field. } 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. } @Override public String toString() { return description + ": " + distance; } }
For me, the TODO: 's are too vague and I'm stuck as to where to begin. Thanks for any help or advice you can give me!
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(3, 0, 0, 4); String eucDistans = "Euclidean distance between (3, 0) and (0, 4): 5.0"; test(eucDist, eucDistans, "Euclidean(int, int, int, int)"); // Note that a Hamming object can be assigned to a Distance variable. Distance str HammDist = new Hamming("cat", "sat"); String strHammDistans = "Hamming distance between cat and sat: 1"; test(str HammDist, str HammDistAns, "Hamming (String, String)"); Distance intHammDist = new Hamming (4, 5); String intHammDistans = "Hamming distance between 100 and 101: 1"; test(intHammDist, intHammDistans, "Hamming(int, int)"); public class Distance { protected string description; protected string distance; public Distance() { // TODO: Initialize the fields using the format shown in the Tests // class. public Distance(int x) { // TODO: Convert x to binary. See the Integer class documentation. String bits = "; description = "Number of 1s in binary " + x + " (" + bits + ")"; // TODO: Calculate the number of is in the binary representation of x, // and assign the result to the distance field. public Distance(Strings) { 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. @Override public string tostring() { return description + ": " + distance; 35 }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