Results 1 to 7 of 7

Thread: robust programs

  1. #1
    dntcheatme is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default 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 11:12 AM. Reason: typo error....

  2. #2
    dntcheatme is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    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...");

    }



    }

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  4. #4
    dntcheatme is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default 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);

    }
    }



    }
    }

  5. #5
    dntcheatme is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    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.
    yaa it is but then unless you implement everything by urself..u dun know how does it work properly..anyways u may go ahead and use the libraries..and well..nothing like homework here...just exploring the language......

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by dntcheatme View Post
    ..anyways u may go ahead and use the libraries..
    Thanks :rolleyes:

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  1. Writing a programs
    By smray7 in forum New To Java
    Replies: 7
    Last Post: 04-28-2011, 08:49 AM
  2. Replies: 0
    Last Post: 04-21-2011, 10:51 AM
  3. Cant run my programs anymore
    By Glenn1990 in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 09:49 PM
  4. UML, robust code, structure questions
    By mac in forum New To Java
    Replies: 2
    Last Post: 12-23-2009, 12:06 AM
  5. I need a simple programs
    By mikau in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •