master
Noah Pederson 6 years ago
parent 015f3067b7
commit 56c14a224a
  1. 2
      .idea/modules.xml
  2. 11
      Chapter15/Chapter15.iml
  3. 49
      Chapter15/src/Reader.java
  4. 2
      Chapter9/.classpath
  5. 11
      PA10/PA10.iml
  6. 137
      PA10/src/SortStuff.java
  7. 189
      src/HugeInteger2.java

@ -6,8 +6,10 @@
<module fileurl="file://$PROJECT_DIR$/Chapter10/Chapter10.iml" filepath="$PROJECT_DIR$/Chapter10/Chapter10.iml" />
<module fileurl="file://$PROJECT_DIR$/Chapter11/Chapter11.iml" filepath="$PROJECT_DIR$/Chapter11/Chapter11.iml" />
<module fileurl="file://$PROJECT_DIR$/Chapter14/Chapter14.iml" filepath="$PROJECT_DIR$/Chapter14/Chapter14.iml" />
<module fileurl="file://$PROJECT_DIR$/Chapter15/Chapter15.iml" filepath="$PROJECT_DIR$/Chapter15/Chapter15.iml" />
<module fileurl="file://$PROJECT_DIR$/Chapter9/Chapter9.iml" filepath="$PROJECT_DIR$/Chapter9/Chapter9.iml" />
<module fileurl="file://$PROJECT_DIR$/Midterm-Part2/Midterm-Part2.iml" filepath="$PROJECT_DIR$/Midterm-Part2/Midterm-Part2.iml" />
<module fileurl="file://$PROJECT_DIR$/PA10/PA10.iml" filepath="$PROJECT_DIR$/PA10/PA10.iml" />
<module fileurl="file://$PROJECT_DIR$/School.iml" filepath="$PROJECT_DIR$/School.iml" />
</modules>
</component>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,49 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Reader {
public static void main(String... args) {
File f = new File("numbers.txt");
ArrayList<Integer> numbers = new ArrayList<>();
Scanner scanner = null; //null so IntelliJ doesn't yell at me
Formatter formatter = null;
try {
scanner = new Scanner(f);
formatter = new Formatter("output.txt");
while (scanner.hasNextLine()) {
try {
numbers.add(scanner.nextInt());
} catch (NoSuchElementException ex) {
//do nothing, it just means we got to the end of the file
}
}
for (Integer i : numbers) {
//The assignment didn't specify a format, so I'm just printing it back out
formatter.format("%d\n", i);
}
} catch (FileNotFoundException e) {
System.err.println("File 'numbers.txt' not found.");
} catch (FormatterClosedException | NoSuchElementException | ServiceConfigurationError e) {
System.err.println(e.getMessage() + "\nStacktrace: " + e.getStackTrace());
} catch (IllegalFormatException e) {
System.err.println("Illegal format exception: " + e.getMessage());
} finally {
if (scanner != null)
scanner.close();
if(formatter != null)
formatter.close();
}
System.out.println("Done!");
}
}

