cs230 Lecture Notes Week 4, Thursday For Monday, please read Chapter 14. Also, do the practice exam and bring questions on Monday. Finally, please read the documentation of the Object class, especially clone, equals, and toString. I will be leaving next Wednesday for a conference on Computer Science Education in beautiful, sunny Kentucky. Stella has an office hour Wed 9-11 for last minute exam questions. Quiz 4 Solutions ---------------- Homework 3 questions -------------------- How does a histogram of suits help with isFlush? How does a histogram of ranks help with isPair, etc? If you have three of a kind, does that count as a pair, too? The most challenging of the bunch is isStraightFlush. Warning: with seven cards, it is not true that isStraight AND isFlush IMPLIES isStraightFlush You should generate TWO tables, one for 5-card hands and one for 7-card hands. To get good probability estimates, you need something like 100,000 hands. More is better. Since shuffling takes a while, it would be good to get more than one hand from each shuffle. Do you have to deal the cards in a round-robin? Object-oriented programming --------------------------- Finally, we are in a position to define what we mean by object-oriented programming, and to compare it with procedural programming. Syntactically, the hallmark of object-oriented programming is the object method. The hallmarks of object oriented design are 1) correspondence between object types and the real-world objects that make up the system we are modeling. 2) using inheritance to create families of related objects Conceptually, the hallmark of object-oriented thinking is the notion that the objects are the active agents. Objects "know" how to perform methods, sometimes by asking other objects to perform methods. As you work on hw3, think about the syntactic pros and cons. Later we will be able to evaluate some of the design-level pros and cons. The Object Hierarchy -------------------- When you declare a new object type, you are, by default, extending the Object class. The Object class is the ultimate parent of all classes. All objects inherit methods from the Object class, including equals, clone and toString. By default, equals tests shallow equality. Objects that have a different notion of equality should override it. The documentation specifies properties equals should have. By default, clone performs a shallow copy. Again, classes that have a different notion of copy should override it. The documentation specifies some properties. toString is used by print and println When you invoke System.out.println (obj); Java invokes obj.toString () By default, toString returns the name of the class and a hexadecimal representation of the object's hashCode. When you define an object method, you usually want to provide a toString method that is more suited to your application. In Part 1 of Homework 3, you should 1) change sameCard to equals 2) write a toString for Card 3) write a toString for Deck Notice that there was a gotcha in the previous assignment. I gave you this code: public static boolean sameCard (Card c1, Card c2) { return (c1.suit == c2.suit && c1.rank == c2.rank); } public static int findCard (Card[] deck, Card card) { for (int i = 0; i< deck.length; i++) { if (deck[i].equals (card)) return i; } return -1; } What's the problem here? This is an example of a subtle bug that is common in object-oriented programs -- it is easy to lose track of which _version_ of a method is being invoked. Two ways to fix it: 1) override equals to test deep equality. 2) invoke sameCard instead of equals.