对我的代码中要写的内容感到困惑

时间:2017-10-17 07:52:02

标签: java

    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

e.g。

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

1 个答案:

答案 0 :(得分:-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;
    }
}

这应该对你有用