Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started