Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package egzaminoceny02;
- class Oceny
- {
- private float[][] oceny = null;
- public Oceny( int liczbaStudentow, int liczbaPrzedmiotow )
- {
- oceny = new float[liczbaStudentow][liczbaPrzedmiotow];
- for( int nrStud = 0; nrStud < oceny.length; ++nrStud )
- for( int nrPrzed = 0; nrPrzed < oceny[ nrStud ].length; ++nrPrzed )
- oceny[ nrStud ][ nrPrzed ] = -1;
- }
- public void wpiszOcene(int student, int przedmiot, float ocena )
- {
- if( student < 0 || student >= oceny.length )
- return;
- if( przedmiot < 0 || przedmiot >= oceny[ student ].length )
- return;
- if( ocena >= 2 && ocena <= 5 )
- oceny[ student ][ przedmiot ] = ocena;
- }
- public float obliczSredna( int student )
- {
- if( student < 0 || student >= oceny[ student ].length )
- return 0;
- float suma = 0;
- int ileOcenWystawionych = 0;
- for( int nrPrzed = 0; nrPrzed < oceny[ student ].length; ++nrPrzed )
- if( oceny[ student ][ nrPrzed ] != -1 )
- {
- suma += oceny[ student ][ nrPrzed ];
- ileOcenWystawionych++;
- }
- if( ileOcenWystawionych > 0 )
- return suma / ileOcenWystawionych;
- else
- return 0;
- }
- }
- public class EgzaminOceny02 {
- public static void main(String[] args)
- {
- Oceny dzienniczek = new Oceny( 5, 3 );
- dzienniczek.wpiszOcene( 0, 0, 5.0f );
- float srednia = dzienniczek.obliczSredna( 1 );
- System.out.println( "Średnia: " + srednia );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement