InterviewSolution
| 1. |
A class Palin has been defined to check whether a positive number is a Palindrome number or not.The number ‘N’ is palindrome if the original number and it’s reverse are the same. Some of the members of the class are given below: Class name: Palin Data members/instance variables: num: integer to store the number revnum: integer to store the reverse of the number Methods/Member functions: Palin(): constructor to initialize data members with legal initial values void accept(): to accept the number int reverse(int y): reverses the parameterized argument ‘y’ and stores it in revenue using a recursive technique void check(): checks whether the number is a Palindrome by invoking the function reverse() and display the result with an appropriate message Specify the class Palin giving the details of the constructor (), void accept(), int reverse(int) and void check(). Define the main() function to create an object and call the functions accordingly to enable the task. |
|
Answer» import java.io.*; class Palin { int num; int revnum; Palin() { num = 0; revnum = 0; } void accept() throws IOException { BufferedReader y = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the Number:"); String a = y. readLine(); num = Integer.parselnt(a); } int reverse(int y) { int len = (y + " ").length(); if(len ==1) { return y; } else { return(((y%10)* (int) Math.powf 10. len - 1)) + reverse(y /10)); } } void check() { revnum = reverse(num); if(num == revnum) { System.out.println("\n Nrnnber is palindrome"); } else { System.out.println("\n Number is not palindrome"); } } public static void main(String args[]) throws IOException { Palin p = new Palin(); p.accept(); p.check(); } } |
|