@ -2,5 +2,5 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="/out/production/Chapter9"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,137 @@
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
/**
* Created by noah on 6/19/2016.
* Project: School
*/
public class SortStuff {
public static void main(String... args) {
int array1[] = new int[1000];
int array2[] = new int[10000];
int array3[] = new int[100000];
int array4[] = new int[100000];
fillRandom(array1);
fillRandom(array2);
fillRandom(array3);
int num = -50000;
for (int i = 0; i < 100000; i++) {
array4[i] = num;
num++;
}
long startTime = 0L;
long endTime = 0L;
displayInt(array1);
//The description gives code, but it didn't specify that I had to print the countdown, so it's easier to do this
sleep();
startTime = System.nanoTime();
Arrays.sort(array1);
endTime = System.nanoTime();
System.out.printf("Sorting array took %d nanoseconds", (endTime - startTime));
displayInt(array1);
startTime = System.nanoTime();
Arrays.binarySearch(array1, 0);
Arrays.binarySearch(array1, 900);
Arrays.binarySearch(array1, 2000);
endTime = System.nanoTime();
System.out.printf("\nBinary searching array took %d nanoseconds\n", (endTime - startTime));
displayInt(array2);
//The description gives code, but it didn't specify that I had to print the countdown, so it's easier to do this
sleep();
startTime = System.nanoTime();
Arrays.sort(array2);
endTime = System.nanoTime();
System.out.printf("Sorting array took %d nanoseconds", endTime - startTime);
displayInt(array2);
startTime = System.nanoTime();
Arrays.binarySearch(array2, 0);
Arrays.binarySearch(array2, 900);
Arrays.binarySearch(array2, 2000);
endTime = System.nanoTime();
System.out.printf("\n\nBinary searching array took %d nanoseconds\n\n", endTime - startTime);
displayInt(array3);
//The description gives code, but it didn't specify that I had to print the countdown, so it's easier to do this
sleep();
startTime = System.nanoTime();
Arrays.sort(array3);
endTime = System.nanoTime();
System.out.printf("Sorting array took %d nanoseconds", endTime - startTime);
displayInt(array3);
startTime = System.nanoTime();
Arrays.binarySearch(array3, 0);
Arrays.binarySearch(array3, 900);
Arrays.binarySearch(array3, 2000);
endTime = System.nanoTime();
System.out.printf("\nBinary searching array took %d nanoseconds\n", endTime - startTime);
displayInt(array4);
//The description gives code, but it didn't specify that I had to print the countdown, so it's easier to do this
sleep();
startTime = System.nanoTime();
Arrays.sort(array4);
endTime = System.nanoTime();
System.out.printf("Sorting array took %d nanoseconds", endTime - startTime);
displayInt(array4);
startTime = System.nanoTime();
Arrays.binarySearch(array4, 0);
Arrays.binarySearch(array4, 900);
Arrays.binarySearch(array4, 2000);
endTime = System.nanoTime();
System.out.printf("\nBinary searching array took %d nanoseconds\n", endTime - startTime);
}
//Modifies the contents of the input array
public static void fillRandom(int[] input) {
input[0] = 0;
input[1] = 900;
SecureRandom random = new SecureRandom();
for (int i = 2; i < input.length; i++) {
input[i] = random.nextInt(2000) - 999;
}
}
public static void displayInt(int[] input) {
int startIndex = 0;
if (input.length >= 100)
startIndex = input.length - 101;
for (int i = startIndex; i < input.length; i++) {
System.out.print(input[i] + " ");
if (i % 10 == 0)
System.out.print("\n");
}
}
private static void sleep() {
// Include following for a 5 second countdown
System.out.printf("%n%n");
for (int i = 4; i >= 0; --i) {
System.out.printf(" %d", i);
try {
Thread.sleep(1000); // pause 1000 milliseconds (one second)
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
System.out.printf("%n%n");
}
}

@ -1,189 +0,0 @@
import *;
/**
* Created by noah on 5/13/2016.
*/
public class HugeInteger2 {
private final int DEFAULT_DIGITS_LENGTH = 20;
private byte[] digits; //This could really be a smaller data type like a byte or something (as small as 4 bits)
// but because I don't feel like having casts everywhere, it's an int
private int digitCount;
public static void main(String args[]) {
HugeInteger2 h1 = new HugeInteger2("19641684156196898849849161");
HugeInteger2 h2 = new HugeInteger2("651651651321616516551321649846123");
HugeInteger2 h3 = new HugeInteger2("651651651321616516551321649846123");
HugeInteger2 h4 = new HugeInteger2("651651651321616516551321649846124");
System.out.println("h1 is equal to h2?: (false) " + h1.isEqualTo(h2));
System.out.println("h2 is equal to h3?: (true) " + h2.isEqualTo(h3));
System.out.println("h1 is less than h2?: (true) " + h1.isLessThan(h2));
System.out.println("h2 is greater than h1?: (true) " + h2.isGreaterThan(h1));
System.out.println("h2 is greater than/equal to h3?: (true) " + h2.isGreaterThanOrEqualTo(h3));
System.out.println("h3 is less than/equal to h4?: (true) " + h3.isLessThanOrEqualTo(h4));
}
public HugeInteger2() {
digits = new byte[DEFAULT_DIGITS_LENGTH];
digitCount = 0;
}
public HugeInteger2(String s) {
parse(s);
}
public HugeInteger2(int digits) {
this.digits = new byte[digits];
digitCount = digits;
}
public void parse(String input) throws NumberFormatException{
int startpos = DEFAULT_DIGITS_LENGTH - input.length();
digitCount = (input.length()/2) + (input.length() % 2);
int z = 0;
for(int i = startpos; i < DEFAULT_DIGITS_LENGTH; i++) {
digits[i] = (byte)Character.getNumericValue(input.charAt(z));
z++;
}
}
//We do this so we can compare individual indexes for when comparing HugeInteger2 instances
public int getDigit(int index) {
if (index >= 0 && index < digits.length * 2) {
return index % 2 == 0 ? getFirstByte(digits[index/2]) : getSecondByte(digits[index/2]);
}
else
throw new ArrayIndexOutOfBoundsException("Not a valid index");
}
//Returns the number of leading zeroes in the array
public int getDigitCount() {
return digits.length * 2;
}
public boolean isEqualTo(HugeInteger2 h) {
if (getDigitCount() != h.getDigitCount())
return false;
for (int i = 0; i < DEFAULT_DIGITS_LENGTH; i++ ){
if(digits[i] != h.getDigit(i))
return false;
}
return true;
}
public boolean isNotEqualTo(HugeInteger2 h) {
if (getDigitCount() != h.getDigitCount())
return true;
for (int i = 0; i < DEFAULT_DIGITS_LENGTH; i++ ){
if(getDigit(i) != h.getDigit(i))
return true;
}
return false;
}
public boolean isGreaterThan(HugeInteger2 h) {
//Compare digit counts, because if the digit counts are different, then obviously they are not equal
// O(1)
if(getDigitCount() > h.getDigitCount())
return true;
else if (getDigitCount() < h.getDigitCount())
return false;
//O(n) worst case
for (int i = 0; i < digits.length; i++) {
//Loop until we find the first instance of a given digit pair where this index is larger than the other
//and return true
if(digits[i] > h.getDigit(i))
return true;
else if (digits[i] < h.getDigit(i))
return false;
}
//If we get to this point, everything is equal and one is not greater than the other, so false
return false;
}
public boolean isLessThan(HugeInteger2 h) {
//Compare digit counts, because if the digit counts are different, then obviously they are not equal
// O(1)
if(getDigitCount() > h.getDigitCount())
return false;
else if (getDigitCount() < h.getDigitCount())
return true;
//O(n) worst case
for (int i = 0; i < digits.length; i++) {
//Loop until we find the first instance of a given digit pair where this index is larger than the other
//and return true
if(digits[i] > h.getDigit(i))
return false;
else if (digits[i] < h.getDigit(i))
return true;
}
//If we get to this point, everything is equal and one is not greater than the other, so false
return false;
}
public boolean isGreaterThanOrEqualTo(HugeInteger2 h) {
//Compare digit counts, because if the digit counts are different, then obviously they are not equal
// O(1)
if(getDigitCount() > h.getDigitCount())
return true;
else if (getDigitCount() < h.getDigitCount())
return false;
//O(n) worst case
for (int i = trimZeroes; i < DEFAULT_DIGITS_LENGTH; i++) {
//Loop until we find the first instance of a given digit pair where this index is larger than the other
//and return true
if(digits[i] > h.getDigit(i))
return true;
else if (digits[i] < h.getDigit(i))
return false;
}
//If we get to this point, everything is equal, so the comparison evaluates to true
return true;
}
public boolean isLessThanOrEqualTo(HugeInteger2 h) {
//Compare digit counts, because if the digit counts are different, then obviously they are not equal
// O(1)
if(getDigitCount() > h.getDigitCount())
return false;
else if (getDigitCount() < h.getDigitCount())
return true;
//O(n) worst case
for (int i = digitCount; i < DEFAULT_DIGITS_LENGTH; i++) {
//Loop until we find the first instance of a given digit pair where this index is larger than the other
//and return true
if(digits[i] > h.getDigit(i))
return false;
else if (digits[i] < h.getDigit(i))
return true;
}
//If we get to this point, everything is equal and one is not greater than the other, so false
return true;
}
public boolean isZero() {
if (digitCount == 0)
return true;
for (int i = trimZeroes; i < DEFAULT_DIGITS_LENGTH; i++) {
if(digits[i] != 0)
return false;
}
return true;
}
public byte getFirstByte(byte b) {
return (byte)(b & (byte)240);
}
public byte getSecondByte(byte b) {
return (byte)(b & (byte) 15);
}
//I do not feel ambitious. This is tedious. No multiply/divide/remainder functions.
}
Loading…
Cancel
Save