Results 1 to 7 of 7
Thread: robust programs
- 05-03-2011, 10:07 AM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
robust programs
hey guys..
this is an initiative to create robust programs.. so people just come ahead and post you queries and we will try together to create the best possible programs here...i am a new guy to this language..
my first initiative to creating a robust program was checking whether a number is a pallindrome or not..although this is a very easy program but making it robust was a little difficult because if we enter a very big number..the program generally throws an exception because the number that we had entered had already exceeded the range of the primitive data type that we had used in the program.
i have made a program using strings..and it works just fine..u can enter a number of any length and it does not throw an exception......Last edited by dntcheatme; 05-03-2011 at 10:12 AM. Reason: typo error....
- 05-03-2011, 10:09 AM #2
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
checking whether a number is pallindrome or not...this program does not throw an exception until and unless the buffer is full...
import java.io.*;
public class CheckPallindrome
{
public static void main(String[] args)
{
String str = null;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter the number to be checked......");
str = reader.readLine();
}
catch( IOException ioe)
{
System.out.println(" exception handled ");
}
checkPallindrome(str);
}
public static void checkPallindrome(String str)
{
System.out.println("Total number of digits in the given number are : "+str.length());
boolean b = false;
for( int i=0,j=(str.length()-1);i<=j;i+=2,j-=2)
{
byte i1 = Byte.parseByte(str.substring(i,i+2));
byte i2 = Byte.parseByte(str.substring(j-1,str.length()-i));
byte temp = (byte)((i2%10)*10 + (i2/10));
if( temp == i1)
{
b = true;
continue;
}
else
{
b = false;
break;
}
}
if( b == true)
System.out.println(" the number is a pallindrome...");
else
System.out.println(" the number is not a pallindrome...");
}
}
- 05-03-2011, 10:14 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Palindromes again, eh?
'an initiative to create robust programs..' or just some new set text for homework issued to all Java courses?
For robustness, it's probably good practice to use the library methods provided, as in palindrome.
- 05-03-2011, 10:17 AM #4
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
fibonacci series
import java.io.*;
class Fibonacci
{
public static void main(String[] args)
{
String str1 = null , str2 = null;
BufferedReader reader1,reader2;
reader1 = new BufferedReader(new InputStreamReader(System.in));
reader2 = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("enter the end index of the series to be generated...");
str1 = reader1.readLine();
System.out.println("enter the starting number of the fibonacci series...");
str2 = reader2.readLine();
}
catch( IOException ioe)
{
System.out.println(" exception handled ");
}
fibonacci(Integer.parseInt(str1),Integer.parseInt( str2));
}
public static void fibonacci(int x,long y)
{
long[] a = new long[2];
long sum;
a[0] = a[1] = y;
long range = 2147483647;
if(a[0]>range)
{
fibonacci1(x,Long.toString(a[0]),Long.toString(a[1]));
}
else
{
for(int i =0; i<x ; i++)
{
System.out.println(a[1]);
sum = a[0] + a[1];
a[0] = a[1];
a[1] = sum;
if(a[1]>range)
{
fibonacci1(x-(i+1),Long.toString(a[0]),Long.toString(a[1]));
}
}
}
}
public static void fibonacci1(int x,String sb1,String sb2)
{
StringBuffer sb3 = new StringBuffer(sb1);
StringBuffer sb4 = new StringBuffer(sb2);
int carry = 0;
if(sb1.length()%2 != 0)
{
sb3.insert(0,0); // No StringIndexOutOfBoundsException thrown because of this....
}
for(int counter = 0; counter<x;counter++)
{
for( int i = sb1.length()-1,j=0;i==0;i-=2,j+=2)
{
int a1 = Integer.parseInt(sb1.substring(i-1,sb1.length()-j));
int a2 = Integer.parseInt(sb2.substring(i-1,sb1.length()-j));
a2 = (a1 + a2 + carry);
carry = a2 % 100;
sb4.replace(i-1,sb3.length()-j,Integer.toString(a2));
System.out.println(sb4);
}
}
}
}
- 05-03-2011, 10:19 AM #5
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
- 05-03-2011, 04:46 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 05-03-2011, 08:54 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
when you post code please use code tags.
[code]
YOUR CODE HERE
[/code]
I also feel like for the most part you can understand what the java library does and safely use them. Diorde's link is definitely a simple way which should work for very very large numbers.
Similar Threads
-
Writing a programs
By smray7 in forum New To JavaReplies: 7Last Post: 04-28-2011, 07:49 AM -
Stable, Robust Image Files to PDF & HTML to PDF Conversion
By sherazam in forum Java SoftwareReplies: 0Last Post: 04-21-2011, 09:51 AM -
Cant run my programs anymore
By Glenn1990 in forum New To JavaReplies: 2Last Post: 02-25-2011, 08:49 PM -
UML, robust code, structure questions
By mac in forum New To JavaReplies: 2Last Post: 12-22-2009, 11:06 PM -
I need a simple programs
By mikau in forum New To JavaReplies: 2Last Post: 02-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks