Sunday 1 September 2013

01. importjava.io.BufferedReader; 02. importjava.io.IOException; 03. importjava.io.InputStreamReader; 04. 05. /** 06. * @author 539189 07. * 08. */ 09. publicclassTribonacci { 10. 11. /** 12. * @param args 13. */ 14. publicstaticvoidmain(String[] args) { 15. // TODO Auto-generated method stub 16. BufferedReader bf =newBufferedReader(newInputStreamReader(System.in)); 17. try{ 18. System.out.print("Please Enter a number:"); 19. Integer n = Integer.parseInt(bf.readLine()); 20. 21. if(n <0){ 22. thrownewNegativeNumberException("Negative Numbers are not allowed!!"); 23. } 24. elseif(n >0&& n <4){ 25. thrownewInvalidInputException("You must not enter a valid input above 4"); 26. } 27. else{ 28. inta=0,b=1,c=1,sum=0; 29. System.out.print("The Tribonacci Serices is :\n 0\t 1\t 1\t"); 30. while(n >3){ 31. sum = a+b+c; 32. System.out.print(sum +"\t"); 33. n--; 34. a = b; 35. b = c; 36. c = sum; 37. } 38. } 39. }catch(NumberFormatException e) { 40. // TODO Auto-generated catch block 41. System.out.print("Please enter Numbers only !!"); 42. }catch(NegativeNumberException e){ 43. System.out.print(e.getMessage()); 44. }catch(InvalidInputException e){ 45. System.out.print(e.getMessage()); 46. } 47. catch(IOException e) { 48. // TODO Auto-generated catch block 49. System.out.print("Unexpceted error !!"); 50. } 51. 52. 53. } 54. 55. } 56. 57. classInvalidInputExceptionextendsException{ 58. privatestaticfinallongserialVersionUID = 1L; 59. 60. publicInvalidInputException(String msg){ 61. super(msg); 62. } 63. 64. }

0 comments:

Post a Comment