[关闭]
@w1024020103 2017-03-02T15:15:25.000000Z 字数 3446 阅读 535

Lab 5 Notes

CS61B UCBerkeley


Part I: Getting Acquainted

Part II: Prototyping and Design

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.

Exploratory Programming

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:

  1. public static void main(String[] args) {
  2. In in = new In(args[0]);
  3. int numOfCol = in.readInt();
  4. int numOfRow= in.readInt();
  5. String [][] rowCol = new String [numOfRow][numOfCol];
  6. for (int i = 0; i < numOfRow; i++){
  7. for (int j = 0; j < numOfCol; j++){
  8. rowCol[i][j] = in.readString();
  9. }
  10. }
  11. Table T1 = new Table("T1", rowCol);
  12. T1.addRow(new String []{"1","4"});
  13. T1.addRow(new String []{"2","5"});
  14. T1.addRow(new String []{"3","6"});
  15. System.out.println(T1.tableName);
  16. System.out.println(T1.numOfCol);
  17. System.out.println(T1.table);
  18. }

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:

image_1ba53f7k4q9ur34jiqp3t1dti9.png-95.8kB

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:

  1. public static void main(String[] args) {
  2. In in = new In(args[0]);
  3. int numOfCol = in.readInt();
  4. int numOfRow = in.readInt();
  5. String [][] rowCol = new String [numOfRow][numOfCol];
  6. rowCol[0][0] = in.readLine();
  7. rowCol[0][2] = in.readLine();
  8. for (int i = 1; i < numOfRow; i++){
  9. for (int j = 0; j < numOfCol; j++){
  10. rowCol[i][j] = in.readString();
  11. }
  12. }
  13. Table T1 = new Table("T1", rowCol);
  14. System.out.println(T1.tableName);
  15. System.out.println(T1.numOfCol);
  16. System.out.println(Arrays.toString(T1.colName));
  17. 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:
2.JPG-14.6kB

But when I added addRow() method, I got an ArrayIndexOutOfBoundsException, it's likely that my table didn't increment its numOfRow somehow.
3.JPG-43.9kB

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.

  1. public void addRow(String[] rowToAdd){
  2. if(rowToAdd == null){
  3. return;
  4. }
  5. numOfRow += 1;
  6. String[][] temp = table;
  7. table = new String[numOfRow][numOfCol];
  8. System.arraycopy(temp,0,table,0,temp.length);
  9. table[numOfRow - 1] = rowToAdd;
  10. }

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.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注