0% found this document useful (0 votes)
29 views

Import Java

This Java program defines a Palindrom class to check if a string is a palindrome. The Palindrom class contains methods to set and get the string, as well as a helper method to check if the string is a palindrome by comparing the first and last characters and incrementally moving inward. The main method prompts the user for input, creates a Palindrom object, calls the helper method on the input, and prints whether it is a palindrome.

Uploaded by

lhei_cadatal2610
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Import Java

This Java program defines a Palindrom class to check if a string is a palindrome. The Palindrom class contains methods to set and get the string, as well as a helper method to check if the string is a palindrome by comparing the first and last characters and incrementally moving inward. The main method prompts the user for input, creates a Palindrom object, calls the helper method on the input, and prints whether it is a palindrome.

Uploaded by

lhei_cadatal2610
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Palindrom {


String pal;
// constructor

public Palindrom(){
this.pal="";
}

public Palindrom(String str){


this.pal=str;
}

public boolean helper(){


int len=this.pal.length();
for(int i =0;i<(len % 2);i++ ){
if (this.pal.charAt(i)!=this.pal.charAt(len-i-1)){
return false;
}
}

return true;
}

public void setter(String pal) {


this.pal = pal;
}

public String getter() {


return pal;
}

// MAIN METHOD
public static void main(String[] args) throws IOException{
//instantiate Plaindrom object
Palindrom palObj=new Palindrom();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
do {
System.out.print("Enter some word(Press '/' for end): ");
str = in.readLine();
if (0==str.compareTo("/")){
break;
}else
{
palObj.setter(str);
if (palObj.helper()){
System.out.println("\n"+palObj.getter()+" is Plaindrom!");
}
else{
System.out.println("\n"+palObj.getter()+" is not Plaindrom!");
}
}
}while (true);

} catch (IOException e) {
e.printStackTrace();
}

}//main

1. import java.io.*;
2. import java.lang.*;
3. /**
4.   *
5.   * @author Developer
6.   */
7. public class Palindrome
8. {
9.  
10. public static void main(String[] args)throws IOException
11. {
12. Console console=System.console();
13. String word=console.readLine("Enter a word of yr choice");
14. int flag=1;
15. int left=0;
16. int right=word.length()-1;
17. while(left<right)
18. {
19. if(word.charAt(left)!=word.charAt(right))
20. {
21. flag=0;
22. }
23. left++;
24. right--;
25. if(flag==1)
26. {
27. System.out.println("The word" + word + "is a palindrome");
28. }
29. else
30. {
31. System.out.println("The word"+ word + "is not a palindrome");
32. }
33.  
34. }
35.  
36. }
37. }
{
flag=0;
}
left++;

You might also like