CSC153 2004S, Class 31: Classes, Objects, and Such Admin: * Where are the noisy side? * Should we let Luis Zuleta Benavides stay? * Only if he's involved. Overview: * Classes and objects. * Fields, methods, and constructors. * Designing a Rational class. * Defining classes. * Defining fields, methods, and constructors. Reminder: * Classes provide "templates" for objects * A book class tells us what characteristics books have * Four parts to the template: * The fields, which describe the characteristics/attributes * May differ between different objects in the same class * The constructors, which describe how to build a new one * The methods, which describe how objects operate * Code is shared between objects * The destructors (Hmmm ... Sam can't count): Can do some cleanup, if necessary * Because Java does memory management for you, you rarely need to write destructors * Memory management is similar (in the abstract) to Scheme: Anything reachable is not garbage; anything else is garbage. Because the langauge controls the pointers, it can rearrange things to "compact" memory. When designing a class, you should think about: * What capabilities we need * What input we might use to create a new object * What fields we need Rational numbers: Ratio of two integers, the second of which is positive. What should you be able to do with rational numbers? * Extract the numerator; int getNumerator() * Extract the denominator; int getDenominator() * Approximate inexactly double toDouble() * Provide a way to write them as a string String toString() * Mathematics with them (add, subtract, multiply, divide) Rational add(Rational r1, Rational r2) [Separate from the object] Rational add(Rational r1); [Must be part of the object] <-- Most Java programmers void incrementSelf(Rational r1); [Must be part of the object] * Change them? NO void setNumerator(int newNumerator) void setDenominator(int newDenominator) * Advantages (compared with creating new ones): * Faster * "Funner" * Prettier * Disadvantages (compared with creating new ones): * Values shouldn't change mid-stream * Depends on your expected application: If you are going to use the rational numbers in writing formulae, you probably shouldn't change them. If you are * Encapsulation suggests that you shouldn't think globally about every change you make * Sam gets really upset when things that don't change naturally change From what should you be able to create rational numbers? * ... What information might you use to create a new rational number? * Numerator and denominator * Numerator only (expected denominator of 1) * Real/double, figure out numerator and denominator * As string (parse it) * Design problem: Rational rat = new Rational(10,2); System.out.println(rat.getNumerator()); -> 10 5 10 * Do we simplify automatically? * Easier * "Correct" * More efficient for addition, subtraction, etc. * Do we have a separate simplify method? YES * Abuse the class * Doing operations that depend on identical denominator * Makes the mutators more confusing * When we add, subtract, multiply, divide two rational numbers, is the result automatically simplified? * Yes