Sets questions, data, programs finished

This commit is contained in:
Xevion
2020-11-24 00:25:57 -06:00
parent ac2e42f06f
commit 9db1b5bd75
15 changed files with 327 additions and 0 deletions

1
other/Sets/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.idea/**

11
other/Sets/Sets.iml Normal file
View File

@@ -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>

View File

@@ -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

View File

@@ -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

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -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");
}
}

View File

@@ -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<Integer> input = new ArrayList<>();
while (scanner.hasNextInt()) input.add(scanner.nextInt());
// Instantiate output arrays
Set<Integer> evens = new TreeSet<>();
Set<Integer> odds = new TreeSet<>();
Set<Integer> 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<Integer> divisors = getDivisors(number);
for (Integer divisor : divisors) sum += divisor;
return number == sum;
}
public static Set<Integer> getDivisors(int number)
{
Set<Integer> 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;
}
}

View File

@@ -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<Integer> a = getIntegerSet(scanner.nextLine());
Set<Integer> 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<Integer> getIntegerSet(String line) {
Scanner scanner = new Scanner(line);
Set<Integer> set = new TreeSet<>();
while (scanner.hasNextInt())
set.add(scanner.nextInt());
return set;
}
/**
* @return Integers that are in either set.
*/
public static Set<Integer> union(Set<Integer> a, Set<Integer> b) {
Set<Integer> union = new TreeSet<>(a);
union.addAll(b);
return union;
}
/**
* @return Integers that are in both Sets
*/
public static Set<Integer> intersection(Set<Integer> a, Set<Integer> b) {
Set<Integer> intersection = new TreeSet<>(a);
intersection.retainAll(b);
return intersection;
}
/**
* @return Integers that are in Set A but not Set B
*/
public static Set<Integer> difference(Set<Integer> a, Set<Integer> b) {
Set<Integer> difference = new TreeSet<>(a);
difference.removeAll(b);
return difference;
}
/**
* @return Integers that appear in either set but NOT both.
*/
public static Set<Integer> symmetric(Set<Integer> a, Set<Integer> b) {
Set<Integer> symmetric = new TreeSet<>(union(a, b));
symmetric.removeAll(intersection(a, b));
return symmetric;
}
}

View File

@@ -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<Integer> one;
private Set<Integer> two;
public MathSet()
{
}
public MathSet(String o, String t)
{
}
public Set<Integer> union()
{
return null;
}
public Set<Integer> intersection()
{
return null;
}
public Set<Integer> differenceAMinusB()
{
return null;
}
public Set<Integer> differenceBMinusA()
{
return null;
}
public Set<Integer> symmetricDifference()
{
return null;
}
public String toString()
{
return "Set one " + one + "\n" + "Set two " + two + "\n";
}
}

View File

@@ -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<Integer> odds;
private Set<Integer> evens;
public OddEvenSets()
{
}
public OddEvenSets(String line)
{
}
public String toString()
{
return "ODDS : " + odds + "\nEVENS : " + evens + "\n\n";
}
}

View File

@@ -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<String> getUniques(String input)
{
return new TreeSet<String>(Arrays.asList(input.split("\\s+")));
}
public static Set<String> getDupes(String input)
{
Set<String> consider = new TreeSet<String>(); // Values that have been seen at least once.
Set<String> dupes = new TreeSet<String>(); // 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;
}
}

View File

@@ -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

View File

@@ -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