Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Main Code: import java.util.Scanner; public class PalindromeTest { public static

ID: 671766 • Letter: M

Question

Main Code:

import java.util.Scanner;
public class PalindromeTest
{
public static void main(String[] args)
{
int used = 0;
char[] chars = new char[80];
Scanner inputWord = new Scanner(System.in);
Scanner reply = new Scanner(System.in);
System.out.println("Enter a string characters, terminated by a period.");

String data;
String cq;
Palindrome_class word;

do
{
//input word from user.
data = inputWord.nextLine();
word = new Palindrome_class(data);

//check for palindrome.
if(word.isPalindrome(chars, used))
System.out.println(data + " is a palindrome.");

else
System.out.println(data + " is not a palindrome.");

//request to continue or quit.
System.out.println("Continue or Quit?");
cq = reply.nextLine();
}
while (cq.equalsIgnoreCase("continue"));

System.exit(0);
}
}

Class Code:

public class Palindrome_class
{
// instance variable
char[] characterArray;

//constructor
//@param data is a string of characters
public Palindrome_class(String data)
{
characterArray = data.toUpperCase().toCharArray();
}

//@return true if the word is a palindrome, otherwise returns false.
public boolean isPalindrome(char[] a, int used)
{
int i = 0, j = used - 1;

while (i < j)
{
if(characterArray[i] == characterArray[j])
{
i++;
j--;
}
else
{
return false;
}
}
return true;
}
}

salindrome is a word or phrase that reads the same forward backward, ignoring blanks and considering uppercase and versions letter of the same to be equalthe and lowercase palindromes: For example, following are warts n straw a radar • Able was Iere I saw Elba • Xyzczyx write a program that will accept a sequence of characters terminated period and will decide whether the string without the period is a a by palindrome. You may assume that the input contains only and letters blanks and is at most 80 characters long, Include a loop that allows the user to check additional strings until she or he requests that the program end. (Hint: Define a static method called isPalindrome that begins as follows /* * Precondition: The array a contains letters and blanks in positions alo ] through a[used - 1). Returns true if the string is a palindrome and false otherwise, public static boo lean isPalindrome (eharu a int used)

Explanation / Answer

PalindromeTest.class

import java.util.Scanner;

public class PalindromeTest

{

public static void main(String[] args)

{

int used = 0;

char[] chars = new char[80];

Scanner inputWord = new Scanner(System.in);

Scanner reply = new Scanner(System.in);

String data;

String cq = null;

Palindrome_class word;

do

{

System.out.println("Enter a string characters, terminated by a period.");

// input word from user.

data = inputWord.nextLine();

if ('.' == data.charAt(data.length() - 1))

{

data = data.substring(0, data.indexOf("."));

word = new Palindrome_class(data);

if (word.isPalindrome(chars, used))

{

System.out.println(data + " is a palindrome.");

}

else

{

System.out.println(data + " is not a palindrome.");

}

}

else

{

System.out.println("Please enter a string ,that must be terminated by a period(Example: pavan.,liril.,etc...).");

}

// request to continue or quit.

System.out.println("Continue or Quit?");

cq = reply.nextLine();

}

while (cq.equalsIgnoreCase("continue"));

System.exit(0);

}

}

Palindrome_class.class:

public class Palindrome_class {

// instance variable

char[] characterArray;

// constructor

// @param data is a string of characters

public Palindrome_class(String data) {

characterArray = data.toUpperCase().toCharArray();

}

// @return true if the word is a palindrome, otherwise returns false.

public boolean isPalindrome(char[] a, int used) {

a = characterArray;

int i1 = 0;

int i2 = a.length - 1;

while (i2 > i1) {

if (a[i1] != a[i2]) {

return false;

}

++i1;

--i2;

}

return true;

}

}

Output:

Enter a string characters, terminated by a period.

liril.

liril is a palindrome.

Continue or Quit?

continue

Enter a string characters, terminated by a period.

pavan.

pavan is not a palindrome.

Continue or Quit?

continue

Enter a string characters, terminated by a period.

liril

Please enter a string ,that must be terminated by a period(Example: pavan.,liril.,etc...).

Continue or Quit?

continue

Enter a string characters, terminated by a period.

malayalam

Please enter a string ,that must be terminated by a period(Example: pavan.,liril.,etc...).

Continue or Quit?

continue

Enter a string characters, terminated by a period.

malayalam.

malayalam is a palindrome.

Continue or Quit?

quit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote