1
0
Fork 0

counterOfCellsWithValue() Methode ergänzt. Angepasste mit() Methode

main
Anastasia Kisner 2024-01-07 16:38:23 +01:00
parent 111b4a36eb
commit a0117ae961
1 changed files with 22 additions and 18 deletions

View File

@ -140,6 +140,27 @@ public class Spreadsheet {
cells[row][col].setValue("" + result);
}
/**
* Method for calculating the amount of cells with values inside a rectangular block of cells, such as from A1 to B3.
* @param startCellName The name of the cell in the upper left corner of the rectangle.
* @param endCellName The name of the cell in the lower right corner of the rectangle.
* @return The counted cells with values calculated.
*/
private long counterOfCellsWithValue(String startCellName, String endCellName){
long counter = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A'));
if(!value.isEmpty()){
counter++;
}
}
}
return counter;
}
/**
* Method for calculating the sum of a rectangular block of cells, such as from A1 to B3.
* @param startCellName The name of the cell in the upper left corner of the rectangle.
@ -199,25 +220,8 @@ public class Spreadsheet {
* @return The arithmetic average calculated.
*/
private long mit(String startCellName, String endCellName) {
long result = 0;
int counter = 0;
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
String value = get((row - 1), (col - 'A'));
if(!value.isEmpty()){
result += Long.parseLong(value);
}else{
continue;
}
counter++;
}
}
result /= counter;
return result;
return sum(startCellName, endCellName)/ counterOfCellsWithValue(startCellName, endCellName);
}
/**