How do I create two files and two classes that one file has all the methods and
ID: 3669154 • Letter: H
Question
How do I create two files and two classes that one file has all the methods and other file just ask for the string input? I need to use a CONSTRUCTOR.
import java.util.Scanner;
class PalindromeDitactor {
public static boolean Palindrome(String input) {
if(input.length()==1 || input.charAt(0)==input.charAt(1)) {
return true; }
if(input.charAt(0)==input.charAt(input.length()-1)) {
return Palindrome(input.substring(1,input.length()-1)); }
return false;
}
public static void main (String[]args) {
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a string: ");
String userString = kb.nextLine();
if(Palindrome(userString))
System.out.println("It is a palindrome.");
else
System.out.println("It isn't a palindrome."); } }
Explanation / Answer
Solution: using constructor you can do like follows:
package com.nancy.chegg;
import java.util.Scanner;
public class PalindromeDitactor extends Util {
static boolean res = false;
PalindromeDitactor(String input){
res = fun(input);
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a string: ");
String userString = kb.nextLine();
new PalindromeDitactor(userString);
if (res)
System.out.println("It is a palindrome.");
else
System.out.println("It isn't a palindrome.");
}
}
______________________________________
package com.nancy.chegg;
public class Util {
public boolean fun(String input){
if (input.length() == 1 || input.charAt(0) == input.charAt(1)) {
return true;
}
if (input.charAt(0) == input.charAt(input.length() - 1)) {
return fun(input.substring(1, input.length() - 1));
}
return false;
}
}
__________________________________________________________
Sample run;
Please enter a string: nanan
It is a palindrome.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.