-2
我的作業被稱爲「關係代數」,它要求我在.txt文件中的兩個表上執行操作Union,Difference,Intersection,Join,Cartesian Product和Project :關係代數作業(數組/集合?)
a 1 a 1
b 2 and z 26
c 3 c 3
我最初的問題基本上是如何處理這個項目?
以下是我完成的項目,如果有任何錯誤,請讓我知道。建議和批評也歡迎。
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
/**
* ----------------------------------------------------------------------------
* ------------------------- Purpose : This class is used to create a row for a
* two dimensional data
*
* @since 06/17/2014
* @author abass.alamnehe
* --------------------------------------------------------
* ---------------------------------------------
*/
class Table { //Class Table
public String getID() { //getters and setters
return ID;
}
public void setID(String iD) {
this.ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String ID = "";
String name = "";
Table(String ID, String name) { //Table constructor
this.ID = ID;
this.name = name;
}
}
public class RelationalAlgebra {
/**
* It reads a two columns table into a two dimensional array
*
* @return ArrayList
* <Table>
* @throws IOException
*/
ArrayList<Table> getTable(String fileName) throws IOException {
ArrayList<Table> T1 = new ArrayList<Table>(); // creates an array list
File inFile = new File(fileName); // creates a file object
Scanner scanner = new Scanner(inFile); // Scanner is a reader class
int repetition = 1; // used to skip the 1st line from input file
while (scanner.hasNext()) { // reads until not data
if (repetition == 1) { // if 1st line, skips
scanner.next();
scanner.next();
repetition = 2;
} else { // else reads each column
String ID = scanner.next();
String name = scanner.next();
T1.add(new Table(ID, name));
}
}
scanner.close(); // close input stream
return T1; // returns the new table in the form of ArrayList
}
/**
* It prints the content of an ArrayList
* <Table>
*
* @param t
*/
void printTable(ArrayList<Table> t) {
for (int i = 0; i < t.size(); i++) {
System.out.println(t.get(i).ID + "\t" + t.get(i).name);
}
}
/**
* It prints the content of an ArrayList into a Cartesian Product
* <Table>
*
* @param t
*/
void printCartProdTable(ArrayList<Table> t) {
for (int i = 0; i < t.size() - 3; i++) {
System.out.print(t.get(i).ID + " " + t.get(i).name + "\t");
System.out.print(t.get(i + 1).ID + " " + t.get(i + 1).name + "\t");
System.out.print(t.get(i + 2).ID + " " + t.get(i + 2).name + "\t");
System.out.println(t.get(i + 3).ID + " " + t.get(i + 3).name + "\t");
}
}
/**
* It prints the content of an ArrayList
* <String>
*
* @param t
*/
void printProj(ArrayList<String> t) {
for (int i = 0; i < t.size(); i++) {
System.out.println(t.get(i));
}
}
ArrayList<Table> intersect(ArrayList<Table> t1, ArrayList<Table> t2) { // method intersect, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
/* Checks each object, "Table", in both ArrayLists (t1 against t2)
* for equality at both ID and Name , ignores case,
* adds matches to res
*/
for (int i = 0; i < t1.size(); i++) {
for (int j = 0; j < t2.size(); j++)
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(j).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(j).name.toString()))
res.add(t2.get(j));
}
printTable(res); //prints table. printTable method
return res; //returns result (ArrayList)
}
ArrayList<Table> join(ArrayList<Table> t1, ArrayList<Table> t2) { // method join, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
/* Checks each, "Table", at corresponding key (t1 against t2)
* for equality at both ID and Name, ignores case,
* adds matches to res
*/
for (int i = 0; i < t1.size(); i++) {
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(i).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(i).name.toString())) {
res.add(t2.get(i));
}
}
printTable(res);
return res; //returns result (ArrayList)
}
ArrayList<Table> union(ArrayList<Table> t1, ArrayList<Table> t2) { // method union, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
/* Adds all of t1 to res. Checks each object
* in both ArrayLists (t1 against t2) for equality at
* both ID and Name , ignores case, adds non-matches
* to res at corresponding key
*/
res.addAll(t1);
int c = 0;
for (int j = 0; j < t1.size(); j++) {
for (int i = 0; i < t2.size(); i++) {
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(j).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(j).name.toString()))
break;
else if (i == t2.size() - 1) {
res.add(j + c, t2.get(j));
c++;
}
}
}
printTable(res); //prints table. printTable method
return res; //returns result (ArrayList)
}
ArrayList<Table> differenceAB(ArrayList<Table> t1, ArrayList<Table> t2) { // method differenceAB, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
/* Checks each object
* in both ArrayLists (t1 against t2) for equality at
* both ID and Name , ignores case, adds t1's non-matches
* to res
*/
for (int i = 0; i < t1.size(); i++) {
for (int j = 0; j < t2.size(); j++) {
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(j).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(j).name.toString()))
break;
else if (j == t2.size() - 1) {
res.add(t1.get(i));
}
}
}
printTable(res); //prints table. printTable method
return res; //returns result (ArrayList)
}
ArrayList<Table> differenceBA(ArrayList<Table> t1, ArrayList<Table> t2) { // method differenceBA, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
/* Alternate for above method.
* Checks each object
* in both ArrayLists (t1 against t2) for equality at
* both ID and Name , ignores case, adds t2's non-matches
* to res
*/
for (int j = 0; j < t1.size(); j++) {
for (int i = 0; i < t2.size(); i++) {
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(j).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(j).name.toString()))
break;
else if (i == t1.size() - 1) {
res.add(t2.get(j));
}
}
}
printTable(res); //prints table. printTable method
return res; //returns result (ArrayList)
}
ArrayList<Table> cartProdBA(ArrayList<Table> t1, ArrayList<Table> t2) { // method cartProdBA, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
for (int j = 0; j < t1.size(); j++) {
for (int i = 0; i < t2.size(); i++) {
/* Distributes ID and name of each table t2,
* across each table t1. ((Adds new table to
* res; each name and ID from t2 with each t1 name and ID))
*/
res.add(new Table(t2.get(j).ID, t1.get(i).ID));
res.add(new Table(t2.get(j).ID, t1.get(i).name));
res.add(new Table(t2.get(j).name, t1.get(i).ID));
res.add(new Table(t2.get(j).name, t1.get(i).name));
}
}
printCartProdTable(res); //prints table. printCartProdTable method
return res; //returns result (ArrayList)
}
ArrayList<Table> cartProdAB(ArrayList<Table> t1, ArrayList<Table> t2) { // method cartProdAB, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
for (int j = 0; j < t1.size(); j++) {
for (int i = 0; i < t2.size(); i++) {
/* Alternate for above method.
* Distributes ID and name of each table t2,
* across each table t1. ((Adds new table to
* res; each name and ID from t2 with each t1 name and ID))
*/
res.add(new Table(t1.get(j).ID, t2.get(i).ID));
res.add(new Table(t1.get(j).ID, t2.get(i).name));
res.add(new Table(t1.get(j).name, t2.get(i).ID));
res.add(new Table(t1.get(j).name, t2.get(i).name));
}
}
printCartProdTable(res); //prints table. printCartProdTable method
return res; //returns result (ArrayList)
}
ArrayList<String> projectID(ArrayList<Table> t1, ArrayList<Table> t2) { // method projectID, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
ArrayList<String> proj = new ArrayList<String>(); // creates an instance of ArrayList, proj
/* Makes union of t1 and t2 in res.
* Adds each ID from res to proj.
*/
res.addAll(t1);
int c = 0;
for (int j = 0; j < t1.size(); j++) {
for (int i = 0; i < t2.size(); i++) {
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(j).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(j).name.toString()))
break;
else if (i == t2.size() - 1) {
res.add(j + c, t2.get(j));
c++;
}
}
}
for (int i = 0; i < res.size(); i++) {
proj.add(res.get(i).ID);
}
printProj(proj); //prints table. printProj method
return proj; //returns proj (ArrayList)
}
ArrayList<String> projectName(ArrayList<Table> t1, ArrayList<Table> t2) { // method projectName, accepts Arraylists of Tables (t1,t2)
ArrayList<Table> res = new ArrayList<Table>(); // creates an instance of ArrayList, result
ArrayList<String> proj = new ArrayList<String>(); // creates an instance of ArrayList, proj
/* Alternate for above method.
* Makes union of t1 and t2 in res.
* Adds each Name from res to proj.
*/
res.addAll(t1);
int c = 0;
for (int j = 0; j < t1.size(); j++) {
for (int i = 0; i < t2.size(); i++) {
if (t1.get(i).ID.toString().equalsIgnoreCase(
t2.get(j).ID.toString())
&& t1.get(i).name.toString().equalsIgnoreCase(
t2.get(j).name.toString()))
break;
else if (i == t2.size() - 1) {
res.add(j + c, t2.get(j));
c++;
}
}
}
for (int i = 0; i < res.size(); i++) {
proj.add(res.get(i).name);
}
printProj(proj); //prints table. printProj method
return proj; //returns proj (ArrayList)
}
/**
* An entry point for program execution
*
* @param args
*/
public static void main(String[] args) throws IOException {
RelationalAlgebra rel = new RelationalAlgebra(); // creates an object of
// this class
ArrayList<Table> t1 = new ArrayList<Table>();
ArrayList<Table> t2 = new ArrayList<Table>(); // creates an instance of ArrayList, t1
// creates an instance of ArrayList, t2
String t1file = JOptionPane.showInputDialog(
"Enter Table 1 (.txt) file location with double backslahes") //user input to string file location, t1
.toString();
String t2file = JOptionPane.showInputDialog(
"Enter Table 2 (.txt) file location with double backslahes") //user input to string file location, t2
.toString();
t1 = rel.getTable(t1file); // creates an object based on the input file
t2 = rel.getTable(t2file); // creates an object based on the input file
boolean input = false; //creates exit for while loop
String select = null; // initializes variable for switch statement
while (!input) { //while loop to prevent crash with invalid input
select = JOptionPane
.showInputDialog(
"Enter a number (1-9) corresponding to desired operation: \n"
+ " 1 = Intersection of Table 1 and Table 2 \n"
+ " 2 = Union of Table 1 and Table 2 \n"
+ " 3 = Join of Table 1 and Table 2 \n"
+ " 4 = Difference (Table 1 - Table 2) \n"
+ " 5 = Difference (Table 2 - Table 1) \n"
+ " 6 = Cartesian Product (Table 1 x Table 2) \n"
+ " 7 = Cartesian Product (Table 2 x Table 1) \n"
+ " 8 = Project 'ID' from the Union of Table 1 and Table 2 \n"
+ " 9 = Project 'Name' from the Union of Table 1 and Table 2 \n")
.toString(); // takes user input for selection of operation 1-9, as string
if (select.matches("1") || select.matches("2") // tests for valid input
|| select.matches("3") || select.matches("4")
|| select.matches("5") || select.matches("6")
|| select.matches("7") || select.matches("8")
|| select.matches("9"))
input = true; // exits while loop, if valid input
}
switch (select) { //switches between all 9 operation methods
// according to user input, which print final results of operation
case "1":
rel.intersect(t1, t2);
break;
case "2":
rel.union(t1, t2);
break;
case "3":
rel.join(t1, t2);
break;
case "4":
rel.differenceAB(t1, t2);
break;
case "5":
rel.differenceBA(t1, t2);
break;
case "6":
rel.cartProdAB(t1, t2);
break;
case "7":
rel.cartProdBA(t1, t2);
break;
case "8":
rel.projectID(t1, t2);
break;
case "9":
rel.projectName(t1, t2);
break;
}
}
}
除了你在課堂上給你什麼,你有什麼嘗試自己? – drum
基本上什麼都沒有,除了 – JMMM
RelationalAlgebra rel = new RelationalAlgebra(); \t \t \t ArrayList
回答
它看起來像你的數據對應的地圖,而不是數組或任何設置。 Java已經內置了地圖類,最有可能爲你的目的是
java.util.HashMap
。有了這種形式的數據,你可以用地圖'keySet
s和entrySet
s做一些很好的事情。在後者中可以做的事情是通過iterator
或者使用Iterable
這個事實遍歷map(鍵和值)中的所有條目;您需要爲您的連接(以及您的簡單迭代)執行此操作,但是您可以找到更簡單的交集,聯合和差異操作方法。請閱讀Javadocs的
Map
和Set
。來源
2014-07-15 22:21:04
好吧,我給地圖試一下 – JMMM
@JIMM&JohnBollinger:有一個元組(數組切片)是一個Map並且有一個集合,比如Set的集合代表關係是合理的。然後Map迭代在屬性名稱 - 值對之上,並且集合迭代在元組之上。但是對於數組/關係集合使用映射是不合適的,因爲Map鍵(以元組作爲值)是不必要的;集合迭代器充當元組的概念引用。 – philipxy
大聲笑,就在我的頭:) – JMMM
相關問題