Chapter 3, Exercise 6, Thinking in Scala
I made the output from this one a bit more verbose.
Hope Radio doesn't mess up the quotings and the backslashes.
object c3x6 {
def show1 ( ns: String, s: String ) : String =
ns + " (is \"" + s + "\")";
def show ( p: String, q: String, opName: String, res: Boolean ) = {
Console.print( show1( "p", p ) );
Console.print( " " + opName + " " );
Console.print( show1( "q", q ) );
Console.println( "? " + res );
}
def testStringComp ( x: String, y: String ) = {
show( x, y, "==", x == y );
show( x, y, "!=", x != y );
show( x, y, ".equals()", x.equals( y ) );
// show( x, y, "<", x < y );
}
def main ( args: Array[String] ) = {
testStringComp( "a", "b" );
testStringComp( "a", "a" );
testStringComp( "123456", "123" + "456" );
}
}
I tried a less than, but these strings are
java.lang.Strings, so they don't have a less-than method.
Here's the output that I got:
p (is "a") == q (is "b")? false
p (is "a") != q (is "b")? true
p (is "a") .equals() q (is "b")? false
p (is "a") == q (is "a")? true
p (is "a") != q (is "a")? false
p (is "a") .equals() q (is "a")? true
p (is "123456") == q (is "123456")? true
p (is "123456") != q (is "123456")? false
p (is "123456") .equals() q (is "123456")? true
11:41:56 PM