diff --git a/Datenanalyse.java b/Datenanalyse.java index e57aa49..3e6f230 100644 --- a/Datenanalyse.java +++ b/Datenanalyse.java @@ -1,37 +1,53 @@ import java.util.Scanner; import java.io.File; +int zaehleWerte(String dateiname) throws Exception { + Scanner sc = new Scanner(new File(dateiname), "UTF-8"); + int anzahl = 0; + while (sc.hasNextLine()) { + String zeile = sc.nextLine(); + if (!zeile.isBlank()) { + anzahl = anzahl + 1; + } + } + sc.close(); + return anzahl; +} + +int[] ladeMesswerte(String dateiname) throws Exception { + int anzahl = zaehleWerte(dateiname); + int[] werte = new int[anzahl]; + Scanner sc = new Scanner(new File(dateiname), "UTF-8"); + int index = 0; + while (sc.hasNextLine()) { + String zeile = sc.nextLine(); + if (!zeile.isBlank()) { + werte[index] = Integer.parseInt(zeile); + index = index + 1; + } + } + sc.close(); + return werte; +} + void main() throws Exception { -int summe = 0; -int anzahl = 0; -int maximum = Integer.MIN_VALUE; + int[] werte = ladeMesswerte("messwerte.txt"); -Scanner sc = new Scanner(new File("messwerte.txt"), "UTF-8"); + if (werte.length >= 1) { + Statistik s = new Statistik(); + int summe = s.berechneSumme(werte); + double durchschnitt = s.berechneDurchschnitt(werte); + int min = s.findeMinimum(werte); + int max = s.findeMaximum(werte); + int ueberSchnitt = s.zaehleUeberDurchschnitt(werte, durchschnitt); -while (sc.hasNextLine()) { -String zeile = sc.nextLine(); -if (zeile.isBlank()) { -continue; -} - -int eingabe = Integer.parseInt(zeile); - -summe = summe + eingabe; -anzahl = anzahl + 1; - -if (eingabe > maximum) { -maximum = eingabe; -} -} - -sc.close(); - -if (anzahl >= 1) { -double durchschnitt = (double) summe / anzahl; -IO.println("Anzahl: " + anzahl); -IO.println("Durchschnitt: " + durchschnitt); -IO.println("Maximum: " + maximum); -} else { -IO.println("Keine Daten eingegeben: "); -} + IO.println("Anzahl: " + werte.length); + IO.println("Summe: " + summe); + IO.println("Durchschnitt: " + durchschnitt); + IO.println("Minimum: " + min); + IO.println("Maximum: " + max); + IO.println("Ueber Durchschnitt: " + ueberSchnitt); + } else { + IO.println("Keine Daten gefunden."); + } } \ No newline at end of file diff --git a/Statistik.java b/Statistik.java index 052cce8..3747a3f 100644 --- a/Statistik.java +++ b/Statistik.java @@ -1,34 +1,43 @@ class Statistik { -int berechneSumme(int[] werte) { -int summe = 0; -for (int i = 0; i < werte.length; i++) { -summe = summe + werte[i]; -} -return summe; -} + int berechneSumme(int[] werte) { + int summe = 0; + for (int i = 0; i < werte.length; i++) { + summe = summe + werte[i]; + } + return summe; + } -double berechneDurchschnitt(int[] werte; -{ -return(double); -berechneSumme(werte) / werte.length; -} + double berechneDurchschnitt(int[] werte) { + return (double) berechneSumme(werte) / werte.length; + } -int findeMinimum(int[] werte) { -int minimum = Integer.MAX_VALUE; -for (int i = 0; i < werte.length; i++) { -if (werte[i] > maximum) { -maximum = werte[i]; -} -} -return maximum; -} + int findeMinimum(int[] werte) { + int minimum = Integer.MAX_VALUE; + for (int i = 0; i < werte.length; i++) { + if (werte[i] < minimum) { + minimum = werte[i]; + } + } + return minimum; + } -int zaehleUeberDurchschnitt(int[] werte, double durchschnitt) { -int treffer = 0; -for (int i = 0; i < werte.length; i++) { -if (werte[i] > durchschnitt) { -treffer = treffer + 1; -} -return treffer; -} -} + int findeMaximum(int[] werte) { + int maximum = Integer.MIN_VALUE; + for (int i = 0; i < werte.length; i++) { + if (werte[i] > maximum) { + maximum = werte[i]; + } + } + return maximum; + } + + int zaehleUeberDurchschnitt(int[] werte, double durchschnitt) { + int treffer = 0; + for (int i = 0; i < werte.length; i++) { + if (werte[i] > durchschnitt) { + treffer = treffer + 1; + } + } + return treffer; + } +} \ No newline at end of file