Facebook
From Adrian Pietrzak, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 192
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Calculations {
  6.  
  7.     private int[] tab;
  8.     private int numberOfVariables;
  9.  
  10.     private int max = Integer.MAX_VALUE;
  11.     private int min = Integer.MIN_VALUE;
  12.     private double sum = 0;
  13.     private double average = 0;
  14.  
  15.     public int getMax() {
  16.         return max;
  17.     }
  18.  
  19.     public int getMin() {
  20.         return min;
  21.     }
  22.  
  23.     public void setMax(int max) {
  24.         this.max = max;
  25.     }
  26.  
  27.     public void setMin(int min) {
  28.         this.min = min;
  29.     }
  30.  
  31.     public void setSum(double sum) {
  32.         this.sum = sum;
  33.     }
  34.  
  35.     public double getSum() {
  36.         return sum;
  37.     }
  38.  
  39.     public void setNumberOfVariables(int numberOfVariables) {
  40.         this.numberOfVariables = numberOfVariables;
  41.     }
  42.  
  43.     public int getNumberOfVariables() {
  44.         return numberOfVariables;
  45.     }
  46.  
  47.     public void setAverage(double average) {
  48.         this.average = average;
  49.     }
  50.  
  51.     public double getAverage() {
  52.         return average;
  53.     }
  54.  
  55.     public void createTableForEnteringValue()
  56.     {
  57.         Scanner numberOfVariablesScan = new Scanner(System.in);
  58.         setNumberOfVariables(numberOfVariablesScan.nextInt());
  59.         tab = new int[numberOfVariables];
  60.     }
  61.  
  62.     public void calculateEnteringValue()
  63.     {
  64.         Scanner valueScan = new Scanner(System.in);
  65.         for (int i = 0; i < numberOfVariables; ++i) {
  66.             Dialog.inputValue();
  67.             tab[i] = valueScan.nextInt();
  68.  
  69.             setSum(sum += tab[i]);
  70.  
  71.             if (tab[i] < min) {
  72.                 setMin(tab[i]);
  73.             }
  74.  
  75.             if (tab[i] > max) {
  76.                 setMax(tab[i]);
  77.             }
  78.         }
  79.  
  80.         setAverage(getSum() / getNumberOfVariables());
  81.     }
  82.  
  83.     public void presentsResult()
  84.     {
  85.         System.out.println(String.format("Ilość podanych liczb: %d", getNumberOfVariables()));
  86.         System.out.println(String.format("Max: %d", getMax()));
  87.         System.out.println(String.format("Min: %d", getMin()));
  88.         System.out.print(String.format("Średnia: %f", getAverage()));
  89.     }
  90. }
  91.  
  92. public class Dialog {
  93.  
  94.     static void isHowManyNumbersQuestion()
  95.     {
  96.         System.out.print("Ile liczb chcesz wczytać?: ");
  97.     }
  98.  
  99.     static void inputValue()
  100.     {
  101.         System.out.print("Wprowadź liczbę: ");
  102.     }
  103.  
  104. }
  105.  
  106. public class Main {
  107.     public static void main(String[] args) {
  108.         Calculations calculations = new Calculations();
  109.  
  110.         Dialog.isHowManyNumbersQuestion();
  111.         calculations.createTableForEnteringValue();
  112.         calculations.calculateEnteringValue();
  113.         calculations.presentsResult();
  114.     }
  115. }