How do you properly notate this so it forms a JAVADOC package palindrome; import
ID: 3918049 • Letter: H
Question
How do you properly notate this so it forms a JAVADOC
package palindrome;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Owner
*/
public class Palindrome {
static boolean recurse(String s, int i, int n) {
if (i == n / 2) {
return s.charAt(i) == (s.charAt(n / 2));
}
if (s.charAt(i) == (s.charAt(n - i - 1))) {
return recurse(s, i + 1, n);
}
return false;
}
public static void main(String args[]) throws FileNotFoundException {
Scanner scan;
try (Scanner sc = new Scanner(new File("input.txt"))) {
List<String> lines = new ArrayList<>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
} for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines.get(i), 0, lines.get(i).length()) == true) {
System.out.println(lines.get(i) + " is a palindrome");
} else {
System.out.println(lines.get(i) + " is not a palindrome");
}
} System.out.println("Enter 5 Strings: ");
scan = new Scanner(System.in);
List<String> lines2 = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
lines2.add(scan.next());
} for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines2.get(i), 0, lines2.get(i).length()) == true) {
System.out.println(lines2.get(i) + " is a palindrome");
} else {
System.out.println(lines2.get(i) + " is not a palindrome");
}
}
}
scan.close();
}
}
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*Represents a class which checks if file contents
*or input from keyboard is palindrome or not
* @author Owner
*/
public class Palindrome {
/*
* this is recursive function
* which checks recursivel whether a string is palindrome or not
* @param s String which needs to be check wheter that is palindrome
* @param i position of character currently checking
* @param n length of the string s
* @return whether string is palindrome or not
*/
static boolean recurse(String s, int i, int n) {
if (i == n / 2) {
return s.charAt(i) == (s.charAt(n / 2));
}
if (s.charAt(i) == (s.charAt(n - i - 1))) {
return recurse(s, i + 1, n);
}
return false;
}
/*
* this opens file input.txt and for each line checks for palindrome
* and
* gets input from user to check for palindrome
* @param args command line arguments
*/
public static void main(String args[]) throws FileNotFoundException {
Scanner scan;
try (Scanner sc = new Scanner(new File("input.txt"))) {
List<String> lines = new ArrayList<>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
} for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines.get(i), 0, lines.get(i).length()) == true) {
System.out.println(lines.get(i) + " is a palindrome");
} else {
System.out.println(lines.get(i) + " is not a palindrome");
}
} System.out.println("Enter 5 Strings: ");
scan = new Scanner(System.in);
List<String> lines2 = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
lines2.add(scan.next());
} for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines2.get(i), 0, lines2.get(i).length()) == true) {
System.out.println(lines2.get(i) + " is a palindrome");
} else {
System.out.println(lines2.get(i) + " is not a palindrome");
}
}
}
scan.close();
}
}
// use @return and @param to generate javaDoc
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.