Created
November 10, 2016 22:30
-
-
Save QuantumFractal/6a4e8c3eaaa184c7023569b0742e5ae2 to your computer and use it in GitHub Desktop.
Exam 2 Review Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package source; | |
public class Fib { | |
public static void main(String[] args) { | |
System.out.println("Total paths given 4 tiles "+countPaths(4)); | |
System.out.println(intPower(2, 16)); | |
} | |
public static void start(int n ){ | |
for (int i=n; i >= 0; i--){ | |
String line = ""; | |
for (int j=0; j < i; j++){ | |
line += " "; | |
} | |
line += "*"; | |
System.out.println(line); | |
} | |
} | |
public static int intPower(int x, int p) { | |
if (p <= 0){ | |
return 1; | |
} | |
if (p % 2 == 0){ | |
int result = intPower(x, p/2); | |
return result * result; | |
} | |
if (p % 2 != 0){ | |
return x * intPower(x, p-1); | |
} | |
return 0; | |
} | |
public static int countPaths(int n) { | |
// Case in which we have a valid path | |
if (n == 1) | |
return 1; | |
// Case in which we have an invalid path | |
if (n < 1) | |
return 0; | |
// Count the number of valid paths for the remaining steps | |
int totalPaths = 0; | |
totalPaths += countPaths(n-1); | |
totalPaths += countPaths(n-2); | |
totalPaths += countPaths(n-3); | |
return totalPaths; | |
//return countPaths(n-1)+countPaths(n-2)+countPaths(n-3); | |
} | |
public static int fib(int n) { | |
if (n == 0){ | |
return 0; | |
} | |
if (n == 1){ | |
return 1; | |
} | |
if (n > 1) { | |
return fib(n-1) + fib(n-2); | |
} else { | |
return -1; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package source; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.PrintWriter; | |
import java.util.Scanner; | |
public class FileIo { | |
public static void main (String[] args) throws FileNotFoundException { | |
System.out.print("Enter filename > "); | |
Scanner in = new Scanner(System.in); | |
String filename = in.next(); | |
File inFile = new File(filename); | |
System.out.println(filename); | |
File outFile = new File(filename+".out"); | |
Scanner inFileScanner = new Scanner(inFile); | |
PrintWriter outFileWriter = new PrintWriter(outFile); | |
while (inFileScanner.hasNextLine()) { | |
String line = inFileScanner.nextLine(); // this is also a comment | |
// this is a comment | |
if (!line.trim().startsWith("//")){ | |
String newLine = line.split("//")[0]; | |
outFileWriter.write(newLine+"\n"); | |
} | |
} | |
System.out.println("Done! Written to "+filename+".out"); | |
inFileScanner.close(); | |
outFileWriter.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package soruce; | |
import java.util.Arrays; | |
public class Mergin { | |
public static void main (String[] args) { | |
int[] arr = {3 ,4 ,2 ,-1 ,0, 10, -5}; | |
mergeSort(arr); | |
System.out.println(Arrays.toString(arr)); | |
//[-5, -1, 0, 2, 3, 4, 10] | |
} | |
public static void mergeSort(int[] arr) | |
{ | |
mergeSortRec(arr, 0, arr.length - 1); | |
} | |
public static void mergeSortRec(int[] arr, int start, int end) { | |
//int mid = (start + end) / 2; | |
// int leftSum = arraySum(arr, start, mid); | |
// int rightSum = arraySum(arr, mid + 1, end); | |
if (start == end) | |
return; | |
int mid = (start + end) / 2; | |
mergeSortRec(arr, start, mid); | |
mergeSortRec(arr, mid + 1, end); | |
merge(arr, start, end, mid); | |
} | |
public static void merge(int[] arr, int start, int end, int mid) { | |
// copy first half of arr to a temporary array | |
int aSize = mid - start + 1; | |
int[] a = new int[aSize]; | |
for (int i = 0; i < aSize; ++i) | |
{ | |
a[i] = arr[i + start]; | |
} | |
int[] b = arr; // 'b' array is just the second half of arr | |
int[] result = arr; // result goes in original array arr | |
int i = 0; // starting index in a | |
int j = mid + 1; // starting index in b | |
final int iMax = aSize; // 1 + max index in a | |
final int jMax = end + 1; // 1 + max index in b | |
int k = start; // index in result | |
// Remaining code is identical to previous version MergeSort.merge() | |
while (i < iMax && j < jMax) | |
{ | |
if (a[i] <= b[j]) | |
{ | |
result[k] = a[i]; | |
i = i + 1; | |
k = k + 1; | |
} | |
else | |
{ | |
result[k] = b[j]; | |
j = j + 1; | |
k = k + 1; | |
} | |
} | |
// pick up any stragglers | |
while (i < iMax) | |
{ | |
result[k] = a[i]; | |
i = i + 1; | |
k = k + 1; | |
} | |
while (j < jMax) | |
{ | |
result[k] = b[j]; | |
j = j + 1; | |
k = k + 1; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package soruce; | |
import java.util.ArrayList; | |
public class WrapperClasses { | |
public static void main(String[] args) { | |
int a = 15; | |
Integer b = 30; | |
Integer c = new Integer(30); | |
Integer d = b; | |
ArrayList<Integer> arr = new ArrayList<Integer>(); | |
arr.add(5); | |
arr.add(a); | |
arr.add(b); | |
b.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment