Question

Decisions in Java – Comparing Strings WS 1. For each of the following pairs of strings,...

Decisions in Java – Comparing Strings WS

1. For each of the following pairs of strings, state, with reasons, which string precedes the other.

(a) "cat" and "dog" (b) "cat" and "Cathy" (c) "X " and " X"

(d) "cab" and "car" (e) "XX" and "X X" (f) "XY" and "XY " (g) "375" and "84" (h) "" and " "

2. State the value of each expression.

(a) "one".equals("one ")

(b) (b) "two".equals("2")

(c) "Three".equals("Three")

(d) "four".equals("for")

3. State whether the value of the expression is negative, zero, or positive.

(a) "for".compareTo("fore")

(b) "fore".compareTo("force") (c) "force".compareTo("Force") (d) "Force".compareTo("Farce") (e) "Farce".compareTo("Fare") (f) "Fare".compareTo("far")

(g) "far".compareTo("far")

(h) "far".compareTo("far ")

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. use compareTo() method

(a) "cat" and "dog" ----- cat, c is smaller than d

(b) "cat" and "Cathy" ---- Cathy, C is smaller than c

(c) "X " and " X" --- " X", space is smaller than X

(d) "cab" and "car" ---- cab, b is smaller than r

(e) "XX" and "X X" ---"X X"

(f) "XY" and "XY " --- "XY"

(g) "375" and "84" ---- "375", 3 is smaller than 8

(h) "" and " " ------ ""

2. State the value of each expression.

(a) "one".equals("one ") ------- false, difference of space ()

(b) (b) "two".equals("2") ---------- false

(c) "Three".equals("Three")-------- true, all characters in the strings are same

(d) "four".equals("for")------------ false

3.

(a) "for".compareTo("fore")---- -1, small and larger string

(b) "fore".compareTo("force") --- 2 , difference between c and e

(c) "force".compareTo("Force") ---- 32, difference between F and f

(d) "Force".compareTo("Farce") ---- 14, difference between o and a

(e) "Farce".compareTo("Fare") --- -2, difference between c and e

(f) "Fare".compareTo("far") --- -32, difference between F and f

(g) "far".compareTo("far") ----- 0 , both are equal

(h) "far".compareTo("far ") ----- -1, small and larger string

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Decisions in Java – Comparing Strings WS 1. For each of the following pairs of strings,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT