Initial commit

master
Noah Pederson 6 years ago
commit c62ff82087
  1. 6
      .classpath
  2. 22
      .idea/compiler.xml
  3. 3
      .idea/copyright/profiles_settings.xml
  4. 6
      .idea/encodings.xml
  5. 19
      .idea/misc.xml
  6. 10
      .idea/modules.xml
  7. 6
      .idea/vcs.xml
  8. 15
      .project
  9. 6
      Chapter10/.classpath
  10. 15
      Chapter10/.project
  11. 6
      Chapter10/Chapter10.eml
  12. 11
      Chapter10/Chapter10.iml
  13. BIN
      Chapter10/Chapter10.zip
  14. 20
      Chapter10/src/Circle.java
  15. 25
      Chapter10/src/Cube.java
  16. 8
      Chapter10/src/Shape.java
  17. 26
      Chapter10/src/Sphere.java
  18. 19
      Chapter10/src/Square.java
  19. 20
      Chapter10/src/Test.java
  20. 25
      Chapter10/src/Tetrahedron.java
  21. 7
      Chapter10/src/ThreeDimensionalShape.java
  22. 21
      Chapter10/src/Triangle.java
  23. 5
      Chapter10/src/TwoDimensionalShape.java
  24. 6
      Chapter9/.classpath
  25. 15
      Chapter9/.project
  26. 6
      Chapter9/Chapter9.eml
  27. 11
      Chapter9/Chapter9.iml
  28. 28
      Chapter9/src/FourTest.java
  29. 13
      Chapter9/src/Parallelogram.java
  30. 32
      Chapter9/src/Point.java
  31. 31
      Chapter9/src/Quadrilateral.java
  32. 9
      Chapter9/src/Rectangle.java
  33. 10
      Chapter9/src/Square.java
  34. 14
      Chapter9/src/Trapezoid.java
  35. BIN
      Chapter9/src/diagram.png
  36. 6
      School.eml
  37. 2
      School.iml
  38. 3
      School.userlibraries
  39. 49
      src/Card.java
  40. 104
      src/DeckOfCards.java
  41. 84
      src/DeckOfCardsTest.java
  42. 177
      src/HugeInteger.java
  43. 189
      src/HugeInteger2.java
  44. 50
      src/Prime.java

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="out/production/School"/>
</classpath>

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>

@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="Java 8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Chapter10/Chapter10.iml" filepath="$PROJECT_DIR$/Chapter10/Chapter10.iml" />
<module fileurl="file://$PROJECT_DIR$/Chapter9/Chapter9.iml" filepath="$PROJECT_DIR$/Chapter9/Chapter9.iml" />
<module fileurl="file://$PROJECT_DIR$/School.iml" filepath="$PROJECT_DIR$/School.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>School</name>
<comment/>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Chapter10</name>
<comment/>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inherit-compiler-output="true" inheritJdk="true">
<output-test url="file://$MODULE_DIR$/../out/test/Chapter10"/>
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</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>

Binary file not shown.

@ -0,0 +1,20 @@
/**
* Created by noah on 5/24/2016.
*/
public class Circle extends TwoDimensionalShape{
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public Circle() {
this(5);
}
@Override
protected double getArea() {
return Math.PI * Math.pow(radius, 2);
}
}

@ -0,0 +1,25 @@
/**
* Created by noah on 5/24/2016.
*/
public class Cube extends ThreeDimensionalShape {
private int width;
public Cube(int width) {
this.width = width;
}
public Cube() {
this(5);
}
@Override
public double getVolume() {
return Math.pow(width, 3);
}
@Override
protected double getArea() {
return 6 * Math.pow(width, 2);
}
}

@ -0,0 +1,8 @@
/**
* Created by noah on 5/24/2016.
*/
public abstract class Shape {
protected abstract double getArea();
}

@ -0,0 +1,26 @@
/**
* Created by noah on 5/24/2016.
*/
public class Sphere extends ThreeDimensionalShape {
private int radius;
public Sphere(int radius) {
this.radius = radius;
}
public Sphere() {
this(5);
}
@Override
public double getVolume() {
return (4.0/3.0) * Math.PI * Math.pow(radius, 3);
}
@Override
public double getArea() {
return 4 * Math.PI * Math.pow(radius, 2);
}
}

@ -0,0 +1,19 @@
/**
* Created by noah on 5/24/2016.
*/
public class Square extends TwoDimensionalShape {
private int width;
public Square(int width) {
this.width = width;
}
public Square() {
this(5);
}
@Override
public double getArea() {
return width * width;
}
}

