Advertisement
Guest User

glogangornogang

a guest
Oct 9th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1.  
  2. package tp3;
  3.  
  4. /**
  5. *
  6. * @author p1410544
  7. */
  8.  
  9. public class matrice // 1. Classe matrice
  10. {
  11. double tab [][]; // 1. Attribut : tableau 2 dim°
  12.  
  13. matrice ( double zzz [][] ) // 2. Construct° + Param. double [][]
  14. {
  15. tab = new double [zzz.length][];
  16. for(int i=0 ; i < zzz.length ; i++)
  17. {
  18. tab[i]= new double [zzz[i].length];
  19. }
  20. for(int i=0 ; i < tab.length ; i++)
  21. {
  22. for(int j=0 ; j < tab[i].length ; j++)
  23. {
  24. tab[i][j]= zzz[i][j];
  25. }
  26. }
  27. }
  28.  
  29. void affiche() // 3. Meth. affichage attribut
  30. {
  31. for(int i=0 ; i < tab.length ; i++)
  32. {
  33. for(int j=0; j < tab[i].length ; j++)
  34. {
  35. System.out.print(tab[i][j] + " ");
  36. }
  37. System.out.println("\n");
  38. }
  39. }
  40.  
  41. public double somme(int a) // 4. Somme élè i-ème ligne
  42. {
  43. double Resultat=0;
  44. for(int i=0 ; i < tab[a].length ; i++)
  45. {
  46. Resultat= Resultat + tab[a][i];
  47. }
  48. return Resultat;
  49. }
  50.  
  51. public int nombre() // 5. Methode compte nb. élé du tableau.
  52. {
  53. int Resultat = 0;
  54. for(int i=0 ; i< tab.length ; i++)
  55. {
  56. for(int j=0 ; j < tab[i].length ; j++)
  57. {
  58. Resultat = Resultat + 1;
  59. }
  60. }
  61. return Resultat;
  62. }
  63.  
  64. public double sommetout() // 6. Somme de tous les les élé de la matrice
  65. {
  66. double res=0;
  67. for(int i=0;i<tab.length;i++)
  68. {
  69. res=res+ somme(i);
  70. }
  71. return res;
  72. }
  73.  
  74. public double moyenne() // 7. Meth. moyenne dees éléments de la matrice
  75. {
  76. return sommetout()/nombre();
  77. }
  78.  
  79. public double plusgrand() // 8. Methode qui retourne element le + grand de la matrice
  80. {
  81. double Resultat = 0 ;
  82. for(int i=0 ; i < tab.length ; i++)
  83. {
  84. for(int j=0 ; j< tab[i].length ; j++)
  85. {
  86. if( tab[i][j] > Resultat)
  87. {
  88. Resultat = tab[i][j];
  89. }
  90. }
  91. }
  92. return Resultat;
  93. }
  94.  
  95. public void present(double a) // 9. Meth. qui indique si le double passé en param° est présent dans le tableau
  96. {
  97. boolean rep = false;
  98. for(int i=0 ; i < tab.length ; i++){
  99. for(int j=0 ; j < tab[i].length ; j++){
  100. if(tab[i][j]== a )
  101. rep = true;
  102. }
  103. }
  104.  
  105. if(rep == true)
  106. System.out.println("La valeur "+a+" est presente dans la matrice \n");
  107.  
  108. else
  109. System.out.println("La valeur "+a+" n'est pas presente dans la matrice \n");
  110. }
  111.  
  112.  
  113. public static void main(String[] args) // 10. Main qui test toutes les méthodes
  114. {
  115. double b[][] = {{ Math.round(Math.random()*51), Math.round(Math.random()*51)},{Math.round(Math.random()*51)},{Math.random()*11,Math.random()*11},{Math.random()*11,Math.random()*11,Math.random()*11}}; // 2. nb lignes =/= nb de colonnes
  116. matrice un= new matrice (b); // 2. Creation objet dans fonction main
  117. un.affiche();
  118. System.out.println("- La somme de la ligne 0 est "+un.somme(0));
  119. System.out.println("- La somme de la ligne 1 est "+un.somme(1));
  120. System.out.println("- La somme de la ligne 2 est "+un.somme(2));
  121. System.out.println("- La somme de la ligne 3 est "+un.somme(3));
  122. System.out.println("- il y a "+un.nombre()+" éléments");
  123. System.out.println("- La somme totale est "+un.sommetout());
  124. System.out.println("- La moyenne est "+un.moyenne());
  125. System.out.println("- La plus grande valeur est "+un.plusgrand()+"\n");
  126. un.present(3);
  127. un.present(10);
  128.  
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement