@w1024020103
2017-03-02T15:15:25.000000Z
字数 3446
阅读 535
CS61B
UCBerkeley
We'd like you to start your design process by considering the hardest operation first, namely "join". Before we get there, though, there is the matter of creating a basic table representation.
Often it's hard to tell exactly what you'll need until you start implementing. Especially for novice programmers, designing without writing any code can be very hard.
To get a better handle on this project, spend the rest of this lab creating a Table
class, which includes:
A constructor.
An addRow
method.
A main
method that creates table T1
from this lab using the constructor and addRow
methods.
There is no specific API for this task. That is, you can use whatever signatures you want for the constructor and addRow
methods. Pick any instance variables that you think will allow you to achieve those objectives.
It's OK to assume that all types are int for now.
Some recommendations: Your constructor should not take a collection of rows. The number and names of the columns should be immutable (See 2/15 lecture for a definition of immutability).
I have a problem reading input file as String.
my code for main method:
public static void main(String[] args) {
In in = new In(args[0]);
int numOfCol = in.readInt();
int numOfRow= in.readInt();
String [][] rowCol = new String [numOfRow][numOfCol];
for (int i = 0; i < numOfRow; i++){
for (int j = 0; j < numOfCol; j++){
rowCol[i][j] = in.readString();
}
}
Table T1 = new Table("T1", rowCol);
T1.addRow(new String []{"1","4"});
T1.addRow(new String []{"2","5"});
T1.addRow(new String []{"3","6"});
System.out.println(T1.tableName);
System.out.println(T1.numOfCol);
System.out.println(T1.table);
}
my input file T1.txt
2 4
x int, y int
2, 5
8, 3
13, 7
I got error when running main method, and I debugged to find out that in.readString read item that are seperated by whitespace, so it thinks that my first string is x
while it should actually be x int
:
I then modified my input txt file to:
2 4 X int
Y int
2 5
8 3
13 7
And for the column names, I used in.readLine()
method to extract the datas from the argument file after I used in.readInt()
to extract int datas for numOfCol
and numofRow
.
Main method:
public static void main(String[] args) {
In in = new In(args[0]);
int numOfCol = in.readInt();
int numOfRow = in.readInt();
String [][] rowCol = new String [numOfRow][numOfCol];
rowCol[0][0] = in.readLine();
rowCol[0][2] = in.readLine();
for (int i = 1; i < numOfRow; i++){
for (int j = 0; j < numOfCol; j++){
rowCol[i][j] = in.readString();
}
}
Table T1 = new Table("T1", rowCol);
System.out.println(T1.tableName);
System.out.println(T1.numOfCol);
System.out.println(Arrays.toString(T1.colName));
System.out.println(Arrays.deepToString(T1.table).replace("], ", "]\n"));
NOTE:
Pay attention to Arrays.toString()
and Arrays.deepToString(T1.table)
, in this case I want to print the 2D array like a table, so I used Arrays.deepToString(T1.table).replace("], ", "]\n"));
If you don't know how to properly use these methods, your output would be some strange things.
Output:
But when I added addRow()
method, I got an ArrayIndexOutOfBoundsException
, it's likely that my table
didn't increment its numOfRow
somehow.
Through debugging, I realized that the numOfRow
of the table
has never changed, in fact only the field variable numOfRow
has changed. And table[][]
only initialized once in the constructor, so after it has initialized with the given numOfRow
, its numOfRow
will never change even though the field variable numOfRow
of Table class has incremented. Knowing this, I added several lines in my addRow()
method.
public void addRow(String[] rowToAdd){
if(rowToAdd == null){
return;
}
numOfRow += 1;
String[][] temp = table;
table = new String[numOfRow][numOfCol];
System.arraycopy(temp,0,table,0,temp.length);
table[numOfRow - 1] = rowToAdd;
}
In this way, I newed a table
object again and thus incrementing its numOfRow
, copied everything from the previous table and added the new row following the last line.