CSC152 2006S, Class 18: Writing Your Own Template Classes (1) Admin: * No written hw for tomorrow! * The reading is actually ready! * Stupid story * Today is lecture/discussion * EC: Hitting Daniel Zamora with a bowling pin (IanBR) * EC: Attending Con Brio concert * EC: Giving blood (or attempting to do so) Overview: * Imperative vs. functional vs. oop * Classes * Fields * Constructors * Methods Claims: * Java is an object-oriented language * Object-oriented programming involves designing objects and having them communicate * Encapsulation, Polymorphism, and Inheritance * We have not yet done object-oriented programming! Whoops! We've been doing *imperative* programming * Looks like instructions in a cookbook * Some basic knowledge (mix in a cookbook, add in a computer) * Sequencing of those operations * Loops * Conditionals * Subroutines * Java supports imperative programming * And imperative programming makes sense for lots of things How do we design and specify objects? * In Java, almost every object is specified with a template called a class * A list of the data associated with the object * A course listing at Grinnell has int courseNumber String department String title String description Person professor double numberOfCredits Place meetingLocation Time meetingTimeIncludingDate boolean writingIntensive? ListOfCourses prereqs * A fraction has long numerator long denominator thebar * Or int or short or BigInteger * A collection of instructions for building the things * Fill in the fields * For a fraction: To build a fraction from two ints, n, and d intended to represent the numerator and dneominator, set the numerator to n and the denominator to d To build a fraction from one int, i, that is equivalent to i, set the numerator to i and the denominator to 1. To build a fraction from a decimal number, r * Use some approximation method: Guess an approximation and refine The guessing game begins: Start with 1/2, that 0.5 TOO LARGE Increase the denominator Next we have 1/3, that's 0.33333333bar TOO LARGE Increase the denominator Next we have 1/4, that's 0.25 TOO LARGE * One technique: find something too small and something too large, and repeatedly take their average and see whether it is too small or too large * Stop when you're close enough. * Methods: How the objects do things * Observers - Get information from the object For a course listing, what is the enrollment? For a fraction, what is the decimal value? * Mutators - Change the object For a course listing, enroll a student For a course listing, change the prof Perhaps we don't mutate fractions * Miscellaneous - Don't seem to get or change How do we do all of this in Java? public class NameOfClass { // +--------+-------------------------------------------------- // | Fields | // +--------+ // Form: OPTIONAL-PROTECTION TYPE NAME; // Refer to as "this.nameOfField"; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ // Form: OPTIONAL-PROTECTION NameOfClass(Parameters) // OPTIONAL-THROWS // { // BODY // } // NameOfClass(Parameters) // +---------+------------------------------------------------- // | Methods | // +---------+ // FORM: LIke static methods, but without the keyword static. // PROTECTION RETURN-TYPE NAME(PARAMETERS) // OPTIONAL-THROWS-CLAUSE // { // BODY // } // NAME(PARAMETERS) } // class NameOfClass * public class Fraction { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The numerator of the fraction (duh!). Can be negative, * positive, or zero. */ long numerator; /** * The denominator of the fraction. Must be positive. */ long denominator; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public Fraction(long _numerator, long _denominator) throws Exception { if (_denominator == 0) { throw new Exception("Didn't your first grade teacher tell you that fractions cannot have 0 denominators?"); } this.numerator = _numerator; this.denominator = _denominator; } // Fraction(long, long) public Fraction(long i) { this.numerator = i; this.denominator = 1; } // Fraction(long) public Fraction(double d) { this.numerator = 1; this.denominator = 1; // STUB! } // Fraction(double) // +---------+------------------------------------------------- // | Methods | // +---------+ public double toDecimal() { return ((double) this.numerator) / ((double) this.denominator); } // toDecimal() public Fraction add(Fraction addend) { long newNumerator = this.numerator * addend.denominator + this.denominator * addend.numerator; long newDenominator = this.denominator * addend.denominator; return new Fraction(newNumerator, newDenominator); } // add(Fraction) } // class NameOfClass * Once you have build your own class, it behaves like the already described classes (e.g., java.lang.Integer) Fraction f = new Fraction(1,3); pen.println("1/3 = " + f.toDecimal());