User:Alexsmail/Computer programming/Object oriented

Link on the related topics: Functional programming.

There is also a great book Object Oriented Programming. That books is describing OO on the programmer level. If you want to start to write rapidly OO or you want to understand existing program, you should read that book. If you want to understand philosophy of OO, if you want to learn theory of OO, this book is for you.

This book can be view as continue of Procedural programming. If one want to continue to learn object oriented, it is recommended to start from the previous book first.

Table of content:

  1. Objects are entities with state&methods. Twenty one game (version of Blackjack).
    a. Struct as historical predecessor.
    b. Classes, methods and constructors overview.
  2. Constructor. This. Null.
  3. Default Constructor, Copy constructor, Assignment operator.
  4. Examples: Fraction, Door, Rectangle.
  5. Wrapper class.
  6. Array is the Object.
  7. Exceptions. In common and in Java. Declare or throw. Runtime exception.
  8. String, Stream, System.
  9. Static & state.
  10. Inheritance. Object in C++ - abstract class. Pure abstract class. Inline function.
  11. Packages. Public.
    a. Access management and inheritance.
  12. Polymorphisms. Casting.
  13. Re-use. Client-server. Black box.
  14. Functions again. Overloading. Redefinition and field&methods hiding. super.
  15. Functions again (cont). Parameters pass by value and by reference. Swap.
  16. Interface. Splitting of what class should do, and how does it do. Design pattern - factory.
  17. Again about final. Variable, method, class.
  18. End.

Objects are entities with state&methods. Twenty one game (version of Blackjack).

edit

Real world example

edit

Consider the real world example. You want to go out the room. You start to think how this mission can be accomplished. To make things simple, consider only the last faze of this mission, you are actually standing near the door. So, you think that you should just open the door and live the room, right? Consider that you actually percepts the world by entities: you distinguishes yourself, and the door from your environment. This entities has some interesting properties. The door can be closed or open, can have lock or not, for example. But actually, you don't care. You know, that you just want to open the door, and you know how this can be done to any door.

While I'll return to the previous example later on this book, meanwhile I want to concentrate on another example - the game twenty one (simplified version of Blackjack).

Much of blackjack's popularity is due to the mix of chance with elements of skill, and the publicity that surrounds card counting (keeping track of which cards have been played since the last shuffle). Blackjack's precursor was vingt-et-un ("twenty-one"), which originated in French casinos around 1700, and did not offer the 3:2 bonus for a two-card 21. I want to introduce very simple version of this game. I will call this simplified version twenty one through out the book.

There are two players on this game. Let name the first player a dealer. Dealer takes the card and gives one card to the second player, only second player sees the cards. Second player has three options - to ask more card, to surrendered or to stay, that is to say enough cards for now. Every card has number of points associated with it. Cards 2 through 10 are worth their face value, and face cards (jack, queen, king) are also worth 10. Ace will be considered to be 11 always. The goal of the game is to have the highest total as long as it doesn't exceed 21; a player with a higher total than 21 is said to bust or have too many. If the second player has more than 21 on his hand, he lose and must surrender. After the second player stays, the dealer gives cards to himself. If it have more than 21, so he just checks if second player is not cheating by just looking that it has less then 21, and second player wins. If it has less or equals to 21, than cards of the second player is opened and counted. If second player has more than 21, then he wins, if he has less, he lose. If there is draw, the dealer wins. Note, that if second player have 21, it can eventually lose, if dealer will have also 21.

I suppose, while blackjack is familiar to you, this simplified rules are new to you and looks very strange. It is done by two reason. First is to simplify the rules, so the code of this game will be short. Second is to give you to think how you thinks about this problem. Clearly, you spited the games by entities. You note, this in this game participates two player and cards. Players has some basic functionality, and have some difference. Dialer has some advantage upon the second player. Cards are passive entity. It just has a property how many points it includes. Players makes some actions (surround, stay or ask more card) considering cards. It is very naturally to split the problem based on entities.

Not all the problem can be naturally splited based on entities. For example, if you want to calculate some of two numbers, just go and calculate. (While it is possible to think that number is entity that can be added, it is not naturally to do). In the real world there are problems that is easy to split to entities and problem that are not. From theoretical point of view it can be rigorously proved, that every problem that can be solved by Procedural programming can be also solved by Object Oriented Programming, and vice-verse, every problem can be also solved by Object Oriented Programming can be also solved by Procedural programming. The question is what is more convenient to programmer, in what terms is easy to think about it. This book is not deals with this question, it just describes object oriented approach, and whether to use it in concrete situation is up to you.

public class Card {
	private String name;
	private int value;
	
	public Card(String name, int value){
		this.name = name;
		this.value = value;
	}
	
	public String getName(){
		return name;
	}
	
	public int getValue(){
		return value;
	}
	
	public static void main(String[] args) {
		Card c = new Card("ten of peak", 10);
		System.out.println("The card is "+c.getName());
		System.out.println("The card value is "+c.getValue());
		System.out.println();
		
		
	}

}