Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I cant get this java file to read heres the code and txt file to read the line words means the order in which its

I cant get this java file to read heres the code and txt file to read

the line words means the order in which its in the textfile get the code to read the files and submit new code and pic of output

line 1 ABCDEF line 2 AB C line 3 BC AD line 4 D E line 5 CF B

import java.io.File; import java.io.FileNotFoundException; import java.util.HashSet; import java.util.Scanner; import java.util.Set;

public class FDS2 { class FD { HashSet lhs; char rhs; public FD(HashSet l, char r) { lhs = l; rhs = r; } public boolean equals(Object obj) { FD fd2 = (FD) obj; return lhs.equals(fd2.lhs) && rhs == fd2.rhs; } public void printout() { for (char c : lhs) System.out.print(c); System.out.print(" "); System.out.print(rhs); System.out.println(); } }; HashSet R = new HashSet(); // all attributes HashSet F = new HashSet(); // the set of FDs public FDS2(String filename) { // 1. split FDs so each FD has a single // attribute on the right Scanner in = null;

try { in = new Scanner(new File(filename));

} catch (FileNotFoundException e) { System.err.println(filename + " not found"); System.exit(1); } String line = in.nextLine(); for (int i = 0; i < line.length(); i++) R.add(line.charAt(i)); while (in.hasNextLine()) { HashSet l = new HashSet(); String[] terms = in.nextLine().split(" "); for (int i = 0; i < terms[0].length(); i++) l.add(terms[0].charAt(i)); for (int i = 0; i < terms[1].length(); i++) F.add(new FD(l, terms[1].charAt(i))); } in.close(); } public FDS2(HashSet r, HashSet f) { R = r; F = f; } HashSet string2set(String X) { HashSet Y = new HashSet(); for (int i = 0; i < X.length(); i++) Y.add(X.charAt(i)); return Y; } void printSet(Set X) { for (char c : X) System.out.print(c); } HashSet closure(HashSet X) { // Algorithm 3.7 HashSet Xplus = new HashSet(X); // 2. initialize

int len = 0; do { // 3. push out len = Xplus.size(); for (FD fd : F) if (Xplus.containsAll(fd.lhs) && !Xplus.contains(fd.rhs)) Xplus.add(fd.rhs); } while (Xplus.size() > len); return Xplus; // 4. found closure of X } FD BCNFviolation() { for (FD fd : F) if (!closure(fd.lhs).containsAll(R)) return fd; return null; } void BCNFdecompose() { // Algorithm 3.20 FD fd = BCNFviolation(); if (fd == null) { // Step 1 printSet(R); System.out.println(); for (FD f : F) { // get a minimal basis for F boolean redundant = false; for (FD f2 : F) // remove those with lhs larger than necessary if (f2.rhs == f.rhs && f.lhs.containsAll(f2.lhs) && !f.lhs.equals(f2.lhs)) { redundant = true; break; } if (!redundant) f.printout(); } System.out.println(); } else { HashSet R1 = closure(fd.lhs); // Step 2 with these three // lines HashSet R2 = new HashSet(fd.lhs); for (char c : R) if (!R1.contains(c)) R2.add(c); // decomposed into R1 and R2 FDS2 fds1 = new FDS2(R1, project(R1, R1, new HashSet())); // Step // 3 fds1.BCNFdecompose(); // recursive call on R1, Step 4 FDS2 fds2 = new FDS2(R2, project(R2, R2, new HashSet())); // Step // 3 fds2.BCNFdecompose(); // recursive call on R2, Step 4 } } HashSet project(HashSet R1, HashSet candidates, HashSet X) { // Algorithm 3.12, starts with candidates = R1 and subset X = empty HashSet T = new HashSet(); // Let T be the eventual output set // of FD's. // Initially, T is empty. Step 1 // This recursive function generates all subsets of R1 in a depth-first // way by set inclusion HashSet Xplus = closure(X); // Step 2 if (X.size() > 0) { for (char A : Xplus) { if (!X.contains('A') && R1.contains('A')) { T.add(new FD(X, A)); } } } ; if (!Xplus.containsAll(R)) { // otherwise no superset needs to be // explored // If we already know that the closure of X is all attributes, then // we cannot discover any // new FD's by closing supersets of X. p.83 HashSet candidates2 = new HashSet(candidates); for (char c : candidates) { // go through all supersets of X by // adding one attribute candidates2.remove(c); X.add(c); T.addAll(project(R1, new HashSet(candidates2), new HashSet(X))); X.remove(c); } } return T; } public static void main(String[] args) {

FDS2 fds = new FDS2(args[0]); fds.BCNFdecompose(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

What is job rotation ?

Answered: 1 week ago