From 18d4e000719fc26658493a3b10d00bccf68fe6b2 Mon Sep 17 00:00:00 2001 From: enghqii Date: Sat, 15 Mar 2014 15:26:21 +0900 Subject: [PATCH 1/4] Update Fibonacci.java parse exception handling, 'start' out of range handling --- Fibonacci.java | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/Fibonacci.java b/Fibonacci.java index ddf00f8..866adbf 100644 --- a/Fibonacci.java +++ b/Fibonacci.java @@ -3,14 +3,29 @@ public class Fibonacci{ public static void main(String []args){ - int start = Integer.parseInt(args[0]); - BigInteger fib1 = new BigInteger("0"); - BigInteger fib2 = new BigInteger("1"); - for(int i = 0; i <= (start -2); i++) { - BigInteger fib3 = fib1.add(fib2); - System.out.println("F"+ i + " = " + fib1 + ", F" + (i+1) + " = " + fib2 + ", F" + (i+2) + " = " + fib3); - fib1 = fib2; - fib2 = fib3; - } + + int start = 10; + + try{ + + if(args.length > 0){ + start = Integer.parseInt(args[0]); + } + + }catch(NumberFormatException e){ + e.printStackTrace(); + } + + if(start > 2){ + + BigInteger fib1 = new BigInteger("0"); + BigInteger fib2 = new BigInteger("1"); + for(int i = 0; i <= (start -2); i++) { + BigInteger fib3 = fib1.add(fib2); + System.out.println("F"+ i + " = " + fib1 + ", F" + (i+1) + " = " + fib2 + ", F" + (i+2) + " = " + fib3); + fib1 = fib2; + fib2 = fib3; + } + } } -} \ No newline at end of file +} From 8c813710f685b9f1f7e1e858248767bfaba0b22a Mon Sep 17 00:00:00 2001 From: enghqii Date: Sat, 15 Mar 2014 15:28:52 +0900 Subject: [PATCH 2/4] Update Fibonacci.java change initial value --- Fibonacci.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Fibonacci.java b/Fibonacci.java index 866adbf..f492c38 100644 --- a/Fibonacci.java +++ b/Fibonacci.java @@ -4,7 +4,7 @@ public class Fibonacci{ public static void main(String []args){ - int start = 10; + int start = 0; try{ From 466e5b6d0385478bfc7ec6dabac27bac46e738ff Mon Sep 17 00:00:00 2001 From: enghqii Date: Sat, 15 Mar 2014 15:50:22 +0900 Subject: [PATCH 3/4] Update Fibonacci.java Execution time display added --- Fibonacci.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Fibonacci.java b/Fibonacci.java index f492c38..301470a 100644 --- a/Fibonacci.java +++ b/Fibonacci.java @@ -6,8 +6,8 @@ public static void main(String []args){ int start = 0; + try{ - if(args.length > 0){ start = Integer.parseInt(args[0]); } @@ -18,6 +18,8 @@ public static void main(String []args){ if(start > 2){ + long startTime = System.currentTimeMillis(); + BigInteger fib1 = new BigInteger("0"); BigInteger fib2 = new BigInteger("1"); for(int i = 0; i <= (start -2); i++) { @@ -26,6 +28,9 @@ public static void main(String []args){ fib1 = fib2; fib2 = fib3; } + + long endTime = System.currentTimeMillis(); + System.out.println("Execution time : " + ((endTime - startTime)/1000.0) + "sec" ); } } } From af81ae69bd057d12cfbb6ec7c40406cd1f53f2dc Mon Sep 17 00:00:00 2001 From: enghqii Date: Sat, 15 Mar 2014 15:58:16 +0900 Subject: [PATCH 4/4] Update Fibonacci.java output simplified --- Fibonacci.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Fibonacci.java b/Fibonacci.java index 301470a..e7a5429 100644 --- a/Fibonacci.java +++ b/Fibonacci.java @@ -22,9 +22,14 @@ public static void main(String []args){ BigInteger fib1 = new BigInteger("0"); BigInteger fib2 = new BigInteger("1"); + + + System.out.println("F" + 0 + " = " + fib1); + System.out.println("F" + 1 + " = " + fib2); + for(int i = 0; i <= (start -2); i++) { BigInteger fib3 = fib1.add(fib2); - System.out.println("F"+ i + " = " + fib1 + ", F" + (i+1) + " = " + fib2 + ", F" + (i+2) + " = " + fib3); + System.out.println("F" + (i+2) + " = " + fib3); fib1 = fib2; fib2 = fib3; }