badalrocks
BAN USERThis is the solution in Java.
package com.badal.careercup;
public class IntPalindrome {
private boolean isPalindrome(int n){
String test=Integer.toString(n);
if(test.charAt(0)!=test.charAt(test.length()-1)){
System.out.println("First and last digits are not same. The number is not a palindrome. ");
return false;
}
String firsthalf=test.substring(0,test.length()/2);
//System.out.println("First Half is: "+firsthalf);
String secondhalf;
if(test.length()%2==0){
secondhalf=test.substring(test.length()/2, test.length());
}else{
secondhalf= test.substring(test.length()/2+1, test.length());
}
StringBuffer sb=new StringBuffer(secondhalf);
sb=sb.reverse();
String reverseSecondhalf=sb.toString();
//System.out.println("Reverse Second Half is: "+reverseSecondhalf);
if(firsthalf.equals(reverseSecondhalf)){
return true;
}
else{
return false;
}
}
public static void main(String [] args){
IntPalindrome obj=new IntPalindrome();
if(obj.isPalindrome(123213)){
System.out.println("The number is a palindrome.");
}else{
System.out.println("The number is not a palindrome.");
}
}
}
package com.badal.careercup;
import java.util.ArrayList;
public class Fibonacci {
private ArrayList<Integer> generateFibonacci (int n){
ArrayList<Integer> fib=new ArrayList<Integer>();
fib.add(0);
fib.add(1);
for(int i=2;i<n;i++){
fib.add(fib.get(i-1)+fib.get(i-2));
}
return fib;
}
public static void main(String[] args){
Fibonacci f=new Fibonacci();
System.out.println(f.generateFibonacci(13));
}
}
package com.badal.careercup;
import java.util.ArrayList;
import java.util.Collections;
public class ReplaceString {
public static void main(String[] args) throws Exception{
ArrayList<Integer> inputEven=new ArrayList<Integer>();
ArrayList<Integer> inputOdd=new ArrayList<Integer>();
for(int i=0;i<args.length;i++){
if(i%2==0){
inputEven.add(Integer.parseInt(args[i]));
}else{
inputOdd.add(Integer.parseInt(args[i]));
}
}
// display even arraylist
for(int x:inputEven){
System.out.print("Even array list is: ");
System.out.print(x+" ");
}
System.out.println();
// display odd arraylist
for(int x:inputOdd){
System.out.print("Odd array list is: ");
System.out.print(x+" ");
}
System.out.println();
Object objMinEven=Collections.min(inputEven);
Object objMaxOdd=Collections.max(inputOdd);
System.out.println("The minimum even number is: "+objMinEven);
System.out.println("The maximum odd number is: "+objMaxOdd);
}
}
Convert the number to a string and then call the string's length function to give the number of digits
- badalrocks December 20, 2008
Two ways to do this in Java:
- badalrocks January 06, 2009Let the classes be A, B and C.
Assume C wants to inherit from A and B.
1)Have B extend A (or vice versa) and C extend B. This can be done only if you own all the 3 classes.
2)Have B contain A i.e. B HAS-A A. And make C extend B.