JavaDoc comments

Added javadoc comments for the remaining methods in Spreadsheet, fixed a small bug in the clearTable method, Added javadoc comments for all methods in Cell
Victor Hans-Georg Waitz 2024-01-02 12:08:50 +01:00
parent 6e17bd2434
commit f922de14d2
2 changed files with 87 additions and 6 deletions

View File

@ -10,27 +10,51 @@ public class Cell {
private String formula = ""; private String formula = "";
private String value = ""; private String value = "";
/**
* Public method to get the formula of the cell.
* @return formula of the cell.
*/
public String getFormula() { public String getFormula() {
return formula; return formula;
} }
/**
* Public method to set the formula of the cell.
* @param formula formula of the cell.
*/
public void setFormula(String formula) { public void setFormula(String formula) {
if (!formula.isEmpty()) if (!formula.isEmpty())
this.formula = formula.toUpperCase().substring(1); // removes '=' at the beginning this.formula = formula.toUpperCase().substring(1); // removes '=' at the beginning
} }
/**
* Public method to get the value of the cell.
* @return value of the cell
*/
public String getValue() { public String getValue() {
return value; return value;
} }
/**
* Public method to set the value of the cell.
* @param value value of the cell.
*/
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
/**
* Public method to get cell value as a string.
* @return value as string.
*/
public String toString() { public String toString() {
return String.format("%4s", value); return String.format("%4s", value);
} }
/**
* Public method to check if the cell is empty.
* @return true if the cell is empty, false otherwise.
*/
public boolean isEmpty() { public boolean isEmpty() {
return value.isEmpty(); return value.isEmpty();
} }

View File

@ -45,15 +45,33 @@ public class Spreadsheet {
// ----- // -----
// retrieve or change values of cells // retrieve or change values of cells
/**
* Private method to get a value from a cell in a spreadsheet.
* @param row The row of the cell.
* @param col The column of the cell.
* @return The value of the cell.
*/
private String get(int row, int col) { private String get(int row, int col) {
return cells[row][col].getValue(); return cells[row][col].getValue();
} }
/**
* Public method to get a value from a cell in a spreadsheet.
* @param cellName The name of the cell.
* @return The value of the cell.
*/
public String get(String cellName) { public String get(String cellName) {
cellName = cellName.toUpperCase(); cellName = cellName.toUpperCase();
return get(getRow(cellName), getCol(cellName)); return get(getRow(cellName), getCol(cellName));
} }
/**
* Private methode to put a value into a cell in a spreadsheet.
* @param row The row of the cell.
* @param col The column of the cell.
* @param value The value to put into the cell.
* @return Nothing.
*/
private void put(int row, int col, String value) { private void put(int row, int col, String value) {
if (!value.startsWith("=")) if (!value.startsWith("="))
cells[row][col].setValue(value); cells[row][col].setValue(value);
@ -64,15 +82,32 @@ public class Spreadsheet {
} }
} }
/**
* Public method to put a value into a cell in a spreadsheet.
* Calls private methode put.
* @param cellName The name of the cell.
* @param value The value to put into the cell.
* @return Nothing.
*/
public void put(String cellName, String value) { public void put(String cellName, String value) {
cellName = cellName.toUpperCase(); cellName = cellName.toUpperCase();
put(getRow(cellName), getCol(cellName), value); put(getRow(cellName), getCol(cellName), value);
} }
/**
* Method to get the column of a cell in a spreadsheet.
* @param cellName The name of the cell.
* @return The column of the cell.
*/
public int getCol(String cellName) { public int getCol(String cellName) {
return cellName.charAt(0) - 'A'; return cellName.charAt(0) - 'A';
} }
/**
* Method to get the row of a cell in a spreadsheet.
* @param cellName The name of the cell.
* @return The row of the cell.
*/
public int getRow(String cellName) { public int getRow(String cellName) {
String row = ""; String row = "";
for (int i = 1; i < cellName.length(); i++){ for (int i = 1; i < cellName.length(); i++){
@ -428,6 +463,7 @@ public class Spreadsheet {
} }
/** /**
*
* This method calculates the result of a "normal" algebraic expression. It only needs to support * This method calculates the result of a "normal" algebraic expression. It only needs to support
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus, * expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
* minus, times, split only. An expression always starts with either a number or a cell name. If it * minus, times, split only. An expression always starts with either a number or a cell name. If it
@ -546,14 +582,26 @@ public class Spreadsheet {
// ----- // -----
/**
* Returns the number of rows in the spreadsheet.
* @return amount rows.
*/
public int getRowsLCount() { public int getRowsLCount() {
return cells.length; return cells.length;
} }
/**
* Returns the number of columns in the spreadsheet.
* @return amount columns.
*/
public int getColsCount() { public int getColsCount() {
return cells[0].length; return cells[0].length;
} }
/**
* Clears the spreadsheet, it fills every value and formula to zero.
* @return Nothing.
*/
public void clearTable() { public void clearTable() {
int rows = getRowsLCount(); int rows = getRowsLCount();
int cols = getColsCount(); int cols = getColsCount();
@ -561,10 +609,15 @@ public class Spreadsheet {
for (int r = 0; r < rows; r++) { for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) { for (int c = 0; c < cols; c++) {
cells[r][c].setValue(""); cells[r][c].setValue("");
cells[r][c].setFormula("");
} }
} }
} }
/**
* Loads a special table for testing purposes.
* @return Nothing.
*/
public void loadSpecialTable(){ public void loadSpecialTable(){
clearTable(); clearTable();
@ -585,6 +638,10 @@ public class Spreadsheet {
put("G5", "3"); put("G5", "3");
} }
/**
* Returns a string representation of the spreadsheet.
* @return The spreadsheet as a string.
*/
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();