1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author arn
*/
import java.util.Scanner;
public class Statistics {
final int ARRAY_LEN = 5; // nombre max d'éléments du tableau
int [] numbers = new int[ARRAY_LEN];
// remplit le tableau avec des valeurs saisies à la console
private void fillArray() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < ARRAY_LEN; i++) {
numbers[i] = scanner.nextInt();
}
scanner.close();
}
// affiche le contenu du tableau à la console
public void displayArray() {
for (int i = 0; i < ARRAY_LEN; i++) {
System.out.print(numbers[i] + ", ");
}
System.out.println();
}
// calcul la valeur maximale
private int getMaxValue() {
int temp = numbers[0];
for (int i = 1; i < ARRAY_LEN; i++) {
if (temp < numbers[i]) {
temp = numbers[i];
}
}
return temp;
}
// calcul la valeur minimale
public int getMinValue() {
int temp = numbers[0];
for (int i = 1; i < ARRAY_LEN; i++) {
if (temp > numbers[i]) {
temp = numbers[i];
}
}
return temp;
}
// calcul la valeur moyenne
public float getMoy() {
float sum=0;
for(int i=0;i<ARRAY_LEN;i++){
sum+=numbers[i];
}
return sum/ARRAY_LEN;
}
// calcul l'écart type
public float getecartype(){
float exm=0.0f;
float moyenne=getMoy();
for(int i=1;i<ARRAY_LEN;i++){
exm+=(numbers[i]-moyenne)*(numbers[i]-moyenne);
}
return (float)Math.sqrt((double)(exm/ARRAY_LEN));
}
// pour l'histogramme
// public void histogramme(int tab[],int tailleEffective,int retab,int [] nbNiveau, int [] borneNiveau){
// int i=0;
// int j=0;
/ / int nbNiveau[10];
// while(tab[i]>borneNiveau[j]){
// j++;
// frequence[j]++;
// }
// }
// affichage de l'histogramme l'histogramme
//
public static void main(String[] args) {
Statistics stats = new Statistics();
System.out.print("Tableau avant la saisie: ");
stats.displayArray();
stats.fillArray();
System.out.print("Tableau après la saisie: ");
stats.displayArray();
System.out.print("Tableau après le calcul: ");
stats.displayArray();
System.out.println("Valeur maximale est: " + stats.getMaxValue());
System.out.println("Valeur minimale est: " + stats.getMinValue());
System.out.println("Valeur moyenne est: " + stats.getMoy());
System.out.println("l'écart-type est: " + stats.getecartype()); |
Partager