From a0117ae9612655ce69463240d1dac6d6fd89635a Mon Sep 17 00:00:00 2001 From: 3011357 <3011357@stud.hs-mannheim.de> Date: Sun, 7 Jan 2024 16:38:23 +0100 Subject: [PATCH] =?UTF-8?q?counterOfCellsWithValue()=20Methode=20erg=C3=A4?= =?UTF-8?q?nzt.=20Angepasste=20mit()=20Methode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../informatik/spreadsheet/Spreadsheet.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index cd6e2f6..53ccd58 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -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); } /**