// Exercise 3.2 (3 and 4). (from the Carter book)
// (3) State whether the value of
// the expression is negative, zero, or positive.
// (4) Suppose that s1 and s2 are variables of type String.
// Write an expression of the form s1.compareTo(s2) <op> 0
// that will have the value true if, and only if, the condition
// is true. State why the condition is true.
public class SelectionStringExercises2
{
public static void main (String[] args)
{
// Exercise 3.2 (3):
// Part (3a): why is the result -1?
System.out.println ("for".compareTo ("fore"));
// Part (3b): why is the result +2?
System.out.println ("fore".compareTo ("force"));
// Part (3c): why is the result +32?
System.out.println ("force".compareTo ("Force"));
// Part (3d): why is the result +14?
System.out.println ("Force".compareTo ("Farce"));
// Part (3e): why is the result -2?
System.out.println ("Farce".compareTo ("Fare"));
// Part (3f): why is the result -32?
System.out.println ("Fare".compareTo ("far"));
// Part (3g): why is the result 0?
System.out.println ("far".compareTo ("far"));
// Part (3h): why is the result -1?
System.out.println ("far".compareTo ("far "));
// Exercise 3.2 (4):
String s1, s2;
s1 = "Brent";
s2 = "James";
// Part (4a): true / false? (s1 equals s2?)
System.out.println (s1.compareTo (s2) == 0);
// Part (4b): true / false? (s1 preceeds s2?)
System.out.println (s1.compareTo (s2) < 0);
// Part (4c): true / false? (s1 equals or follows s2?)
System.out.println (s1.compareTo (s2) >= 0);
// Part (4d): true / false? (s1 does not follow s2?)
System.out.println (s1.compareTo (s2) <= 0);
} // main method
} // SelectionStringExercises2 class
No comments:
Post a Comment