Write a program to reverse a string using recursive algorithm
package com.java2novice.algos;
public class StringRecursiveReversal
{
String reverse = "";
public String reverseString(String str)
{
if(str.length()
== 1)
{
return
str;
}
else
{
reverse
+= str.charAt(str.length()-1)
+reverseString(str.substring(0,str.length()-1));
return
reverse;
}
}
public static void main(String
a[])
{
StringRecursiveReversal
srr = new StringRecursiveReversal();
System.out.println("Result:
"+srr.reverseString("Java2novice"));
}
}
Write a program to reverse a number
package com.java2novice.algos;
public class NumberReverse
{
public int reverseNumber(int number)
{
int reverse = 0;
while(number
!= 0)
{
reverse
= (reverse*10)+(number%10);
number
= number/10;
}
return reverse;
}
public static void main(String a[])
{
NumberReverse
nr = new NumberReverse();
System.out.println("Result:
"+nr.reverseNumber(17868));
}
}
Write a program to convert decimal number to binary format
package com.java2novice.algos;
public class DecimalToBinary
{
public void printBinaryFormat(int number)
{
int binary[] = new int[25];
int index = 0;
while(number
> 0)
{
binary[index++]
= number%2;
number
= number/2;
}
for(int i = index-1;i >=
0;i--)
{
System.out.print(binary[i]);
}
}
public static void main(String a[])
{
DecimalToBinary
dtb = new DecimalToBinary();
dtb.printBinaryFormat(25);
}
}
Write a program to find perfect number or not
package com.java2novice.algos;
public class IsPerfectNumber
{
public boolean isPerfectNumber(int number)
{
int temp = 0;
for(int i=1;i<=number/2;i++)
{
if(number%i
== 0){
temp
+= i;
}
}
if(temp
== number)
{
System.out.println("It
is a perfect number");
return true;
}
else
{
System.out.println("It
is not a perfect number");
return false;
}
}
public static void main(String a[])
{
IsPerfectNumber
ipn = new IsPerfectNumber();
System.out.println("Is
perfect number: "+ipn.isPerfectNumber(28));
}
}
Write a program to implement Array List
package com.java2novice.algos;
import java.util.Arrays;
public class MyArrayList
{
private Object[] myStore;
private int actSize =
0;
public MyArrayList()
{
myStore
= new Object[10];
}
public Object get(int index)
{
if(index < actSize)
{
return
myStore[index];
}
else
{
throw
new ArrayIndexOutOfBoundsException();
}
}
public void add(Object obj)
{
if(myStore.length-actSize
<= 5)
{
increaseListSize();
}
myStore[actSize++]
= obj;
}
public Object remove(int
index)
{
if(index
< actSize)
{
Object
obj = myStore[index];
myStore[index]
= null;
int
tmp = index;
while(tmp
< actSize)
{
myStore[tmp]
= myStore[tmp+1];
myStore[tmp+1]
= null;
tmp++;
}
actSize--;
return
obj;
}
else
{
throw
new ArrayIndexOutOfBoundsException();
}
}
public int size()
{
return
actSize;
}
private void increaseListSize()
{
myStore
= Arrays.copyOf(myStore, myStore.length*2);
System.out.println("\nNew
length: "+myStore.length);
}
public static void
main(String a[])
{
MyArrayList
mal = new MyArrayList();
mal.add(new
Integer(2));
mal.add(new
Integer(5));
mal.add(new
Integer(1));
mal.add(new
Integer(23));
mal.add(new
Integer(14));
for(int
i=0;i
{
System.out.print(mal.get(i)+"
");
}
mal.add(new
Integer(29));
System.out.println("Element
at Index 5:"+mal.get(5));
System.out.println("List
size: "+mal.size());
System.out.println("Removing
element at index 2: "+mal.remove(2));
for(int
i=0;i
{
System.out.print(mal.get(i)+"
");
}
}
}
No comments:
Post a Comment