gpt-4-1106-preview· openai
Suzhen/CodeChat
Requirements: Write a program with a main method Create at least one new method which is…
- User
Requirements: Write a program with a main method Create at least one new method which is called from main that takes at least one parameter Call at least two distinct methods from the String class Utilize conditional statements or compound Boolean expressions Utilize iteration
- Assistant
Here's a sample Java program that meets all of the specified requirements:
public class StringManipulator { public static void main(String[] args) { String message = "Hello, World!"; // Call the custom method to reverse a string and check if it's a palindrome String reversedMessage = reverseString(message); System.out.println("Reversed Message: " + reversedMessage); // Call the custom method to check if the message contains a certain substring String toSearch = "World"; boolean containsSubstring = containsSubstring(message, toSearch); System.out.println("Does the message contain '" + toSearch + "'? " + containsSubstring); } // Method to reverse a string private static String reverseString(String input) { StringBuilder reversed = new StringBuilder(); // Utilizing iteration to reverse the string for (int i = input.length() - 1; i >= 0; i--) { reversed.append(input.charAt(i)); // Call to a String class method (charAt) } return reversed.toString(); } // Method to check if the original string contains a certain substring private static boolean containsSubstring(String original, String substring) { // Utilizing conditional statement and calling two String class methods (contains and isEmpty) if (!substring.isEmpty() && original.contains(substring)) { // Call to another String class method (contains) return true; } else { return false; } } }