@ -0,0 +1,20 @@
/**
* Created by noah on 5/24/2016.
*/
public class Test {
public static void main(String... args) {
Shape[] shapes = new Shape[]{
new Circle(2), new Square(55), new Sphere(43), new Tetrahedron(2), new Triangle(66), new Cube(55)
};
for(Shape s : shapes) {
if(s instanceof TwoDimensionalShape) {
System.out.println("Shape is a Two Dimensional Shape\nArea: " + s.getArea());
} else if (s instanceof ThreeDimensionalShape) {
System.out.println("Shape is a Three Dimensional Shape\nArea: " + s.getArea() + "\nVolume: " + ((ThreeDimensionalShape) s).getVolume());
}
}
}
}

@ -0,0 +1,25 @@
/**
* Created by noah on 5/24/2016.
*/
public class Tetrahedron extends ThreeDimensionalShape {
private int edge;
public Tetrahedron(int edge) {
this.edge = edge;
}
public Tetrahedron() {
this(5);
}
@Override
public double getVolume() {
return Math.pow(edge, 3)/(6 * Math.sqrt(2));
}
@Override
protected double getArea() {
return Math.sqrt(3) * Math.pow(edge, 2);
}
}

@ -0,0 +1,7 @@
/**
* Created by noah on 5/24/2016.
*/
public abstract class ThreeDimensionalShape extends Shape {
public abstract double getVolume();
}

@ -0,0 +1,21 @@
/**
* Created by noah on 5/24/2016.
*/
public class Triangle extends TwoDimensionalShape {
private int width;
public Triangle(int width) {
this.width = width;
}
public Triangle() {
this(5);
}
@Override
public double getArea() {
return (width * width)/2;
}
}

@ -0,0 +1,5 @@
/**
* Created by noah on 5/24/2016.
*/
public abstract class TwoDimensionalShape extends Shape {
}

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Chapter9</name>
<comment/>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inherit-compiler-output="true" inheritJdk="true">
<output-test url="file://$MODULE_DIR$/../out/test/Chapter9"/>
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</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,28 @@
/**
* Created by noah on 5/22/2016.
*/
public class FourTest {
public static void main(String... args) {
Parallelogram p = new Parallelogram(5, 5);
Rectangle r = new Rectangle(3, 5);
Square s = new Square(4, 4);
Point p1 = new Point(0, 0);
Point p2 = new Point(5, 0);
Point p3 = new Point(1, 2);
Point p4 = new Point(4, 2);
Point[] points = new Point[4];
points[0] = p1;
points[1] = p2;
points[2] = p3;
points[3] = p4;
Trapezoid t = new Trapezoid(points);
System.out.println("Parallelogram\n\tArea: " + p.getArea());
System.out.println("Rectangle\n\tArea: " + r.getArea());
System.out.println("Squre\n\tArea: " + s.getArea());
System.out.println("Trapezoid\n\tArea: " + t.getArea());
}
}

@ -0,0 +1,13 @@
/**
* Created by noah on 5/22/2016.
*/
public class Parallelogram extends Quadrilateral {
public Parallelogram(int width, int height) {
super(width, height);
}
public double getArea() {
return Math.abs(points[0].getX() - points[1].getX()) * Math.abs(points[0].getY() - points[3].getY());
}
}

@ -0,0 +1,32 @@
/**
* Created by noah on 5/22/2016.
*/
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0,0);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

@ -0,0 +1,31 @@
/**
* Created by noah on 5/22/2016.
*/
public class Quadrilateral {
protected Point[] points;
public Quadrilateral(Point[] points) {
this.points = points;
}
public Quadrilateral(int width, int height) {
Point p1 = new Point(0,0);
Point p2 = new Point(width, 0);
Point p3 = new Point(0, height);
Point p4 = new Point(width, height);
points = new Point[4];
points[0] = p1;
points[1] = p2;
points[2] = p3;
points[3] = p4;
}
public Quadrilateral() {
points = new Point[4];
}
//It is possible to calculate the area of an arbitrary 4 sided shape, but it involves more complex math than I am
//willing to code right now.
}

@ -0,0 +1,9 @@
/**
* Created by noah on 5/22/2016.
*/
public class Rectangle extends Parallelogram {
public Rectangle(int width, int height) {
super(width, height);
}
//The way to calculate area is no different from a parallelogram so no overrided methods :(
}

