Guest User

OOP Suggestions

a guest
May 26th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. What I suggest is not necessarily a mistake, but you might find it easier to use an ArrayList instead of just a regular array for dealing with variable sized arrays especially if the array contains objects instead of just primitive types. With an ArrayList, you can add and remove elements like you would in a linked list however it still acts as an array. If you do use ArrayList for primitive types such as integers, you can do listOfIntegers.contains(5) to search listOfIntegers to see if the array contains the integer 5 or listOfNames.contains(“Bob”) to see if “Bob” is in your name list. ArrayList has a lot of other class functions too that you may find useful.
  2.  
  3. As a second suggestion, I would try to stay away from “tricks” such as the one found in your class constructor with i/13 and i%13:
  4.  
  5. public Deck(){
  6. for(int i = 0; i<52; i++)
  7. deckArray[i] = new Card(i/13,i%13);
  8. }
  9.  
  10. It's not really “clear” what this is doing. It requires “background” knowledge of what's going on. Of course in this case, the “background” knowledge is very simple and straight forward such as “13 cards per suit, so i/13 and i%13”. However, usually one the goals of OOP is to make things a lot simpler and to not rely on some trick or workaround that could break your code if not done correctly.
  11.  
  12. Lastly, classes are very good for locking down or controlling everything within the class. In general, the classes should modify the objects/variables within the class and not by anything else. If a variable within the class needs to be read or modified by the outside world, it should ask permission from the class. An example from your code:
  13.  
  14. for(int i = 0; i < numberOfPlayers; i++){
  15. players[i] = new Player(deck.dealCard(), deck.dealCard());
  16. System.out.println("Player" + (i+1) + ":"
  17. + "\n " + players[i].card1.cardToString()
  18. + "\n " + players[i].card2.cardToString()
  19. + "\n");
  20. }
  21.  
  22. With players[i].card1, card1 and card2 are exposed to external scopes. card1 and card2 should be private and if you want to read them or modify them, it should be done through a method such as players[i].getCard(0) where getCard(int) has control over what happens to card1 or card2. However, in Java, as opposed to C++, getCard(int) would still pass the object by reference and any code in external scopes would still be able to modify the object. What you would need to do is create a copy of the object and then return the copy of the object:
  23.  
  24. public Card getCard(int index) {
  25. Card card = null;
  26. if (index >= 0 && index <= cards.length) {
  27. card = new Card(cards[index].type, cards[index].value);
  28. }
  29. return card;
  30. }
  31.  
  32. In C++, you don't need to make a copy because it creates its own copy as long as you are not using pointers. In my Java solution to this challenge, I did not create a copy before returning an object. It's just something to keep in mind.
Advertisement
Add Comment
Please, Sign In to add comment