reset password
Author Message
Anonymous
Posts: 166
Posted 18:06 Jan 25, 2014 |

Is there an example for this question? I can't seem to figure out how to do it? Help thanks!

rabbott
Posts: 1649
Posted 18:19 Jan 25, 2014 |

This is R 4.8

R 4.8

What are the values of the following expressions? In each line, assume that

String s = "Hello";

String t = "World";

a. s.length() + t.length()

b. s.substring(1, 2)

c. s.substring(s.length() / 2, s.length())

d. s + t

e. t + s

This problem is asking you to perform operations on String objects and to report the result. For example, for          
          String s = "Hello"
s.length() is 5.

Part (a) is asking you to find the length of "Hello" and the length of "World" and add those two numbers together. Be careful with (d) and (e). In those parts "+" means String concatenation, not numerical addition.

If you aren't sure of your answer, try the examples on BlueJ and see if you get what you expect.

Last edited by rabbott at 18:33 Jan 25, 2014.
Anonymous
Posts: 166
Posted 18:26 Jan 25, 2014 |

Where did you get to s.length () to be 5?

Anonymous
Posts: 166
Posted 18:28 Jan 25, 2014 |

Is it because Hello is 5 letters?

Last edited by Anonymous at 18:29 Jan 25, 2014.
Anonymous
Posts: 166
Posted 18:29 Jan 25, 2014 |

So is t.length() also 5? since world is 5 letters?

rabbott
Posts: 1649
Posted 18:35 Jan 25, 2014 |

Yes, the length() function tells you the number of elements in the thing to which it is applied. In this case it is telling you the number of characters in a String. If you are not sure what it does. try it out in BlueJ and look it up in the Java API.

Last edited by rabbott at 18:37 Jan 25, 2014.
Anonymous
Posts: 166
Posted 18:51 Jan 25, 2014 |

for part b the answer should be e correct? since it starts by zero

rabbott
Posts: 1649
Posted 20:28 Jan 25, 2014 |

Correct.