Question 151
Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".
makeAbba("Hi", "Bye") → "HiByeByeHi"
makeAbba("Yo", "Alice") → "YoAliceAliceYo"
makeAbba("What", "Up") → "WhatUpUpWhat"
Complete the method makeAbba below:
public String makeAbba(String a, String b) {
}Main Solution
The method must concatenate the strings in the pattern a + b + b + a. Here is the complete solution:
public String makeAbba(String a, String b) {
return a + b + b + a;
}
Explanation:
- The pattern "abba" means: first string
a, then stringb, then stringbagain, then stringaagain. - Java's
+operator onStringobjects performs concatenation, soa + b + b + abuilds the desired result in one expression. - Verification with the given examples:
makeAbba("Hi", "Bye")→"Hi" + "Bye" + "Bye" + "Hi"="HiByeByeHi"✓makeAbba("Yo", "Alice")→"Yo" + "Alice" + "Alice" + "Yo"="YoAliceAliceYo"✓makeAbba("What", "Up")→"What" + "Up" + "Up" + "What"="WhatUpUpWhat"✓
Teacher Explanation
a + b + b + a, leveraging Java's string concatenation operator to build the "abba" pattern. No loops or conditionals are needed.Education Coach Advice
Traps
A common mistake is writing return a + b + a + b; (the "abab" pattern) instead of return a + b + b + a; (the "abba" pattern). Students may misread the problem description or confuse the order, producing the wrong arrangement of the two strings.

