diff --git a/.vscode/launch.json b/.vscode/launch.json index 1704183..f3ab8dc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,41 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "java", + "name": "CodeLens (Launch) - test", + "request": "launch", + "mainClass": "Homework.test", + "projectName": "H212_87e1e728" + }, + { + "type": "java", + "name": "CodeLens (Launch) - Homework01", + "request": "launch", + "mainClass": "Homework.Homework01", + "projectName": "H212_87e1e728" + }, + { + "type": "java", + "name": "CodeLens (Launch) - BottomUpFibonacci", + "request": "launch", + "mainClass": "PrincetonJava.CodeSamples.BottomUpFibonacci", + "projectName": "H212_87e1e728" + }, + { + "type": "java", + "name": "CodeLens (Launch) - TopDownFibonacci", + "request": "launch", + "mainClass": "PrincetonJava.CodeSamples.TopDownFibonacci", + "projectName": "H212_87e1e728" + }, + { + "type": "java", + "name": "CodeLens (Launch) - Quadratic", + "request": "launch", + "mainClass": "PrincetonJava.CodeSamples.Quadratic", + "projectName": "H212_87e1e728" + }, { "type": "java", "name": "CodeLens (Launch) - runRutabega", diff --git a/HelloWorld.java b/HelloWorld.java deleted file mode 100644 index 5adb2d8..0000000 --- a/HelloWorld.java +++ /dev/null @@ -1,11 +0,0 @@ -/** - * HelloWorld - */ -public class HelloWorld { - - public static void main(String[] args) { - System.out.println("Hello, World!"); - System.out.println("H" + "\n" + "e" + "\n" + "l" + "\n" + - "l" + "\n" + "o"); - } -} \ No newline at end of file diff --git a/Homework/Homework01.java b/Homework/Homework01.java index 25bf15a..64e68b8 100644 --- a/Homework/Homework01.java +++ b/Homework/Homework01.java @@ -1,10 +1,36 @@ package Homework; -import java.util.Dictionary; - +import java.math.BigInteger; +import java.util.*; public class Homework01 { - Dictionary d = new Dictionary(); - public static int fib(int n) { + + private static BigInteger x = new BigInteger("1"); + public static void main(String[] args) { + // Scanner for terminal number input + // Scanner scan = new Scanner(System.in); + // System.out.println("Enter # for Fibonacci Computation: "); + // int number = scan.nextInt(); + // System.out.println("Fibonacci of " + number + ": " + fib(number)); + + // Fixed variable for debugging + // String number = "34"; + + // Command Line Argument input + String number = args[0]; + int n = Integer.parseInt(number); + System.out.println(fib(n)); + + } + public static BigInteger fib(int index){ + if (index == 0) return x; + else if (index == 1) return x; + return fibHelper(1,index,x,x); } + public static BigInteger fibHelper(int location, int target, BigInteger a, BigInteger b){ + if (location == target) return b; + else{ + return fibHelper(location + 1, target, b, a.add(b)); + } + } } \ No newline at end of file diff --git a/PrincetonJava/CodeSamples/BottomUpFibonacci.java b/PrincetonJava/CodeSamples/BottomUpFibonacci.java new file mode 100644 index 0000000..1f02912 --- /dev/null +++ b/PrincetonJava/CodeSamples/BottomUpFibonacci.java @@ -0,0 +1,23 @@ +// Bottom-Up dynamic programming - Richard Bellman 1950s +// computes solution to all subproblems,starting with simplest and gradually going further +package PrincetonJava.CodeSamples; + +/** + * BottomUpFibonacci + */ +public class BottomUpFibonacci { + + public static void main(String[] args) { + System.out.println(fibDP(34)); + } + + public static long fibDP(int n) { + long[] f = new long[n+1]; + f[0] = 0; + f[1] = 1; + for (int i = 2; i <= n; i++) + f[i] = f[i-1] + f[i+2]; + return f[n]; + + } +} \ No newline at end of file diff --git a/PrincetonJava/CodeSamples/Quadratic.java b/PrincetonJava/CodeSamples/Quadratic.java new file mode 100644 index 0000000..3b6caf1 --- /dev/null +++ b/PrincetonJava/CodeSamples/Quadratic.java @@ -0,0 +1,18 @@ +package PrincetonJava.CodeSamples; + + +/** + * Quadratic + */ +public class Quadratic { + + public static void main(String[] args) { + + double b = Double.parseDouble(args[0]); + double c = Double.parseDouble(args[1]); + double discriminant = b*b - 4.0*c; + double d = Math.sqrt(discriminant); + System.out.println((-b + d) / 2.0); + System.out.println((-b - d) / 2.0); + } +} \ No newline at end of file diff --git a/PrincetonJava/CodeSamples/StandardInput.java b/PrincetonJava/CodeSamples/StandardInput.java new file mode 100644 index 0000000..ec05153 --- /dev/null +++ b/PrincetonJava/CodeSamples/StandardInput.java @@ -0,0 +1,13 @@ +package PrincetonJava.CodeSamples; +import java.util.*; + +public class StandardInput { + + public static void main(String[] args) { + Scanner test = new Scanner(System.in); + System.out.println("Enter Username: "); + + String userName = test.nextLine(); + System.out.println("Username is: " + userName); + } +} \ No newline at end of file diff --git a/PrincetonJava/CodeSamples/TopDownFibonacci.java b/PrincetonJava/CodeSamples/TopDownFibonacci.java new file mode 100644 index 0000000..ec999f8 --- /dev/null +++ b/PrincetonJava/CodeSamples/TopDownFibonacci.java @@ -0,0 +1,26 @@ +// Top-Down = memoization +// Cache (store) values of each subproblem. +// Uses a static variable (class/global variable) +// Uses an array of longs + +package PrincetonJava.CodeSamples; + +/** + * TopDownFibonacci + */ +public class TopDownFibonacci { + + public static void main(String[] args) { + System.out.println(fibMemo(80)); + } + + static long[] memo = new long[100]; + + public static long fibMemo(int n){ + if (n==0) return 0; + if (n==1) return 1; + if (memo[n] == 0) + memo[n] = fibMemo(n-1) + fibMemo(n-2); + return memo[n]; + } +} \ No newline at end of file