Csc 1302, Honors Principles of Computer Science II (Fall 2021)
Week 2 (3 September 2021)
Initialize Database from Files; Check Equality of Tuples; Remove Duplicates in Relation
During this week you will write the following methods:- Initialize Database object by reading data stored in several files
in a directory that is given in command line (this method belongs to Database.java)
//Create the database object by reading data from several files in directory dir public void initializeDatabase(String dir) { }
Here is a sample of files available in the directory: drinks (also in .zip format: drinks.zip). The file catalog.dat contains schema information for all the relations in the database and individual .dat files contain the relation instances (tuples).To read data from a file, you can use the following code:
FileInputStream fin1 = null; BufferedReader infile1 = null; try { fin1 = new FileInputStream("f.dat"); infile1 = new BufferedReader(new InputStreamReader(fin1)); String s = infile1.readLine(); ... ... fin1.close(); } catch (IOException e) { System.out.println("Error reading file"); }
- Test equality of tuples (this method belongs to Tuple.java)
// Return true if this tuple is equal to compareTuple; false otherwise public boolean equals(Tuple compareTuple) { }
- Remove duplicate tuples (this method belongs to Relation.java)
// Remove duplicate tuples from this relation public void removeDuplicates() { }
Download the Driver Programs and implement all the methods in these Java classes. Compile and run the driver programs.
You should see the following output when you run Driver2.java:
[raj@tinman w2]$ java Driver2 drinks Database Schema -------- ------ SERVES(BNAME:VARCHAR,RNAME:VARCHAR) BAR(BNAME:VARCHAR) SELLS(BAR:VARCHAR,BEER:VARCHAR,PRICE:INTEGER) DRINKER(DNAME:VARCHAR) LIKES(DNAME:VARCHAR,RNAME:VARCHAR) FREQUENTS(DNAME:VARCHAR,BNAME:VARCHAR) BARS(NAME:VARCHAR,ADDR:VARCHAR) BEER(RNAME:VARCHAR,PRICE:INTEGER) BAR(BNAME:VARCHAR) Number of tuples: 4 Jillians: Dugans: ESPN Zone: Charlies: DRINKER(DNAME:VARCHAR) Number of tuples: 5 John: Peter: Donald: Jeremy: Clark: SELLS(BAR:VARCHAR,BEER:VARCHAR,PRICE:INTEGER) Number of tuples: 9 Jillians:Bud:6: Jillians:Michelob:6: Jillians:Heineken:8: Dugans:Bud:9: Dugans:Michelob:10: Dugans:Fosters:12: ESPN Zone:Fosters:9: Charlies:Heineken:10: Charlies:Foster:10:
and the following output when you run the DuplicatesDriver.java program:
[raj@tinman w2]$ java DuplicatesDriver Before Removing Duplicates: STUDENT(SID:INTEGER,SNAME:VARCHAR) Number of tuples: 7 1111:Robert Adams: 1112:Charles Bailey: 1113:Donald James: 1112:Charles Bailey: 1112:Charles Bailey: 1114:Michael James: 1113:Donald James: After Removing Duplicates: STUDENT(SID:INTEGER,SNAME:VARCHAR) Number of tuples: 4 1111:Robert Adams: 1112:Charles Bailey: 1113:Donald James: 1114:Michael James: