Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package egzaminoceny02;
  2.  
  3. class Oceny
  4. {
  5. private float[][] oceny = null;
  6.  
  7. public Oceny( int liczbaStudentow, int liczbaPrzedmiotow )
  8. {
  9. oceny = new float[liczbaStudentow][liczbaPrzedmiotow];
  10. for( int nrStud = 0; nrStud < oceny.length; ++nrStud )
  11. for( int nrPrzed = 0; nrPrzed < oceny[ nrStud ].length; ++nrPrzed )
  12. oceny[ nrStud ][ nrPrzed ] = -1;
  13. }
  14. public void wpiszOcene(int student, int przedmiot, float ocena )
  15. {
  16. if( student < 0 || student >= oceny.length )
  17. return;
  18. if( przedmiot < 0 || przedmiot >= oceny[ student ].length )
  19. return;
  20. if( ocena >= 2 && ocena <= 5 )
  21. oceny[ student ][ przedmiot ] = ocena;
  22. }
  23. public float obliczSredna( int student )
  24. {
  25. if( student < 0 || student >= oceny[ student ].length )
  26. return 0;
  27.  
  28. float suma = 0;
  29. int ileOcenWystawionych = 0;
  30. for( int nrPrzed = 0; nrPrzed < oceny[ student ].length; ++nrPrzed )
  31. if( oceny[ student ][ nrPrzed ] != -1 )
  32. {
  33. suma += oceny[ student ][ nrPrzed ];
  34. ileOcenWystawionych++;
  35. }
  36.  
  37. if( ileOcenWystawionych > 0 )
  38. return suma / ileOcenWystawionych;
  39. else
  40. return 0;
  41. }
  42. }
  43.  
  44. public class EgzaminOceny02 {
  45.  
  46. public static void main(String[] args)
  47. {
  48. Oceny dzienniczek = new Oceny( 5, 3 );
  49. dzienniczek.wpiszOcene( 0, 0, 5.0f );
  50. float srednia = dzienniczek.obliczSredna( 1 );
  51.  
  52. System.out.println( "Średnia: " + srednia );
  53. }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement