Skip to content

Commit

Permalink
Homework01 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydattolo committed Jan 23, 2020
1 parent f0dddc2 commit 4c2ded2
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 15 deletions.
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 0 additions & 11 deletions HelloWorld.java

This file was deleted.

34 changes: 30 additions & 4 deletions Homework/Homework01.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
23 changes: 23 additions & 0 deletions PrincetonJava/CodeSamples/BottomUpFibonacci.java
Original file line number Diff line number Diff line change
@@ -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];

}
}
18 changes: 18 additions & 0 deletions PrincetonJava/CodeSamples/Quadratic.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions PrincetonJava/CodeSamples/StandardInput.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions PrincetonJava/CodeSamples/TopDownFibonacci.java
Original file line number Diff line number Diff line change
@@ -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];
}
}

0 comments on commit 4c2ded2

Please sign in to comment.