تمرينات

تمرين 15.2
حول عملية الصنف التالية إلى عملية كائنية.
كود:
public static double abs(Complex c) {
   return Math.sqrt(c.real * c.real + c.imag * c.imag);
}
تمرين 15.3    حول العملية الكائنية التالية إلى عملية صنف.
public boolean equals(Complex b) {
   return(real == b.real && imag == b.imag);
}

تمرين 15.4

هذا التمرين تابع للتمرين 11.3. الغرض هو التدرب على بنية عمليات الكائنات والتعود على رسائل الخطأ المتعلقة بها.
a. حول العمليات في صنف Relational من عمليات صنف إلى عمليات كائنات، وقم بالتغييرات اللازمة في main.
b. اصنع بعض الأخطاء. جرب استدعاء عمليات صنف على أنها عمليات كائنية وبالعكس. حاول أن تعرف ما هو المشروع وما هو الممنوع، وحاول أن تفهم رسائل الخطأ التي تحصل عليها عندما تخلط الأمور.
c. فكر بمحاسن ومساوئ عمليات الأصناف وعمليات الكائنات. أيهما أكثر اختصاراً (عادة)؟ أيهما تبدو طبيعية أكثر للتعبير عن الحسابات (computations)؟ (أو ربما، أي نوع من الحسابات يمكن التعبير عنه بصورة طبيعية أكثر باستخدام كل من النمطين؟)

تمرين 15.5

The goal of this exercise is to write a program that generates random poker hands and classifies them, so that we can estimate the probability of the various poker hands. If you don't play poker, you can read about it here
http://en.wikipedia.org/wiki/List_of_poker_hands.


a.Start with http://thinklikecs.webs.com/resource...CardSoln3.java. and make sure you can compile and run it.
b.Write a definition for a class named PokerHand that extends Deck.
c.Write a Deck method named deal that creates a PokerHand, transfers cards from the deck to the hand, and returns the hand.
d.In main use shuffle and deal to generate and print four PokerHands with five cards each. Did you get anything good?
e.Write a PokerHand method called hasFlush returns a boolean indicating whether the hand contains a flush.
f.Write a method called hasThreeKind that indicates whether the hand contains Three of a Kind.
g.Write a loop that generates a few thousand hands and checks whether they contain a flush or three of a kind. Estimate the probability of getting one of those hands. Compare your results to the probabilities at
http://en.wikipedia.org/wiki/List_of_poker_hands .

h.Write methods that test for the other poker hands. Some are easier than others. You might find it useful to write some general-purpose helper methods that can be used for more than one test.
i.In some poker games, players get seven cards each, and they form a hand with the best five of the seven. Modify your program to generate seven-card hands and recompute the probabilities.