hello
i am doing a assignment in java for the database and i am stuck in the assignment. Please help me understand this statement
private final List<Comparable []> tuples= new ArrayList<Comparable []> ();
Printable View
hello
i am doing a assignment in java for the database and i am stuck in the assignment. Please help me understand this statement
private final List<Comparable []> tuples= new ArrayList<Comparable []> ();
The question is far too generic for me to list the array of comparables.
The statement merely declares and initializes a List of arrays as an ArrayList of arrays.
Each array contains objects that we don't know much about except that they all implement the Comparable interface. The variable tuples will always be pointing to this initialized list.
this is too much confusing,, can u please explain it to me,,
i am a new programmer to java and I am having a lot of difficulties in making this project,, and have to submit by tomorrow,, please help
Perhaps you can get better help if you provide more context for your question. Where did you see this line? How are you supposed to be using this? Anything you know, let us know.
You are going to fail your project then. You should have started your project earlier when you had the time to learn about all these concepts. Now you've suddenly got all these new terms and new concepts that you must grasp and use in a project to be submitted tomorrow.
No one is going to be able to teach you all you need to understand today.
At least you've leaned the lesson of never postponing for tomorrow what you can do today.
Yes u are right,, but right now i need help more than critisizm,, if you cant help me,, then dont try to ,,, anyways if u want to help me please participate,,
@fubarable,,i m using this line to get the list of tuples in the table,, which are given as
private final List<Comparable []> tuples= new ArrayList<Comparable []> ();
right now i am stuck in the 'comparable' interface in this line,, i have another hint of code,, but i dont no how to use it to get it working
Comparable [] values0 = { "Star_Wars" , 1977, 124, "T", "Fox", 12345 };
this line is i think is used in the programm
Each element of the 'tuples' list is an array of Comparables. Comparable is an interface that classes can implement so their objects can be compared. The list can contain arrays of any object that implements the Comparable interface. String implements the Comparable interface, so Strings can be in the arrays.
you never say what you are having trouble with... you just said you're stuck on a line without saying a thing about what's wrong with it, what you think is wrong with it, what you've tried, etc.
it's hard for people to give you useful help when we don't even know what you understand.
import static java.lang.System.*;
import java.io.*;
import java.util.*;
/************************************************** ***************************************
* This class implements relational database tables (including attribute names, domains
* and a list of tuples. Five basic relational algebra operators are provided: project,
* select, union and minus. The insert data manipulation operator is also provided.
* Missing are update and delete data manipulation operators, primary keys, duplicate
* elimination and indexes.
*/
public class Table implements Serializable, Cloneable
{
/** Serializable class version number.
*/
private static final long serialVersionUID = 11L;
/** Counter for naming temporary tables.
*/
private static int count = 0;
/** Table name.
*/
private final String name;
/** Array of attribute names.
*/
private final String [] attribute;
/** Array of attribute domains: a domain may be
* real: Double, Float; integer: Long, Integer, Short, Byte; string: String
*/
private final Class [] domain;
/** Collection of tuples.
*/
private final List<Comparable []> tuples;
/************************************************** ***********************************
* Construct an empty table from the meta-data specifications.
* @param name The name of the relation.
* @param attribute String containing attributes names
* @param domain String containing attribute domains (data types).
*/
public Table (String name, String [] attribute, Class [] domain)
{
this.name = name;
this.attribute = attribute;
this.domain = domain;
this.tuples = new ArrayList<Comparable []> ();
} // Table
/************************************************** ***********************************
* Construct an empty table from the raw string specifications.
* @param name The name of the relation.
* @param attributes String containing attributes names
* @param domains String containing attribute domains (data types).
*/
public Table (String name, String attributes, String domains)
{
this.name = name;
this.attribute = attributes.split (" ");
this.domain = findClass (domains.split (" "));
this.tuples = new ArrayList<Comparable []> ();
} // Table
/************************************************** ***********************************
* Construct an empty table using the meta-data of an existing table.
* @param tab The table supplying the meta-data.
* @param suffix The suffix appended to create new table name.
*/
public Table (Table tab, String suffix)
{
this (tab.name + suffix, tab.attribute, tab.domain);
} // Table
/************************************************** ***********************************
* Project the tuples onto a lower dimension by keeping only the given attributes.
* #usage movie.project ("title year studioName")
* @param attributeList Attributes to project onto.
* @return Table of projected tuples.
*/
public Table project (String attributeList)
{
out.println ("" + name + " . project ( " + attributeList + " )");
String [] pAttribute = attributeList.split (" ");
Class [] colDomain = extractDom (match (pAttribute), domain);
Table result = new Table (name + count++, pAttribute, colDomain);
for (int i = 0; i < tuples.size (); i++) {
//\\//\\ TO BE IMPLEMENTED //\\//\\
} // for
return result;
} // project
/************************************************** ***********************************
* Select the tuples satisfying the given condition.
* A condition is written as infix expression consists of
* 6 comparison operators: "==", "!=", "<", "<=", ">", ">="
* 2 Boolean operators: "&", "|" (from high to low precedence)
* #usage movie.select ("1979 < year & year < 1990")
* @param condition Check condition for tuples.
* @return Table with tuples satisfying the condition.
*/
public Table select (String condition)
{
out.println ("" + name + " . select ( " + condition + " )");
Table result = new Table (name + count++, attribute, domain);
String [] postfix = infix2postfix (condition.split (" "));
for (Comparable [] tup : tuples) {
if (evalTup (postfix, tup)) {
result.tuples.add (tup);
} // if
} // for
return result;
} // select
/************************************************** ***********************************
* Union this table and table 2.
* #usage movie.union (show)
* @param table2 The rhs table in the union operation.
* @return Table representing the union.
*/
public Table union (Table table2)
{
out.println ("" + name + " . union ( " + table2.name + " )");
Table result = new Table (name + count++, attribute, domain);
//\\//\\ TO BE IMPLEMENTED //\\//\\
return result;
} // union
/************************************************** ***********************************
* Take the difference of this table and table 2.
* #usage movie.minus (show)
* @param table2 The rhs table in the minus operation.
* @return Table representing the difference.
*/
public Table minus (Table table2)
{
out.println ("" + name + " . minus ( " + table2.name + " )");
Table result = new Table (name + count++, attribute, domain);
//\\//\\ TO BE IMPLEMENTED //\\//\\
return result;
} // minus
/************************************************** ***********************************
* Insert a tuple to the table.
* #usage movie.insert ("'Star_Wars'", 1977, 124, "T", "Fox", 12345)
* @param tuple Array of attribute values.
* @return Whether insertion was successful.
*/
public boolean insert (Comparable [] tup)
{
out.println ("insert ( " + tup + " )");
if (typeCheck (tup)) {
tuples.add (tup);
return true;
} else {
return false;
} // if
} // insert
/************************************************** ***********************************
* Print the table.
*/
public void print ()
{
out.println ("\n\n Table " + name);
for (String a : attribute) {
out.print (" " + a + " ");
} // for
out.println ();
for (Comparable [] tup : tuples) {
out.print ("[");
for (Comparable attr : tup) {
out.print (attr + " ");
} // for
out.println ("]");
} // for
/************************************************** ***********************************
* Get the column position for the given attribute name.
* @param attr The given attribute name.
* @return The column position.
*/
private int columnPos (String attr)
{
for (int i = 0; i < attribute.length; i++) {
if (attribute [i].equals (attr)) {
return i;
} // if
} // for
return -1; // not found
} // columnPos
/************************************************** ***********************************
* Check whether the tuple satisfies the condition.
* @param pCondition The postfix expression for the condition
* @param tup The tuple to check.
* @return Whether to keep the tuple.
*/
private boolean evalTup (String [] pCondition, Comparable [] tup)
{
if (pCondition == null) {
return true;
} // if
Stack<Object> s = new Stack<Object> ();
//\\//\\ TO BE IMPLEMENTED //\\//\\
s.push (Boolean.TRUE);
return (Boolean) s.pop ();
} // evalTup
/************************************************** ***********************************
* Convert an infix expression to a postfix expression. This implementation does not
* handle parentheses.
* @param infix A tokenized infix expression.
* @return The resultant tokenized postfix expression.
*/
private String [] infix2postfix (String [] infix)
{
String [] postfix = null;
if (infix != null && infix.length > 0) {
postfix = new String [infix.length];
} // if
//\\//\\ TO BE IMPLEMENTED //\\//\\
return postfix;
} // infix2postfix
/************************************************** ***********************************
* Match the column and attribute names to determine the domains.
* @param column Array of column names.
* @return Array of column index position.
*/
private int [] match (String [] column)
{
int [] colPos = new int [column.length];
for (int j = 0; j < column.length; j++) {
boolean matched = false;
for (int k = 0; k < attribute.length; k++) {
if (column [j].equals (attribute [k])) {
matched = true;
colPos [j] = k;
} // for
} // for
if ( ! matched) {
out.println ("match: domain not found for " + column [j]);
} // if
} // for
return colPos;
} // match
/************************************************** ***********************************
* Check the size of the tuple (number of elements in list) as well as the type of
* each value to ensure it is from the right domain.
* @param tup The tuple as a list of attribute values.
* @return Whether the tuple has the right size and values that comply
* with the given domains.
*/
private boolean typeCheck (Comparable [] tup)
{
//\\//\\ TO BE IMPLEMENTED //\\//\\
return true;
} // typeCheck
/************************************************** ***********************************
* Find the classes in the "java.lang" package with given names.
* @param className Array of class name (e.g., {"Integer", "String"})
* @return Array of Java classes.
*/
private Class [] findClass (String [] className)
{
Class [] classArray = new Class [className.length];
for (int i = 0; i < className.length; i++) {
try {
classArray [i] = Class.forName ("java.lang." + className [i]);
} catch (ClassNotFoundException ex) {
out.println ("findClass: " + ex);
} // try
} // for
return classArray;
} // findClass
/************************************************** ***********************************
* Extract the corresponding domains.
* @param colPos Column positions to extract.
* @param group Where to extract from.
* @return The extracted domains.
*/
private Class [] extractDom (int [] colPos, Class [] group)
{
Class [] obj = new Class [colPos.length];
for (int j = 0; j < colPos.length; j++) {
obj [j] = group [colPos [j]];
} // for
return obj;
} // extractDom
/************************************************** ***********************************
* Main method for testing purpose only.
* @param args Command-line arguments.
*/
public static void main (String [] args)
{
out.println ("----------------------------------------------------------------------");
Comparable [] values0 = { "Star_Wars" , 1977, 124, "T", "Fox", 12345 };
Comparable [] values1 = { "Star_Wars_2", 1980, 124, "T", "Fox", 12345 };
Comparable [] values2 = { "Rocky", 1985, 200, "T", "Universal", 12125 };
Comparable [] values3 = { "Rambo", 1978, 100, "T", "Universal", 32355 };
out.println ("\n----------------------- CreateTable movie ----------------------------");
Table movie = new Table ( "movie", "title year length inColor studioName producerCno",
"String Integer Integer String String Integer");
out.println ("------------------------------------------------------------------------");
movie.insert (values0);
movie.insert (values1);
movie.insert (values2);
movie.print ();
out.println ("\n----------------------- CreateTable cinema ---------------------------");
Table cinema = new Table ("cinema", "title year length inColor studioName producerCno",
"String Integer Integer String String Integer");
cinema.insert (values1);
cinema.insert (values2);
cinema.insert (values3);
cinema.print ();
out.println ("\n----------------------- Test Project ---------------------------------");
Table t_project = movie.project ("title year");
t_project.print ();
out.println ("\n----------------------- Test Select ----------------------------------");
Table t_select = movie.select ("title == 'Star_Wars'");
t_select.print ();
out.println ("\n----------------------- Test Union -----------------------------------");
Table t_union = movie.union (cinema);
t_union.print ();
out.println ("\n----------------------- Test Minus -----------------------------------");
Table t_minus = movie.minus (cinema);
t_minus.print ();
} // main
} // Table class
this is the whole programm, i am having trouble with the specific parts which says to be implemented,, if u can help me figure out these parts,, i have done most of the work,, but im stuck in specific places
thanks in advance
1) Use code tags when posting code, since without them, your code is close to unreadable.
2) You need to tell us more about how you're stuck, what you've tried, etc...
I am trying to find out what the comparable is doing inside the list<> interface and what is the function of this comparable array in this program
If you could edit your post and put the tags [code] and [/code] around them, it would be very helpful, as Fubarable said. If you could also give us an idea of what you think would work, that would help as well.
ok, lets do this one part at a time.Quote:
private final List<Comparable []> tuples= new ArrayList<Comparable []> ();
private - this is the scope. As private, it can only be used in this Class or it's nested classes
final - it will not be modified
List - the generic super-type which is extended by ArrayList and others
<> - Generics, specifies the type of data contained in the structure
Comparable - an interface. Anything that realizes this interface implements a compareTo() method. Thats all it means.
[] - an array
<Comparable []> - Specifies the data type as an array of objects that realize Comparable
tuples - your object name
new - instantiation
ArrayList - part of the collections framework. Basically a dynamic array (or dynamic vector).
ArrayList<Comparable []> - create an ArrayList that will be filled with arrays of Comparable objects. So, in english/pseudocode, you could read the statement as:
define a private unchanging list of comparable arrays, call it tuples, then create the tuples object and store an ArrayList of Comparable arrays there. You could have also written it like this:
Though, I agree with everyone else -- the day before this is due is a recipe for failure. You should have an understanding of this syntax weeks before an assignment is due (or at least days).Code:private final ArrayList<Comparable []> tuples= new ArrayList<Comparable []> ();
Good luck, I hope it becomes clear to you :D
thanks to every one,, it really helps
i hope u guys dont mind my ignorance
Correct, that is what I was trying to say, apparently it was misinterpreted. When I say make an int array, I mean int[] which is an array of int's, not an array that is itself comparable (is that even possible with regular arrays [] ?). I meant the same by Comparable array (array of comparables) but I can see how that could be confused.Quote:
It's a list of arrays that contain Comparable objects.
Thanks for addressing my semantics.
P.S. The OP was asking some of the same questions, so I summed everything together into one post that examined the statement piece by piece. I apologize if someone felt I was stepping on toes, I was simply trying to provide a different approach to help the OP. If you didn't like my post, that is fair, but lets let the original poster decide if I was helpful or not.