Question: To introduce another few methods and practice with them, let's write one more method. Sometimes, when printing an inventory, we might have long titles (
To introduce another few
methods and practice with them, let's write one more method. Sometimes, when printing an inventory, we
might have long titles like the full title of "Make It Stick" that don't fit in a column widthwise. A common trick is to truncate the string
down to a certain length, and replace the end with If the string is below the given length, we leave it alone. So let's
write truncateTitle which will take in a length to cut the title down to and return the truncated title.
class Book
@param length The number of characters to appear before
@return A new string containing length characters followed by if the
string was too long, or the original string otherwise
String truncateTitleint length
class ExamplesBook
Book schemer new BookThe Little Schemer", "Daniel P Friedman", ;
Book stick new BookMake It Stick: The Science of Successful Learning", "Peter C Brown", ;
String truncate this.stick.truncateTitle;
String truncate this.schemer.truncateTitle;
String truncate this.schemer. truncateTitle;
Do Now! Determine the expected output for the following calls to
Fill in the blanks
String truncate this.stick.truncateTitle; should be
String truncate this.schemer.truncateTitle; should be
String truncate this.schemer.truncateTitle; should be
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
