NB:The following program keeps failing all the tests. Can someone help me check
ID: 3742652 • Letter: N
Question
NB:The following program keeps failing all the tests. Can someone help me check and fix program ? package week_0; import static input.InputUtils.stringInput; /** Finish the shout() method. It should convert a String to uppercase, and add "!!!" to the end. So if the user enters "hello", the method will be called with "hello". The method will turn "hello" into "HELLO!!!" */ public class Question_2_Shout { public static void main(String[] args) { // You don't need to modify any of the code in the main method String string = stringInput("Enter a string to be shouted "); String toShout = shout(string); System.out.println(toShout); } public static String shout(String shoutThis) { // TODO convert the shoutThis String to uppercase, and add "!!!" to the end. // For example, change "cat" to "CAT!!!" shoutThis = "hello"; String shoutThisInUpperCase = shoutThis.toUpperCase().concat("!!!"); System.out.println("The string to be shouted is: " +shoutThis); // TODO replace this line with your code. return shoutThisInUpperCase; } }
Explanation / Answer
Mistake: Reinitializing shoutThis in shout() method.
Note:
I removed print statement System.out.println("The string to be shouted is: " +shoutThis); in shout() method if required, you can add it back.
Modified Code:
package week_0;
import static input.InputUtils.stringInput;
/**
Finish the shout() method. It should convert a String to uppercase, and add "!!!" to the end.
So if the user enters "hello", the method will be called with "hello". The method will turn "hello" into "HELLO!!!"
*/
public class Question_2_Shout {
public static void main(String[] args) {
// You don't need to modify any of the code in the main method
String string = stringInput("Enter a string to be shouted ");
String toShout = shout(string);
System.out.println(toShout);
}
public static String shout(String shoutThis) {
// TODO convert the shoutThis String to uppercase, and add "!!!" to the end.
// For example, change "cat" to "CAT!!!"
String shoutThisInUpperCase = shoutThis.toUpperCase().concat("!!!");
// TODO replace this line with your code.
return shoutThisInUpperCase;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.