@ -0,0 +1,10 @@
/**
* Created by noah on 5/22/2016.
*/
public class Square extends Rectangle {
//The way to calculate area is no different from a parallelogram so no overrided methods :(
public Square(int width, int height) {
super(width, height);
}
}

@ -0,0 +1,14 @@
/**
* Created by noah on 5/22/2016.
*/
public class Trapezoid extends Quadrilateral {
public Trapezoid(Point[] points) {
super(points);
}
public double getArea() {
return (Math.abs(points[0].getX() - points[1].getX()) + Math.abs(points[2].getX() - points[3].getX())/2 * (Math.abs(points[0].getY() - points[3].getY())));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inherit-compiler-output="true" inheritJdk="true">
<output-test url="file://$MODULE_DIR$/out/test/School"/>
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</component>

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="eclipse" classpath-dir="$MODULE_DIR$" type="JAVA_MODULE" version="4" />

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<eclipse-userlibraries />

@ -0,0 +1,49 @@
// Card class represents a playing card.
public class Card
{
private final String face; // face of card
private final String suit; // suit of card
// constructor initializes face and suit
public Card(String cardFace, String cardSuit)
{
face = cardFace; // initialize face of card
suit = cardSuit; // initialize suit of card
}
public String getFace()
{
// *** code goes here ***
return face;
}
public String getSuit()
{
// *** code goes here ***
return suit;
}
// return String representation of Card
public String toString()
{
return face + " of " + suit;
}
} // end class Card
/**************************************************************************
* (C) Copyright 1992-2015 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

@ -0,0 +1,104 @@
// DeckOfCards class represents a deck of playing cards.
import java.util.Random;
public class DeckOfCards
{
private static final String[] faces =
{"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private static final String[] suits =
{"Hearts", "Diamonds", "Clubs", "Spades"};
// random number generator
private static final Random randomNumbers = new Random();
private static final int NUMBER_OF_CARDS = 52; // constant number of cards
private Card[] deck; // array of Card objects
private int currentCard; // the index of next Card to be dealt
// constructor fills deck of cards
public DeckOfCards()
{
deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects
currentCard = 0; // initialize currentCard
// populate deck with Card objects
for (int count = 0; count < deck.length; count++)
deck[count] =
new Card(faces[count % 13], suits[count / 13]);
}
// shuffle deck of cards with one-pass algorithm
public void shuffle()
{
currentCard = 0; // initialize currentCard
// for each card, pick another random card (0-51) and swap them
for (int first = 0; first < deck.length; first++)
{
// random 0-51
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
//swap
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
// deal one card
public Card dealCard()
{
// determine whether cards remain to be dealt
if (currentCard < deck.length)
return deck[currentCard++]; // return current Card in array
else
return null; // return null to indicate that all cards were dealt
}
//This really doesn't make sense to implement here, as the deck itself doesn't determine if a hand has three of a kind
// the player does. Also, ew O(n^2)
public boolean containsThreeOfKind(Card[] cards) {
for(int i = 0; i < cards.length; i++) {
int duplicates = 0;
for(int z = 0; z < cards.length; z++) {
if(cards[i].getFace().equals(cards[z].getFace()))
duplicates++;
}
if (duplicates >= 3)
return true;
}
return false;
}
public boolean containsFlush(Card[] cards) {
for(int i = 0; i < cards.length; i++) {
int duplicates = 0;
for(int z = 0; z < cards.length; z++) {
if(cards[i].getSuit().equals(cards[z].getSuit()))
duplicates++;
}
if (duplicates >= 5)
return true;
}
return false;
}
} // end class DeckOfCards
/**************************************************************************
* (C) Copyright 1992-2015 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

@ -0,0 +1,84 @@
// Card shuffling and dealing application.
public class DeckOfCardsTest
{
// execute application
public static void main(String[] args)
{
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order
// display all 52 cards in the order dealt
/*for (int i = 0; i < 52; i++)
{
// deal and display a Card
System.out.printf("%-19s", myDeckOfCards.dealCard());
if (i % 4 == 0) // display in four columns
{
System.out.println();
}
} */
// deal a hand (get first five cards)
// *** code goes here ***
Card[] hand = new Card[5];
for(int i = 0; i < hand.length; i++) {
hand[i] = myDeckOfCards.dealCard();
}
// display result
System.out.println("\nHand contains:");
// *** code goes here ***
for(Card c : hand) {
System.out.println(c.toString());
}
// *** display output stating whether hand contains three of kind, flush, or neither
System.out.println("Contains 3 of a kind: " + myDeckOfCards.containsThreeOfKind(hand));
System.out.println("Contains a flush: " + myDeckOfCards.containsFlush(hand));
}
//Run this method to find flushes. That statistical chances of getting a flush are pretty small, so I was having
//trouble verifying that my code worked
public static void findFlush() {
for(int ii = 0; ii < 10000; ii++) {
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order
Card[] hand = new Card[5];
for (int i = 0; i < hand.length; i++) {
hand[i] = myDeckOfCards.dealCard();
}
if(myDeckOfCards.containsFlush(hand)) {
// display result
System.out.println("\nHand contains:");
// *** code goes here ***
for (Card c : hand) {
System.out.println(c.toString());
}
}
}
}
} // end class DeckOfCardsTest
/**************************************************************************
* (C) Copyright 1992-2015 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

@ -0,0 +1,177 @@
/**
* Created by noah on 5/13/2016.
*/
public class HugeInteger {
private final int DIGITS_LENGTH = 40;
private int[] 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;
private int trimZeroes;
public static void main(String args[]) {
HugeInteger h1 = new HugeInteger("19641684156196898849849161");
HugeInteger h2 = new HugeInteger("651651651321616516551321649846123");
HugeInteger h3 = new HugeInteger("651651651321616516551321649846123");
HugeInteger h4 = new HugeInteger("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 HugeInteger() {
digits = new int[DIGITS_LENGTH];
digitCount = 0;
trimZeroes = 0;
}
public HugeInteger(String s) {
this();
parse(s);
}
public void parse(String input) throws NumberFormatException{
if(input.length() > DIGITS_LENGTH)
throw new NumberFormatException("Too many digits");
int startpos = DIGITS_LENGTH - input.length();
digitCount = input.length();
trimZeroes = DIGITS_LENGTH - digitCount - 1 ;
int z = 0;
for(int i = startpos; i < DIGITS_LENGTH ; i++) {
digits[i] = Character.getNumericValue(input.charAt(z));
z++;
}
}
//We do this so we can compare individual indexes for when comparing HugeInteger instances
public int getDigit(int index) {
if (index >= 0 && index < DIGITS_LENGTH)
return digits[index];
else
throw new ArrayIndexOutOfBoundsException("Not a valid index");
}
//Returns the number of leading zeroes in the array
public int getDigitCount() {
return digitCount;
}
public boolean isEqualTo(HugeInteger h) {
if (getDigitCount() != h.getDigitCount())
return false;
for (int i = 0; i < DIGITS_LENGTH; i++ ){
if(digits[i] != h.getDigit(i))
return false;
}
return true;
}
public boolean isNotEqualTo(HugeInteger h) {
if (getDigitCount() != h.getDigitCount())
return true;
for (int i = 0; i < DIGITS_LENGTH; i++ ){
if(digits[i] != h.getDigit(i))
return true;
}
return false;
}
public boolean isGreaterThan(HugeInteger 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 < 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(HugeInteger 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 = trimZeroes; 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(HugeInteger 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 < 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(HugeInteger 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 < 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 < DIGITS_LENGTH; i++) {
if(digits[i] != 0)
return false;
}
return true;
}
//I do not feel ambitious. This is tedious. No multiply/divide/remainder functions.
}

@ -0,0 +1,189 @@
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.
}

@ -0,0 +1,50 @@
import java.util.Scanner;
/**
* Created by noah on 5/13/2016.
*/
public class Prime {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("PLease type an integer number: ");
int input = scanner.nextInt();
System.out.println("The number " + input + " is prime?: " + isPrime(input));
}
//Based off a primality test algorithm found at https://en.wikipedia.org/wiki/Primality_test
public static boolean isPrime(int prime) {
if (prime <= 1)
return false;
else if (prime <= 3)
return true;
else if (prime % 2 == 0 || prime % 3 == 0)
return false;
int i = 5;
while (i * i <= prime) {
if(prime % i == 0 || prime % (i + 2) == 0)
return false;
i += 6;
}
return true;
}
public static boolean isPrime2(int prime) {
if (prime <= 1)
return false;
else if (prime <= 3)
return true;
else if (prime % 2 == 0 || prime % 3 == 0)
return false;
int sqrt = (int)Math.sqrt(prime);
for (int i = 2; i < sqrt; i++) {
if(prime % i == 0) {
return false;
}
}
return true;
}
}
Loading…
Cancel
Save