Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package mikołaje;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import gwiazdka.Prezent;
  6.  
  7. public class Worek {
  8. int pojemność;
  9. int ilePrezentów = 0;
  10. int ileMaks;
  11. Prezent[] zawartość;
  12.  
  13. int temp = pojemność;
  14.  
  15. public Worek(int pojemność, int ileMaks){
  16. this.pojemność = pojemność;
  17. this.ileMaks = ileMaks;
  18. this.zawartość = new Prezent[ileMaks];
  19. }
  20. //DODAJ PREZENT
  21. public void dodajPrezent(Prezent prezent){
  22. if(ilePrezentów<ileMaks ) { //<------------ bez tego wyskakuje out of bound; próbowałem też "&& temp >= 0" ale wtedy bierze mikołaj 1 prezent i kończy zabawę
  23. zawartość[ilePrezentów] = prezent;
  24. ilePrezentów += 1;
  25. temp -= prezent.waga();
  26. }
  27. }
  28. @Override
  29. public String toString(){
  30. return "Worek{pojemność= " + pojemność + ", l.prez. = " + ilePrezentów + "/" + ileMaks + ", zawartość =(" + Arrays.deepToString(zawartość) + ")";
  31. }
  32. public int pojemność(){
  33. return pojemność;
  34. }
  35. public int ilePrezentów() {
  36. return ilePrezentów;
  37. }
  38. public int ileMaks() {
  39. return ileMaks;
  40. }
  41. public Prezent[] zawartość(){
  42. return zawartość;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement