6 THE NTWICE PROBLEM Complete the method nTwice. The method has two inputs: a String str and an int n. The output of the method is a new String made of the first and last n chars from str. The original String length will be at least length n. Here is some sample input and output: nTwice("Hello", 2) → "Helo" nTwice("Chocolate", 3) → "Choate"

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 12RQ
icon
Related questions
Question

Complete the method nTwice. The method has two inputs: a String str and an int n. The output of the method is a new String made of the first and last n chars from str. The original String length will be at least length n. Here is some sample input and output:

 

### 6 The NTwice Problem

Complete the method **nTwice**. The method has two inputs: a **String str** and an **int n**. The output of the method is a new **String** made of the first and last **n** characters from **str**. The original **String** length will be at least length **n**. Here is some sample input and output:

* **nTwice("Hello", 2)** → "Helol"
  - The first 2 characters of "Hello" are "He".
  - The last 2 characters of "Hello" are "lo".
  - The result is "Helo".

* **nTwice("Chocolate", 3)** → "Choate"
  - The first 3 characters of "Chocolate" are "Cho".
  - The last 3 characters of "Chocolate" are "ate".
  - The result is "Choate".

* **nTwice("Chocolate", 1)** → "Ce"
  - The first character of "Chocolate" is "C".
  - The last character of "Chocolate" is "e".
  - The result is "Ce".

In each example, the method **nTwice** combines the specified number of characters from the start and end of the provided string to create the new string.
Transcribed Image Text:### 6 The NTwice Problem Complete the method **nTwice**. The method has two inputs: a **String str** and an **int n**. The output of the method is a new **String** made of the first and last **n** characters from **str**. The original **String** length will be at least length **n**. Here is some sample input and output: * **nTwice("Hello", 2)** → "Helol" - The first 2 characters of "Hello" are "He". - The last 2 characters of "Hello" are "lo". - The result is "Helo". * **nTwice("Chocolate", 3)** → "Choate" - The first 3 characters of "Chocolate" are "Cho". - The last 3 characters of "Chocolate" are "ate". - The result is "Choate". * **nTwice("Chocolate", 1)** → "Ce" - The first character of "Chocolate" is "C". - The last character of "Chocolate" is "e". - The result is "Ce". In each example, the method **nTwice** combines the specified number of characters from the start and end of the provided string to create the new string.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Methods of StringBuilder class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,