Question: heres the code for the java database make select statments for java in which does the following highlight the answers for each statment a) What
heres the code for the java database make select statments for java in which does the following highlight the answers for each statment
a) What PC models have a speed of at least 3.00?
d) Find the model numbers of all color laser printers.
e) Find those manufacturers that cell laptops, but not PC's.
Show your modified main function and the answers to your queries as relations.
import java.io.*; import java.util.*; import java.util.function.*;
public class NewMain{
class Relation{ String name; int cols; int rows; String[] attributes; String[][] tuples; HashMap
public Relation(){ }
public Relation(String filename){ Scanner in = null; try { in = new Scanner(new File(filename)); } catch (FileNotFoundException e){ System.err.println(filename + " not found."); System.exit(1); } String[] terms = in.nextLine().split("\t"); name = terms[0]; cols = Integer.parseInt(terms[1]); rows = Integer.parseInt(terms[2]); attributes = new String[cols]; tuples = new String[rows][cols]; terms = in.nextLine().split("\t"); for (int c = 0; c < cols; c++) attributes[c] = terms[c]; for (int r = 0; r < rows; r++){ terms = in.nextLine().split("\t"); for (int c = 0; c < cols; c++) tuples[r][c] = terms[c]; } in.close(); for (int c = 0; c < cols; c++) att2col.put(attributes[c], c); }
public void showRelation(){ System.out.println(name + "\t" + cols + "\t" + rows); System.out.print(attributes[0]); for (int c = 1; c < cols; c++) System.out.print("\t" + attributes[c]); System.out.println(); for (int r = 0; r < rows; r++){ System.out.print(tuples[r][0]); for (int c = 1; c < cols; c++) System.out.print("\t" + tuples[r][c]); System.out.println(); } }
public Relation project(String... attrs){ int[] colIndex = new int[attrs.length]; for (int c = 0; c < attrs.length; c++) if (att2col.containsKey(attrs[c])) colIndex[c] = att2col.get(attrs[c]); else{ System.err.println("attribute " + attrs[c] + " not found."); System.exit(1); }
Relation PR = new Relation(); PR.cols = attrs.length; PR.name = "project(" + name; for (int c = 0; c < PR.cols; c++) PR.name += "," + attrs[c]; PR.name += ")"; PR.rows = rows; PR.attributes = new String[PR.cols]; for (int c = 0; c < PR.cols; c++) PR.attributes[c] = attrs[c]; PR.tuples = new String[rows][PR.cols]; for (int r = 0; r < rows; r++) for (int c = 0; c < PR.cols; c++) PR.tuples[r][c] = tuples[r][colIndex[c]]; return PR; }
public Relation select(Function
public Relation intersection(Relation other){ if (cols != other.cols) return null; boolean[] intersects = new boolean[rows]; int newRows = 0; for (int r = 0; r < rows; r++){ int r1 = 0; for (; r1 < other.rows; r1++){ int c = 0; for (; c < cols; c++) if (!tuples[r][c].equals(other.tuples[r1][c])) break; if (c == cols) break; } intersects[r] = r1 < other.rows; if (intersects[r]) newRows++; } Relation R = new Relation(); R.name = name + " n " + other.name; R.cols = cols; R.rows = newRows; R.attributes = new String[cols]; for (int c = 0; c < cols; c++){ R.attributes[c] = attributes[c]; R.att2col.put(attributes[c], c); } R.tuples = new String[R.rows][cols]; int n = 0; for (int r = 0; r < rows; r++) if (intersects[r]){ for (int c = 0; c < cols; c++) R.tuples[n][c] = tuples[r][c]; n++; } return R; }
public Relation union(Relation other){ if (cols != other.cols) return null; boolean[] intersects = new boolean[rows]; int newRows = other.rows; for (int r = 0; r < rows; r++){ int r1 = 0; for (; r1 < other.rows; r1++){ int c = 0; for (; c < cols; c++) if (!tuples[r][c].equals(other.tuples[r1][c])) break; if (c == cols) break; } intersects[r] = r1 < other.rows; if (!intersects[r]) newRows++; } Relation R = new Relation(); R.name = name + " u " + other.name; R.cols = cols; R.rows = newRows; R.attributes = new String[cols]; for (int c = 0; c < cols; c++){ R.attributes[c] = attributes[c]; R.att2col.put(attributes[c], c); } R.tuples = new String[R.rows][cols]; for (int r = 0; r < other.rows; r++) for (int c = 0; c < cols; c++) R.tuples[r][c] = other.tuples[r][c]; int n = other.rows; for (int r = 0; r < rows; r++) if (!intersects[r]){ for (int c = 0; c < cols; c++) R.tuples[n][c] = tuples[r][c]; n++; } return R; }
public Relation difference(Relation other){ if (cols != other.cols) return null; boolean[] intersects = new boolean[rows]; int newRows = rows; for (int r = 0; r < rows; r++){ int r1 = 0; for (; r1 < other.rows; r1++){ int c = 0; for (; c < cols; c++) if (!tuples[r][c].equals(other.tuples[r1][c])) break; if (c == cols) break; } intersects[r] = r1 < other.rows; if (intersects[r]) newRows--; } Relation R = new Relation(); R.name = name + " - " + other.name; R.cols = cols; R.rows = newRows; R.attributes = new String[cols]; for (int c = 0; c < cols; c++){ R.attributes[c] = attributes[c]; R.att2col.put(attributes[c], c); } R.tuples = new String[R.rows][cols]; int n = 0; for (int r = 0; r < rows; r++) if (!intersects[r]){ for (int c = 0; c < cols; c++) R.tuples[n][c] = tuples[r][c]; n++; } return R; }
public Relation unique(){ // remove duplicate tuples boolean[] intersects = new boolean[rows]; int newRows = 0; for (int r = 0; r < rows; r++){ int r1 = 0; for (; r1 < r; r1++){ int c = 0; for (; c < cols; c++) if (!tuples[r][c].equals(tuples[r1][c])) break; if (c == cols) break; } intersects[r] = r1 < r; if (!intersects[r]) newRows++; } Relation R = new Relation(); R.name = name + ".uniq"; R.cols = cols; R.rows = newRows; R.attributes = new String[cols]; for (int c = 0; c < cols; c++){ R.attributes[c] = attributes[c]; R.att2col.put(attributes[c], c); } R.tuples = new String[R.rows][cols]; int n = 0; for (int r = 0; r < rows; r++) if (!intersects[r]){ for (int c = 0; c < cols; c++) R.tuples[n][c] = tuples[r][c]; n++; } return R; } };
void algebra(){ Relation Product = new Relation("Product.txt"); Relation PC = new Relation("PC.txt"); Relation Laptop = new Relation("Laptop.txt"); Relation Printer = new Relation("Printer.txt"); Relation R1 = PC.project("model", "speed").union(Laptop.project("model", "speed")); Relation R2 = R1.select(r -> Double.parseDouble(r[R1.att2col.get("speed")]) < 2.00).project("model"); R2.showRelation(); }
public static void main(String[] args){ NewMain db2 = new NewMain(); db2.algebra(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
