diff --git a/other/Sets/.gitignore b/other/Sets/.gitignore new file mode 100644 index 0000000..b1001a6 --- /dev/null +++ b/other/Sets/.gitignore @@ -0,0 +1 @@ +.idea/** diff --git a/other/Sets/Sets.iml b/other/Sets/Sets.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/other/Sets/Sets.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/other/Sets/out/production/Sets/lab07b.dat b/other/Sets/out/production/Sets/lab07b.dat new file mode 100644 index 0000000..ced64d4 --- /dev/null +++ b/other/Sets/out/production/Sets/lab07b.dat @@ -0,0 +1,6 @@ +1 5 9 4 6 8 12 +3 5 7 17 29 4 6 56 72 +3 6 12 2 28 6 +4 4 4 4 4 4 4 4 +1 1 1 1 1 1 1 1 +1 2 3 4 5 6 7 8 9 \ No newline at end of file diff --git a/other/Sets/out/production/Sets/lab07c.dat b/other/Sets/out/production/Sets/lab07c.dat new file mode 100644 index 0000000..f21cba7 --- /dev/null +++ b/other/Sets/out/production/Sets/lab07c.dat @@ -0,0 +1,6 @@ +1 2 3 4 5 +4 5 6 7 8 +10 11 12 13 14 15 16 17 +11 13 15 17 19 21 23 +4 5 5 6 76 7 7 8 8 8 8 8 +23 3 4 3 5 3 53 5 46 46 4 6 5 3 4 diff --git a/other/Sets/questions/lab07a.doc b/other/Sets/questions/lab07a.doc new file mode 100644 index 0000000..832ab19 Binary files /dev/null and b/other/Sets/questions/lab07a.doc differ diff --git a/other/Sets/questions/lab07b.doc b/other/Sets/questions/lab07b.doc new file mode 100644 index 0000000..e45e7b3 Binary files /dev/null and b/other/Sets/questions/lab07b.doc differ diff --git a/other/Sets/questions/lab07c.doc b/other/Sets/questions/lab07c.doc new file mode 100644 index 0000000..8e137d9 Binary files /dev/null and b/other/Sets/questions/lab07c.doc differ diff --git a/other/Sets/src/Lab07a.java b/other/Sets/src/Lab07a.java new file mode 100644 index 0000000..8a1c6c7 --- /dev/null +++ b/other/Sets/src/Lab07a.java @@ -0,0 +1,18 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 15 September 2020 +//Class - Computer Science II PreAP +//Lab - Sets 07a + +import static java.lang.System.out; + +public class Lab07a +{ + public static void main(String[] args) + { + String list = "a b c d e f g h a b c d e f g h i j k"; + System.out.println("Original List : " + list); + out.println("Uniques : " + UniquesDupes.getUniques(list)); + out.println("Dupes : " + UniquesDupes.getDupes(list) + "\n\n"); + } +} \ No newline at end of file diff --git a/other/Sets/src/Lab07b.java b/other/Sets/src/Lab07b.java new file mode 100644 index 0000000..bbaf5d4 --- /dev/null +++ b/other/Sets/src/Lab07b.java @@ -0,0 +1,67 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 15 September 2020 +//Class - Computer Science II PreAP +//Lab - Sets 07b + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; +import java.util.Set; +import java.util.TreeSet; + +import static java.lang.System.out; + +public class Lab07b +{ + public static void main(String[] args) throws IOException + { + // Get input data + Scanner scanner = new Scanner(new File("./src/lab07b.dat")); + ArrayList input = new ArrayList<>(); + while (scanner.hasNextInt()) input.add(scanner.nextInt()); + + // Instantiate output arrays + Set evens = new TreeSet<>(); + Set odds = new TreeSet<>(); + Set perfects = new TreeSet<>(); + + // Read and separate + for (Integer number : input) { + if (number % 2 == 0) + evens.add(number); + else + odds.add(number); + + if (isPerfect(number)) + perfects.add(number); + } + + out.println("ODDS: " + odds + "\nEvens: " + evens + "\nPerfects: " + perfects); + } + + public static boolean isPerfect(int number) + { + int sum = 0; + Set divisors = getDivisors(number); + for (Integer divisor : divisors) sum += divisor; + return number == sum; + } + + public static Set getDivisors(int number) + { + Set divisors = new TreeSet<>(); + divisors.add(1); + + for (int i = 2; i <= Math.sqrt(number); i++) { + double quotient = number / (float) i; + if (quotient % 1 == 0) { + divisors.add(i); + divisors.add((int) quotient); + } + } + + return divisors; + } +} \ No newline at end of file diff --git a/other/Sets/src/Lab07c.java b/other/Sets/src/Lab07c.java new file mode 100644 index 0000000..4b8dc6d --- /dev/null +++ b/other/Sets/src/Lab07c.java @@ -0,0 +1,85 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 15 September 2020 +//Class - Computer Science II PreAP +//Lab - Sets 07c + +import java.io.File; +import java.io.IOException; +import java.util.Scanner; +import java.util.Set; +import java.util.TreeSet; + +import static java.lang.System.out; + +public class Lab07c +{ + public static void main(String[] args) throws IOException + { + // Get input data + Scanner scanner = new Scanner(new File("./src/lab07b.dat")); + while (scanner.hasNextLine()) { + Set a = getIntegerSet(scanner.nextLine()); + Set b = getIntegerSet(scanner.nextLine()); + + out.println("Set one: " + a + "\nSet two: " + b); + out.println("\nunion - " + union(a, b)); + out.println("intersection - " + intersection(a, b)); + out.println("difference A-B - " + difference(a, b)); + out.println("difference B-A - " + difference(b, a)); + out.println("symmetric difference - " + symmetric(a, b)); + + // Don't add unnecessary newline on the final sets processing + if(scanner.hasNextLine()) + out.println(); + } + } + + /** + * @param line A string containing integers + * @return a Set containing parsed integers from the given String + */ + public static Set getIntegerSet(String line) { + Scanner scanner = new Scanner(line); + Set set = new TreeSet<>(); + while (scanner.hasNextInt()) + set.add(scanner.nextInt()); + return set; + } + + /** + * @return Integers that are in either set. + */ + public static Set union(Set a, Set b) { + Set union = new TreeSet<>(a); + union.addAll(b); + return union; + } + + /** + * @return Integers that are in both Sets + */ + public static Set intersection(Set a, Set b) { + Set intersection = new TreeSet<>(a); + intersection.retainAll(b); + return intersection; + } + + /** + * @return Integers that are in Set A but not Set B + */ + public static Set difference(Set a, Set b) { + Set difference = new TreeSet<>(a); + difference.removeAll(b); + return difference; + } + + /** + * @return Integers that appear in either set but NOT both. + */ + public static Set symmetric(Set a, Set b) { + Set symmetric = new TreeSet<>(union(a, b)); + symmetric.removeAll(intersection(a, b)); + return symmetric; + } +} \ No newline at end of file diff --git a/other/Sets/src/MathSet.java b/other/Sets/src/MathSet.java new file mode 100644 index 0000000..70c0dfb --- /dev/null +++ b/other/Sets/src/MathSet.java @@ -0,0 +1,54 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 15 September 2020 +//Class - Computer Science II PreAP +//Lab - Sets 07c (MathSet) + +import java.util.Set; +import java.util.TreeSet; +import java.util.Arrays; +import static java.lang.System.*; + +public class MathSet +{ + private Set one; + private Set two; + + public MathSet() + { + } + + public MathSet(String o, String t) + { + } + + public Set union() + { + return null; + } + + public Set intersection() + { + return null; + } + + public Set differenceAMinusB() + { + return null; + } + + public Set differenceBMinusA() + { + return null; + } + + public Set symmetricDifference() + { + return null; + } + + public String toString() + { + return "Set one " + one + "\n" + "Set two " + two + "\n"; + } +} \ No newline at end of file diff --git a/other/Sets/src/OddEvenSets.java b/other/Sets/src/OddEvenSets.java new file mode 100644 index 0000000..350651e --- /dev/null +++ b/other/Sets/src/OddEvenSets.java @@ -0,0 +1,30 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - +//Date - +//Class - +//Lab - + +import java.util.Set; +import java.util.TreeSet; +import java.util.Arrays; +import java.util.Scanner; +import static java.lang.System.*; + +public class OddEvenSets +{ + private Set odds; + private Set evens; + + public OddEvenSets() + { + } + + public OddEvenSets(String line) + { + } + + public String toString() + { + return "ODDS : " + odds + "\nEVENS : " + evens + "\n\n"; + } +} \ No newline at end of file diff --git a/other/Sets/src/UniquesDupes.java b/other/Sets/src/UniquesDupes.java new file mode 100644 index 0000000..508c407 --- /dev/null +++ b/other/Sets/src/UniquesDupes.java @@ -0,0 +1,37 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - +//Date - +//Class - +//Lab - + +import java.util.Set; +import java.util.TreeSet; +import java.util.Arrays; +import java.util.ArrayList; + +import static java.lang.System.*; + +public class UniquesDupes +{ + public static Set getUniques(String input) + { + return new TreeSet(Arrays.asList(input.split("\\s+"))); + } + + public static Set getDupes(String input) + { + Set consider = new TreeSet(); // Values that have been seen at least once. + Set dupes = new TreeSet(); // Values that appear two or more times. + + for (String substring : input.split("\\s+")) { + // One has already been seen, add to dupes set. + if (consider.contains(substring)) + dupes.add(substring); + else + // Never been seen before, add to 'considering' set + consider.add(substring); + } + + return dupes; + } +} \ No newline at end of file diff --git a/other/Sets/src/lab07b.dat b/other/Sets/src/lab07b.dat new file mode 100644 index 0000000..ced64d4 --- /dev/null +++ b/other/Sets/src/lab07b.dat @@ -0,0 +1,6 @@ +1 5 9 4 6 8 12 +3 5 7 17 29 4 6 56 72 +3 6 12 2 28 6 +4 4 4 4 4 4 4 4 +1 1 1 1 1 1 1 1 +1 2 3 4 5 6 7 8 9 \ No newline at end of file diff --git a/other/Sets/src/lab07c.dat b/other/Sets/src/lab07c.dat new file mode 100644 index 0000000..f21cba7 --- /dev/null +++ b/other/Sets/src/lab07c.dat @@ -0,0 +1,6 @@ +1 2 3 4 5 +4 5 6 7 8 +10 11 12 13 14 15 16 17 +11 13 15 17 19 21 23 +4 5 5 6 76 7 7 8 8 8 8 8 +23 3 4 3 5 3 53 5 46 46 4 6 5 3 4