I need to write a multiply-linked data structue program in java for a number gri
ID: 3623298 • Letter: I
Question
I need to write a multiply-linked data structue program in java for a number grid that provide a grid with ten rows and six columns, the cell in the grid can hold and display either a number(a double) or a string. operations will be provided which display the grid, assign values to the cell, do arithmetic on cell, do arithmetic on rows and coluns, fill cells with values, inset, delete and move rows and columns. the default value for all the cells is an empty string. The program will present a menu of operations to perform. when inputs is taken from the user for a cell value any input beginning with a double quote mark is treated as a string, but the quote mark is not included in the string. any other input is assumed to be a number and is accepted as a double.Explanation / Answer
This is too big of a project for me to give the code to do all the operations, but I can get you started and show you the kinds of data structures you would need to have. The way I approached it, we have a class Cell which contains the data for a single cell. Each Cell is contained both in a Row object and also in a Column object. The Grid object holds a list of the grid's rows and a list of the grid's columns. When you do an operation affecting the layout of the grid, for instance inserting a column, you have to take into account the effects on both rows and columns. In the case of inserting a column, you need to insert a cell into each Row object in addition to inserting a new Column object into the grid.
I gave methods for assigning values to cells and for inserting a column. Hopefully this is enough to give you a good start.
import java.util.ArrayList;
import java.util.List;
public class Grid {
private List<Row> rows;
private List<Column> columns;
public void assign(int row, int col, double value) {
rows.get(row).assign(col, value);
}
public void assign(int row, int col, String value) {
rows.get(row).assign(col,value);
}
public void insertColumn(int index) {
Column column = new Column(columns.get(0).size());
columns.add(index,column);
for (int i=0; i < rows.size(); i++) {
rows.get(i).insertCell(index,column.getCell(i));
}
}
private enum DataType {
STRING,DOUBLE
}
private abstract class CellList {
private List<Cell> cells;
public CellList (int size) {
cells = new ArrayList<Cell>(size);
for (int i=0; i<size; i++) {
cells.add(new Cell());
}
}
public void insertCell(int i, Cell cell) {
cells.add(i, cell);
}
public void assign(int index, double value) {
cells.get(index).assign(value);
}
public void assign(int index, String value) {
cells.get(index).assign(value);
}
public int size() {
return cells.size();
}
public Cell getCell(int i) {
return cells.get(i);
}
}
private class Row extends CellList {
public Row (int size) {
super(size);
}
}
private class Column extends CellList {
public Column (int size) {
super(size);
}
}
private class Cell {
private String stringData;
private double doubleData;
private DataType dataType;
public Cell() {
stringData = "";
dataType = DataType.STRING;
}
public String getStringData() {
if (dataType.equals(DataType.STRING)) {
return stringData;
} else {
throw new UnsupportedOperationException();
}
}
public double getDoubleData() {
if (dataType.equals(DataType.DOUBLE)) {
return doubleData;
} else {
throw new UnsupportedOperationException();
}
}
public void assign(double value) {
doubleData = value;
dataType = DataType.DOUBLE;
}
public void assign(String value) {
stringData = value;
dataType = DataType.STRING;
}
}
}