From 92749dd8d130bea175752555c734d73b687969e7 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Wed, 19 Apr 2023 16:00:40 -0500 Subject: [PATCH] MP5 Progress --- .../mp5_calebfontenot/Large.java | 6 +- .../mp5_calebfontenot/NormalizeDatabase.java | 147 ++++++++++++------ .../mp5_calebfontenot/ShuffleArrayList.java | 44 +++--- .../mp5_calebfontenot/SortArrayList.java | 47 ++++++ 4 files changed, 174 insertions(+), 70 deletions(-) create mode 100644 Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/SortArrayList.java diff --git a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Large.java b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Large.java index 26dcc7e..4d54beb 100644 --- a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Large.java +++ b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Large.java @@ -35,7 +35,11 @@ public class Large { System.out.println("Unable to read file"); } for (int i = 0; i < firstNameArr.size(); ++i) { - System.out.println("first name:" + firstNameArr.get(i)); + System.out.println("first name :" + firstNameArr.get(i)); + System.out.println("last name: " + lastNameArr.get(i)); + System.out.println("job title: " + jobTitleArr.get(i)); + System.out.println("salary: " + salaryArr.get(i)); + System.out.println("--------------------------------------"); } } } diff --git a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/NormalizeDatabase.java b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/NormalizeDatabase.java index e02a825..c1c4981 100644 --- a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/NormalizeDatabase.java +++ b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/NormalizeDatabase.java @@ -20,86 +20,133 @@ import java.util.Set; * * @author ASDV2 */ -public class NormalizeDatabase { - - /** - * Finds the closure of a set of attributes given a set of FDs of a relation R - * - * @param attributes attributes to find their closure - * @param fds set of FDs of relation R - * @return the closure of the parameter attributes. - */ - public static String closure(String attributes, ArrayList fds) +public class NormalizeDatabase +{ +/**Finds the closure of a set of attributes given a set of FDs of a relation R + * + * @param attributes attributes to find their closure + * @param fds set of FDs of relation R + * @return the closure of the parameter attributes. + */ + public static String closure(String attributes, ArrayList fds ) { attributes = attributes.toUpperCase(); - String closurePrevious = attributes; + for (int j = 0; j < fds.size(); ++j) + { + FD capitalsFD = new FD(fds.get(j).getLhs().toUpperCase(), + fds.get(j).getRhs().toUpperCase()); + fds.set(j, capitalsFD); + } + // 1. Set x+ = x String closure = attributes; - for (int i = 0; i < fds.size(); ++i) { + String closurePreveious = attributes; +// 2. Starting with x+ apply each FD xF —> y in F where +// xF belongs in closure x+ but but the rhs y is not already in x+, to find determined +// attributes y +// 3. x+ = x+ U y - if (closure.contains(fds.get(i).getRhs())) { - continue; + while (true) + { + + for (int i = 0; i < fds.size(); ++i) + { + if (closure.contains(fds.get(i).getRhs())) + continue; + // if the left hand side of the FD is contained in the closure + // then add to the closure the RHS of the FD + if (closure.contains(fds.get(i).getLhs())) + closure += fds.get(i).getRhs(); } - FD capitalFD = new FD(fds.get(i).getLhs().toUpperCase(), - fds.get(i).getRhs().toUpperCase()); - } - //1. Set x+ = x - //2. Starting with x+ apply each FD xF —> y in F where - //xF belongs in closure x+, but whre the rhs y is not already in the closure x+ - //to find the determined attribute - //3. x+ = x+ U y - while (true) { - for (int i = 0; i < fds.size(); ++i) { - //the LHS of the FD is contained in the closure - //then add to the closure the RHS of the FD - if (attributes.contains(fds.get(i).getLhs())) { - attributes += fds.get(i).getRhs(); - } - } - if (closurePrevious.equals(closure)) { + if (closurePreveious.equals(closure)) break; - } else { - closurePrevious = closure; - } + else + closurePreveious = closure; } - //4, If y not empty go to (2) - //5. Return x+ +// 4, If y not empty goto (2) +// 5. Return x+ return closure; - } + } /** - * Eliminates redundant attributes from the LHS of each FD of a set of FDs given as parameters. + * Eliminates redundant attributes from the LHS of each FD of a set of FDs + * given as parameters. * * @param fds the set of FDs to eliminate the redundancy * @return and ArrayList with no redundancy on LHS of each FD. */ public static ArrayList eliminateRedundantAttributes(ArrayList fds) { - return null; + for (int j = 0; j < fds.size(); ++j) + { + int s = fds.get(j).getLhs().length(); + if (s < 2) + { + continue; + } + else + { + String fl = fds.get(j).getLhs().substring(0); + ArrayList fFD = new ArrayList(); + String s1 = " "; + if (fds.get(j).getLhs().length() == 2) + { + + s1 = fds.get(j).getLhs().substring(1); + fFD.add(new FD (fds.get(j).getLhs().substring(1), fds.get(j).getLhs())); + System.out.println("closure " + closure(s1, fds)); + System.out.println("Attribute " + s1); + System.out.println("final (removed " + fFD); + } +// else if (fds.get(j).getLhs().charAt(1) == 3) +// { +// lFD.add(fds.get(j).getLhs().charAt(1)); +// lFD.add(fds.get(j).getLhs().charAt(2)); +// } +// else if (fds.get(j).getLhs().charAt(1) == 4) +// { +// lFD.add(fds.get(j).getLhs().charAt(1)); +// lFD.add(fds.get(j).getLhs().charAt(2)); +// lFD.add(fds.get(j).getLhs().charAt(3)); +// } + if (closure(s1, fds).contains(fl)) + { + return fFD; + } + else + { + return fds; + } + } + } + return null; } - public static void main(String[] args) { ArrayList fds = new ArrayList(); - FD fd = new FD("a", "bc"); + FD fd = new FD("a", "BC"); FD[] fdDecomposed = fd.decomposeRightHandSide(); - for (int i = 0; i < fdDecomposed.length; ++i) { - fds.add(new FD(fdDecomposed[i].getLhs(), fdDecomposed[i].getRhs())); + for (int i = 0; i < fdDecomposed.length; ++i) + { + fds.add( new FD(fdDecomposed[i].getLhs(), fdDecomposed[i].getRhs())); } - fds.add(new FD("a", "bc")); - fds.add(new FD("b", "c")); + + fds.add(new FD("B", "C")); fds.add(new FD("AB", "B")); + fds.add(new FD("C", "A")); System.out.println(fds); - System.out.println(closure("ac", fds)); - + System.out.println(closure("b", fds)); + System.out.println(eliminateRedundantAttributes(fds)); /* TEST it with Let F1 = {1. A -> BC 2. B -> C, 3. AB -> D }. Attribute B is extraneous in FD 3 AB -> D - */ - /* + */ + + + /* F2 = { 1. AB -> C, 2. C -> A, 3. BC -> D, @@ -111,6 +158,6 @@ public class NormalizeDatabase { 9. CG -> D, 10. CE -> A, 11. CE -> G} - */ + */ } } diff --git a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java index b9a7490..844b4ef 100644 --- a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java +++ b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java @@ -12,24 +12,31 @@ import java.util.*; */ public class ShuffleArrayList { - public static void shuffle(ArrayList list) - { - ArrayList selectedIndicies = new ArrayList(); - for (int i = 0; i < list.size(); ++i) { - Random rng = new Random(); - Number random1; - do { - random1 = rng.nextInt(list.size()); - } while (selectedIndicies.contains(random1)); - selectedIndicies.add((int) random1); - System.out.println(random1 + ", " + i); - Number temp = list.get((int) random1); - list.set(i, temp); - } - } +public static void shuffle(ArrayList list) { + // Create a new Random object. + Random rng = new Random(); + // Create an ArrayList to store the indices of the elements that have been selected. + ArrayList selectedIndices = new ArrayList(); - public static String checkForDuplicates(ArrayList list) - { + // Loop through each element in the list. + for (int i = 0; i < list.size(); ++i) { + // Generate a random index that has not been selected before. + int randomIndex; + do { + randomIndex = rng.nextInt(list.size()); // Generate a random integer between 0 (inclusive) and the size of the list (exclusive). + } while (selectedIndices.contains(randomIndex)); // Repeat until an unselected index is found. + selectedIndices.add(randomIndex); // Add the selected index to the list of selected indices. + //System.out.println(randomIndex + ", " + i); + // Swap the element at the random index with the element at the current index of the loop. + // This shuffles the list by randomly selecting an element to swap with the current element at each iteration. + Number temp = list.get(randomIndex); // Save the element at the random index to a temporary variable. + list.set(randomIndex, list.get(i)); // Overwrite the element at the random index with the element at the current index of the loop. + list.set(i, temp); // Set the current index of the loop to the saved element, effectively swapping the two elements. + } +} + + + public static String checkForDuplicates(ArrayList list) { // Ensure Array does not include repeat numbers. boolean repeatNumber = false; for (int i = 0; i < list.size(); ++i) { @@ -49,8 +56,7 @@ public class ShuffleArrayList { } } - public static void main(String[] args) - { + public static void main(String[] args) { final int ARRAY_SIZE = 50; ArrayList list = new ArrayList<>(); for (int i = 0; i < ARRAY_SIZE; ++i) { diff --git a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/SortArrayList.java b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/SortArrayList.java new file mode 100644 index 0000000..dfb52ea --- /dev/null +++ b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/SortArrayList.java @@ -0,0 +1,47 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.mp5_calebfontenot; + +import java.math.BigDecimal; +import java.util.ArrayList; + +/** + * + * @author caleb + */ +public class SortArrayList { + + public static void sort(ArrayList list) { + // Selection sort implementation for ArrayLists. + for (int i = 0; i < list.size(); ++i) { + for (int j = i + 1; j < list.size(); ++j) { + // BigDecimal should work for any type. Have not confirmed this. + Number numI = list.get(i); + Number numJ = list.get(j); + BigDecimal bigNumI = new BigDecimal(list.get(i).toString()); + BigDecimal bigNumJ = new BigDecimal(list.get(j).toString()); + if (bigNumI.compareTo(bigNumJ) == 1) { + Number tmp = numI; + list.set(i, numJ); + list.set(j, tmp); + } + } + } + } + + public static void main(String[] args) { + final int ARRAY_SIZE = 50; + ArrayList list = new ArrayList<>(); + for (int i = 0; i < ARRAY_SIZE; ++i) { + list.add((i) + Math.random()); // Fill ArrayList with sequential numbers. + } + System.out.println(list); + ShuffleArrayList.shuffle(list); // Use our shuffle method from earlier + System.out.println(list); + sort(list); + System.out.println(list); + + } +}