Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

do the following java program so that it reads and output the code using a text file to read theirs instuctions inside the code heres

do the following java program so that it reads and output the code using a text file to read theirs instuctions inside the code

heres a example of the textfile in order put them in a text file and have the program read and then it should solve it

ABCDEF AB C BC AD D E CF B

// DB5.java CS5151/6051 2020 Cheng

// Algorithm 3.7 closure of X under F

// Usage: java DB5 F

// F is a file that has the first line all the attributes and

// then an FD a line with a space between the left-hand side and the right-hand

side

// A set enumeration algorithm is used to go through all subsets of attributes

// closure, nontrivial FDs, and whether the subset is a superkey are displayed

// recursive calls in set enumeration SE stops if closure is all attributes

import java.io.*;

import java.util.*;

public class DB5{

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;

}

};

HashSet R = new HashSet(); // all attributes

HashSet F = new HashSet(); // the set of FDs

HashSet X = null; // X used in Algorithm 3.7

public DB5(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();

}

HashSet closure(HashSet Xinit){ // Algorithm 3.7

X = new HashSet(Xinit); // 2. initialize

int len = 0;

do { // 3. push out

len = X.size();

F.forEach(fd -> {

// your code for step 3 of Algorithm 3.7

// if all of fd.lhs are in X, but fd.rhs is not, then add fd.rhs

to X.

// should be an if-then statement with ending ;

// Hint: HashSet methods contains(T) and containsAll(Set)

});

} while (X.size() > len);

return X; // 4. found closure of X

}

void SE(HashSet view, HashSet subset){ // subset enumeration

if (handleSubset(subset)) return;

HashSet newView = new HashSet(view);

view.forEach(c -> {

newView.remove(c);

subset.add(c);

SE(new HashSet(newView), new HashSet(subset));

subset.remove(c);

});

}

boolean handleSubset(HashSet subset){

System.out.print("\"");

subset.forEach(x -> System.out.print(x));

System.out.print("\",\"");

HashSet clo = closure(subset);

boolean superkey = clo.size() == R.size();

clo.forEach(x -> System.out.print(x));

System.out.print("\"");

clo.removeAll(subset);

if (clo.size() > 0){

System.out.print(",");

subset.forEach(x -> System.out.print(x));

System.out.print("->");

clo.forEach(x -> System.out.print(x));

}

if (superkey) System.out.println(" superkey"); else System.out.println();

return superkey;

}

void closuresToAll(){

System.out.println("Subset,Closure,Nontrivial FDs");

SE(R, new HashSet());

}

public static void main(String[] args){

DB5key db5 = new DB5key(args[0]);

db5.closuresToAll();

}

}

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions