[Solved] Using Javapublic Class Joinoperator Consider Tables B Share Single Column Whose Name Store Q37168241
Using javapublic class JoinOperator { // Consider tables A and B that share a single column whose name // is stored in the variable uniqueKey. // Every cell in this column has a unique value. // // GOAL: // // It is your job to build a new table that joins the data from // A and B based on the column uniqueKey. // // SUBPROBLEMS: // // (a) The new table must have a map consisting of the column names // from A and B (10 pts) // // (b) In order to match rows from A and B that agree on column uniqueKey, // you must use a HashMap (15 pts) // // (c) After matching rows, you must build new rows that join together // the data from A and B (10 pts) // // (d) Complete the join method and pass the sanity check (10 pts) // public static Table join(Table A, Table B, String uniqueKey) { return null; } // (Optional) You may use the following as a helper method for part (a) // Build and return a new map such that the new map contains all of the // keys from columnNamesFromA and all of the keys from columnNamesFromB. // The new map sends each key to an integer index. If there are n keys, // then each integer index from 0 to n – 1 is used once. private static Map<String, Integer> joinColumnNames(Map<String, Integer> columnNamesFromA, Map<String, Integer> columnNamesFromB) { return null; } // (Optional) You may use the following as a helper method for part (b) // Build and return a HashMap that sends strings to row indexes such // that the HashMap allows you to lookup the index of a row that // contains a specified string value in column uniqueKey. private static Map<String, Integer> createHashMap(Table A, String uniqueKey) { return null; } // (Optional) You may use the following as a helper method for part (c) // Build and return a row that joins together data from the specified // row from A and the specified row from B. private static Row joinSingleRow(Map<String, Integer> columnNamesForNewTable, Table A, Table B, int rowIndexFromA, int rowIndexFromB) { return null; }}public class Table { private Map<String, Integer> columnNames; private List<Row> rows; public Table(Map<String, Integer> columnNames, List<Row> rows) { this.columnNames = columnNames; this.rows = rows; } public Table(String filename) { rows = new ArrayList<Row>(); load(filename); } public Map<String, Integer> columnNamesMap() { return columnNames; } public String getCell(int index, String columnName) { if (columnNames.get(columnName) == null) { return null; } else { return rows.get(index).get(columnNames.get(columnName)); } } public int size() { return rows.size(); } private void load(String filename) { try { String currentDirectory = System.getProperty(“user.dir”) + “/”; // System.out.println(currentDirectory); BufferedReader bf = new BufferedReader(new FileReader(currentDirectory + filename)); // Parse column names columnNames = parseColumnNames(bf.readLine()); // Parse rows String line; while ((line = bf.readLine()) != null) { rows.add(parseRow(line)); } bf.close(); } catch (IOException e) { System.out.println(“File ‘” + filename + “‘ not found.”); } } private Map<String, Integer> parseColumnNames(String line) { Map<String, Integer> columnNames = new HashMap<String, Integer>(); String[] tokens = line.split(“,”); for (int i = 0; i < tokens.length; i++) { columnNames.put(tokens[i], i); } return columnNames; } private Row parseRow(String line) { return new Row(line.split(“,”)); } // This is for debugging purposes public String toString() { String[] columnNamesArray = columnNames.keySet().toArray(new String[0]); Arrays.sort(columnNamesArray); String[] rowsArray = new String[this.size()]; for (int i = 0; i < this.size(); i++) { rowsArray[i] = “”; for (int j = 0; j < columnNamesArray.length; j++) { rowsArray[i] += this.getCell(i, columnNamesArray[j]) + “|”; } } Arrays.sort(rowsArray); return Arrays.toString(rowsArray); }}
Expert Answer
Answer to Using java public class JoinOperator { // Consider tables A and B that share a single column whose name // is stored in… . . .
OR

