StringMethod.java /** * Compares string1 and string2 and returns a comma-delimited concatenated * string consi

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

StringMethod.java /** * Compares string1 and string2 and returns a comma-delimited concatenated * string consi

Post by answerhappygod »

StringMethod.java
/**
* Compares string1 and string2 and returns a
comma-delimited concatenated
* string consisting of the string that is first
lexically followed by the
* string that is second lexically. If the
strings are equal, then only string1
* is returned.
*
* @param string1 String to compare against
string2.
* @param string2 String to compare against
string1.
* @return A concatenation of string1 and
string2 in order.
*/
public static String pairs(String string1, String
string2) {
String line = null;
// enter code here
return line;
}
/**
* Finds the distance between the s1 and s2. The
distance between two strings of
* the same length is the number of positions in
the strings at which their
* characters are different. If two strings are
not the same length, the
* distance is unknown (-1). If the distance is
zero, then two strings are
* equal.
*
* @param string1 String to compare against
string2.
* @param string2 String to compare against
string1.
* @return The distance between string1 and
string2.
*/
public static int stringDistance(String string1,
String string2) {
int distance = 0;
// enter code here
return distance;
}
}
Main.java
public class Main {
// Constants
private static final String LINE =
"-".repeat(40);
private static final String TEST_LINE =
"=".repeat(80);
/**
* @param args Unused.
*/
public static void main(String[] args) {
System.out.println("Strings Lab Methods
Tests");
System.out.println();
System.out.println("Tests are of the
form:");
System.out.println(" Test Operation\n
{expected value}: actual value");
System.out.println();
testPairs();
testStringDistance();
System.out.println(TEST_LINE);
System.out.println("Done");
}
private static void testPairs() {
System.out.println(TEST_LINE);
System.out.println("Testing pairs");
System.out.println(LINE);
String s1 = "a";
String s2 = "b";
String expected = "a,b";
String actual = StringMethods.pairs(s1,
s2);
System.out.println("pairs(\"" + s1 + "\", \"" +
s2 + "\")\n {" + expected + "}: " + actual);
s1 = "b";
s2 = "a";
expected = "a,b";
actual = StringMethods.pairs(s1, s2);
System.out.println("pairs(\"" + s1 + "\", \"" +
s2 + "\")\n {" + expected + "}: " + actual);
s1 = "line";
s2 = "line";
expected = "line";
actual = StringMethods.pairs(s1, s2);
System.out.println("pairs(\"" + s1 + "\", \"" +
s2 + "\")\n {" + expected + "}: " + actual);
System.out.println();
}
private static void testStringDistance() {
System.out.println(TEST_LINE);
System.out.println("Testing
stringDistance");
System.out.println(LINE);
String s1 = "a";
String s2 = "a";
int expected = 0;
int actual = StringMethods.stringDistance(s1,
s2);
System.out.println("stringDistance(\"" + s1 +
"\", \"" + s2 + "\")\n {" + expected + "}: " + actual);
s1 = "a";
s2 = "b";
expected = 1;
actual = StringMethods.stringDistance(s1,
s2);
System.out.println("stringDistance(\"" + s1 +
"\", \"" + s2 + "\")\n {" + expected + "}: " + actual);
s1 = "North";
s2 = "South";
expected = 2;
actual = StringMethods.stringDistance(s1,
s2);
System.out.println("stringDistance(\"" + s1 +
"\", \"" + s2 + "\")\n {" + expected + "}: " + actual);
s1 = "short";
s2 = "longer";
expected = -1;
actual = StringMethods.stringDistance(s1,
s2);
System.out.println("stringDistance(\"" + s1 +
"\", \"" + s2 + "\")\n {" + expected + "}: " + actual);
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply