Csc 1302, Honors Principles of Computer Science II (Fall 2016)
Week 7 (6 October 2016)
Project Operator
During this week you will write the following methods:- (Tuple.java):
// This method takes as input an array list of column names, each of which // belonging to this.attributes, and returns a new tuple with only those // components that correspond to the column names in cnames. public Tuple project(ArrayList<String> cnames) { Create an array list, doms, with domains values for column names in cnames (Note: you would need .indexOf() method of ArrayList to determine the index where the column name is stored) Create a new Tuple object, tup; for each column name in cnames: add corresponding component of this.tuple to tup; return tup; }
- (Relation.java) Project Operator:
// This methods takes as input an array list of column names, each of which // belonging to this.attributes, and returns a relation whose tuples are // formed by projecting the columns from cnames. // Example: R(A:INTEGER, B:INTEGER, C:DECIMAL) with tuples // {(10,20,3.5),(11,22,7.8),(10,25,3.5)} // Then, with cnames = {A,C}, the output relation should // have schema (A:INTEGER, C:DECIMAL) and tuples // {(10,3.5),(11,7.8)} // Note that after projection one may get duplicate tuples, which should // be deleted. public Relation project(ArrayList<String> cnames) { Create new attrs and doms from cnames and this.domains; Create a new Relation object, rel; for each tuple in this.table: call the project() method of Tuple class and get a new tuple; add the new tuple to this.table; Remove duplicates from rel; return rel; }
Download the Driver Programs and implement all the methods in this assignment. Compile and run the driver programs. You should see the following output when you run DriverW7.java:
[raj@tinman w4]$ java DriverW7 STUDENT(SID:INTEGER,SNAME:VARCHAR,PHONE:INTEGER,MAJOR:VARCHAR,GPA:DECIMAL) Number of tuples: 4 1111:Robert Adams:1234:Computer Science:4.0: 1112:Charles Bailey:5656:Computer Science:3.5: 1113:David Beatle:1212:Mathematics:3.5: 1114:Graham Gooch:5678:Computer Science:3.5: PROJECT_SID_GPA_STUDENT(SID:INTEGER,GPA:DECIMAL) Number of tuples: 4 1111:4.0: 1112:3.5: 1113:3.5: 1114:3.5: PROJECT_MAJOR_GPA_STUDENT(MAJOR:VARCHAR,GPA:DECIMAL) Number of tuples: 3 Computer Science:4.0: Computer Science:3.5: Mathematics:3.5: PROJECT_MAJOR_STUDENT(MAJOR:VARCHAR) Number of tuples: 2 Computer Science: Mathematics: