Csc 1302, Honors Principles of Computer Science II (Fall 2016)

Week 2 (1 September 2016)

Initialize Database from file; Remove duplicates in Relation, check equality in Tuple

During this week you will write the following methods:
  1. Initialize Database object by reading data stored in several files in a directory that is given in command line (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 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");
      }
    
    Note: You will need 2 FileInputStream and 2 BufferedReader objects; one to read the catalog.dat file and the other to read the .dat file.
  2. Remove duplicate tuples (Relation.java)
    // Remove duplicate tuples from this relation
    public void removeDuplicates() {
    
    }
    
  3. Test equality of tuples (Tuple.java)
    // Return true if this tuple is equal to compareTuple; false otherwise
    public boolean equals(Tuple compareTuple) {
    
    }
    

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 Driver.java:
[raj@tinman w2]$ java Driver 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: