2017-10-17 62 views
-4
public class Client { 
    private ArrayList<DataEntry> data; 
    private String title; 

    /** 
    * create a new worksheet with given title 
    * @param title 
    */ 
    public Worksheet(String title) { 
     data = new ArrayList<DataEntry>(); 
     this.title = title; 
    } 

    /** 
    * @return a shallow copy of the data 
    */ 
    public ArrayList<DataEntry> getData() { 
     return data; 
    } 

    /** 
    * 
    * @return title of the worksheet 
    */ 
    public String getTitle() { 
     return title; 
    } 


    /** 
    * 
    * @param row 
    * @param column 
    * @return value of item at given row and column (if any), null otherwise 
    */ 
    public Double get(int row, int column) { 

     return null; // to be completed 
    } 

    /** 
    * set the value of DataEntry object at given row and column to given value 
    * 
    * if a DataEntry object for given row and column already exists, overwrite the current value 
    * if a DataEntry object for given row and column doesn't exist, add a new DataEntry object 
    * with given row, column, value to the list. 
    * @param row 
    * @param column 
    * @param val 
    */ 
    public void set(int row, int column, double val) { 



     //to be completed 
    } 

    /** 
    * 
    * @param row 
    * @param column 
    * @return index of DataEntry object in list data with given row and column 
    * return -1 if no such DataEntry object found 
    */ 
    public int indexOf(int row, int column) { 
     this.get(row, column); 
     return 0; //to be completed 
    } 
} 

我被授予此代码以供将来的考试练习。我不知道如何完成任何任务。任何帮助理解做什么将不胜感激!谢谢!!对我在代码中写什么感到困惑

P.S.我还有另一个名为DataEntry的java类,它包含一系列具有标头的setter和getter,如公共

public void setRow(int r) { 
    row = Math.max(0, r); 
    } 
+0

阅读上面的方法的注释工作,这说明了什么样的行为应该预计 –

+0

我读过我只是不知道该怎么里面写评论方法。行和列没有创建变量 –

+0

看看你的'ArrayList',它应该包含什么类型的对象?这应该是你需要做什么的一个重要暗示。 –

回答

-1
import java.util.ArrayList; 

public class Worksheet { 
    private ArrayList<DataEntry> data; 
    private String title; 

    /** 
    * create a new worksheet with given title 
    * 
    * @param title 
    */ 
    public Worksheet(String title) { 
     data = new ArrayList<DataEntry>(); 
     this.title = title; 
    } 

    /** 
    * @return a shallow copy of the data 
    */ 
    public ArrayList<DataEntry> getData() { 
     return data; 
    } 

    /** 
    * @return title of the worksheet 
    */ 
    public String getTitle() { 
     return title; 
    } 

    /** 
    * @param row 
    * @param column 
    * @return value of item at given row and column (if any), null otherwise 
    */ 
    public Double get(int row, int column) { 

     Double retVal = null; 
     for (DataEntry dataEntry : data) { 
      if (dataEntry.getColumn() == column && dataEntry.getRow() == row) { 
       retVal = dataEntry.getValue(); 
      } 
     } 
     return retVal; 
    } 

    /** 
    * set the value of DataEntry object at given row and column to given value 
    * <p> 
    * if a DataEntry object for given row and column already exists, overwrite the current value 
    * if a DataEntry object for given row and column doesn't exist, add a new DataEntry object 
    * with given row, column, value to the list. 
    * 
    * @param row 
    * @param column 
    * @param val 
    */ 
    public void set(int row, int column, double val) { 
     boolean isNew = true; 
     for (DataEntry dataEntry : data) { 
      if (dataEntry.getColumn() == column && dataEntry.getRow() == row) { 
       dataEntry.setValue(val); 
       isNew = false; 
      } 
     } 
     if (isNew) { 
      DataEntry newData = new DataEntry(); 
      newData.setColumn(column); 
      newData.setRow(row); 
      newData.setValue(val); 
      data.add(newData); 

     } 

    } 

    /** 
    * @param row 
    * @param column 
    * @return index of DataEntry object in list data with given row and column 
    * return -1 if no such DataEntry object found 
    */ 
    public int indexOf(int row, int column) { 
     Double value = this.get(row, column); 
     if (value != null) { 
      for (DataEntry dataEntry : data) { 
       if (dataEntry.getColumn() == column && dataEntry.getRow() == row) { 
        return data.indexOf(dataEntry); 
       } 
      } 
     } 
     return -1; 
    } 
} 

这应该为你