-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71099b6
commit 65b529a
Showing
44 changed files
with
82,534 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package Animals; | ||
|
||
// Animal is now an abstract class | ||
public abstract class Animal { | ||
|
||
private int age; // VS private int age; | ||
|
||
public Animal(int age) { | ||
this.age = age; | ||
System.out.println("An animal has been created!"); | ||
} | ||
|
||
// Abstract Method | ||
public abstract void eat(); | ||
|
||
// Non-Abstract Method | ||
public void sleep() { | ||
System.out.println("An animal is sleeping."); | ||
} | ||
|
||
// Getters | ||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Dog d = new Dog(); | ||
Cat c = new Cat(); | ||
d.eat(); | ||
c.eat(); | ||
d.sleep(); | ||
c.sleep(); | ||
|
||
// Casting | ||
Object dog = new Dog(); | ||
Dog realDog = (Dog) dog; | ||
realDog.eat(); | ||
realDog.ruff(); | ||
|
||
// More Casting | ||
Object str = "est"; | ||
// On the right side of the equals sign you put the datatype in parenthesis | ||
String realS = (String) str; | ||
realS.getBytes(); | ||
|
||
// What happens when... | ||
Dog doggy = new Dog(); | ||
if (d instanceof Animal) { | ||
Animal animal = (Animal) doggy; | ||
animal.sleep(); | ||
} | ||
doggy.sleep(); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package Animals; | ||
|
||
public class Cat extends Animal { | ||
|
||
public Cat() { | ||
super(7); | ||
System.out.println("A cat has been created."); | ||
} | ||
|
||
public void eat() { | ||
System.out.println("A cat is eating."); | ||
} | ||
|
||
// Override the Sleep method in Animal | ||
public void sleep() { | ||
System.out.println("A cat is sleeping."); | ||
} | ||
// Method | ||
public void meow() { | ||
System.out.println("A cat meows!"); | ||
} | ||
|
||
// Method | ||
public void prance() { | ||
System.out.println("A cat is prancing."); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package Animals; | ||
|
||
public class Dog extends Animal { | ||
|
||
// Constructor | ||
public Dog() { | ||
super(15); //References Animal | ||
System.out.println("A dog has been created."); | ||
} | ||
|
||
// This is necessary becuase the Animal Class is abstract | ||
public void eat(){ | ||
System.out.println("A dog is eating"); | ||
} | ||
|
||
// Override the Sleep method in Animal | ||
public void sleep() { | ||
System.out.println("A dog is sleeping."); | ||
} | ||
|
||
// Method | ||
public void ruff() { | ||
System.out.println("The dog says ruff"); | ||
} | ||
|
||
//Method | ||
public void run() { | ||
System.out.println("A dog is running"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.util.Arrays; | ||
|
||
public class ArrayPractice { | ||
|
||
// "<E>" can be anything (int, String, Boolean) | ||
public static <E> void printArray(E[] array) { | ||
for (E element : array) { | ||
System.out.println(element + " "); | ||
} | ||
System.out.println(); | ||
} | ||
public static void main(String[] args) { | ||
// Location ID: 0,1,2,3,4 | ||
// Example Array [0,5,2,6,7] | ||
|
||
// First way to declare arrays | ||
Integer[] intArray1; | ||
|
||
//Second way to declare arrays | ||
Integer[] intArray2 = new Integer[4]; | ||
|
||
//Third way to declare arrays | ||
Integer[] intArray3 = {5,0,2,6,7}; | ||
|
||
String[] shoppingList = {"bananas", "apples", "celery"}; | ||
|
||
// Setting variables in an array | ||
intArray2[0] = 10; // Set the first number in array 2 to the number 10. | ||
intArray2[1] = 20; | ||
intArray2[2] = 30; | ||
intArray2[3] = 40; | ||
|
||
//Print out arrays | ||
System.out.println(Arrays.toString(intArray2)); | ||
System.out.println(Arrays.toString(intArray3)); | ||
System.out.println(); | ||
|
||
// Custom print out arrays | ||
printArray(intArray2); | ||
printArray(intArray3); | ||
System.out.println(); | ||
|
||
// Retrieve objects | ||
System.out.println(intArray2[3]); | ||
System.out.println(); | ||
|
||
//Given Functions | ||
Arrays.sort(intArray3); // Sorting Arrays | ||
printArray(intArray3); | ||
|
||
System.out.println("Special For Loop:"); | ||
// Special for loop: foreach | ||
for (String s : shoppingList) { | ||
System.out.println(s); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package BinarySearchTree; | ||
|
||
public class BinarySearchTree { | ||
public static void main(String[] args) throws Exception{ | ||
|
||
EmptyBST e = new EmptyBST(); | ||
NonEmptyBST n = new NonEmptyBST(5); | ||
Testers.checkIsEmpty(e); | ||
Testers.checkIsEmpty(n); | ||
Testers.checkAddMemberCardinality(e, 5); | ||
Testers.checkAddMemberCardinality(n, 5); | ||
Testers.checkAddMemberCardinality(n, 6); | ||
|
||
int tests = 1000; | ||
for (int i = 0; i < tests; i++) { | ||
Tree t; | ||
if (i % 10 == 0) { | ||
t = Testers.rndTree(0); | ||
} else { | ||
t = Testers.rndTree(10); | ||
} | ||
Testers.checkAddMemberCardinality(t, i); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package BinarySearchTree; | ||
|
||
public class EmptyBST<D extends Comparable> implements Tree<D>{ | ||
|
||
public EmptyBST() { | ||
|
||
} | ||
|
||
public boolean isEmpty() { | ||
return true; | ||
} | ||
|
||
public int cardinality() { | ||
return 0; | ||
} | ||
|
||
public boolean member(D elt) { | ||
return false; | ||
} | ||
|
||
public NonEmptyBST<D> add(D elt) { | ||
return new NonEmptyBST<D>(elt); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package BinarySearchTree; | ||
|
||
public class NonEmptyBST<D extends Comparable> implements Tree<D> { | ||
|
||
// Fields and input areas for the Non Empty BST | ||
D data; | ||
Tree<D> left; | ||
Tree right; | ||
|
||
// "elt" is short for element | ||
public NonEmptyBST(D elt) { | ||
data = elt; | ||
left = new EmptyBST<D>(); | ||
right = new EmptyBST<D>(); | ||
} | ||
|
||
public NonEmptyBST(D elt, Tree<D> leftTree, Tree<D> rightTree) { | ||
data = elt; | ||
left = leftTree; | ||
right = rightTree; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
public int cardinality() { | ||
return 1 + left.cardinality() + right.cardinality(); | ||
} | ||
|
||
public boolean member(D elt) { | ||
if (data == elt) { | ||
return true; | ||
} else { | ||
if (elt.compareTo(data) < 0) { | ||
return left.member(elt); | ||
} else { | ||
return right.member(elt); | ||
} | ||
} | ||
} | ||
|
||
public NonEmptyBST<D> add(D elt) { | ||
if (data == elt) { | ||
return this; | ||
} else { | ||
if (elt.compareTo(data) < 0) { | ||
return new NonEmptyBST(data, left.add(elt), right); | ||
} else { | ||
return new NonEmptyBST(data, left, right.add(elt)); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package BinarySearchTree; | ||
|
||
import java.util.Random; | ||
|
||
public class Testers { | ||
|
||
// Random Integers | ||
public static int rndInt(int min, int max) { | ||
Random rand = new Random(); | ||
return rand.nextInt((max - min) + 1) + min; | ||
// Min = 5 and Max = 15 | ||
// Example: 15-5 = 10 + 1 == 11 --> 0 to 10 | ||
// + 5 to whatever the random number is | ||
} | ||
|
||
// Random Binary Search Trees | ||
public static Tree rndTree(int count) { | ||
if (count == 0) { | ||
return new EmptyBST(); | ||
} else { | ||
return rndTree(count - 1).add(rndInt(0,50)); | ||
} | ||
} | ||
|
||
// x + (x*2) = x + x *2 { This is an example of a test. We know these are equal, so if for some reason they were not we have a problem.} | ||
|
||
public static void checkIsEmpty(Tree t) throws Exception{ | ||
// if the tree t is an instance of EmptyBST then t.isEmpty should return True | ||
// if the tree t is an instance of NonEmptyBST then t.isEmpty should return False | ||
if (t instanceof EmptyBST) { | ||
if (!t.isEmpty()) { | ||
throw new Exception("All is not good. The tree is an EmptyBST and it is non-empty."); | ||
} else if (t instanceof NonEmptyBST) { | ||
if (t.isEmpty()) { | ||
throw new Exception("All is not good, the tree is a NonEmptyBST and it is empty"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void checkAddMemberCardinality(Tree t, int x) throws Exception { | ||
int nT = (t.add(x)).cardinality(); | ||
// 1. Either something was added and the cardinality increased by one. | ||
if (nT == (t.cardinality() + 1)) { | ||
if (t.member(x)) { | ||
throw new Exception("The cardinality increased by 1, but the thing that was added was already a member of the tree"); | ||
} | ||
} // 2. OR the thing that was added was already there and not really added so the cardinality stayed the same. | ||
else if (nT == t.cardinality()) { | ||
if (!t.member(x)) { | ||
throw new Exception("The cardinality didn't increase by 1, but we added a new thing"); | ||
} else { | ||
throw new Exception("Something is wrong with our program."); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package BinarySearchTree; | ||
|
||
// Allowing for generics by allowing D to extend Comparable | ||
public interface Tree<D extends Comparable> { | ||
|
||
public boolean isEmpty(); | ||
public int cardinality(); | ||
public boolean member(D element); | ||
public NonEmptyBST<D> add(D elt); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package CalendarPractice; | ||
|
||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.text.SimpleDateFormat; | ||
|
||
public class CalendarPractice { | ||
|
||
public static void main(String[] args) { | ||
Calendar cal = Calendar.getInstance(); | ||
cal.add(Calendar.DATE, 125); | ||
System.out.println(cal.getTime()); | ||
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); | ||
String formatted = format1.format(cal.getTime()); | ||
System.out.println(formatted); | ||
} | ||
} |
Oops, something went wrong.