19-Designing-and-Implementing-Classes-1

19-Designing-and-Implementing-Classes-1 - CS106A Handout 19...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 19 Spring 2011 April 22 nd , 2011 Class Design and Implementation The majority of Chapter 6—which you should be reading over the course of the next few days—discusses classes, objects, object orientation, interface design, and implementation. We’ll discuss both how to design a class as well as how to implement one over the course of today’s lecture. Harmonic Numbers The following program prints out the first 20 Harmonic numbers, where the n th Harmonic number is understood, by definition, to be the sum of the first n unit reciprocals. The implementation is fairly straightforward, provided we make use of an invented class called the Rational , which programmatically imitates the idea of a rational number represented as a fraction. In the same way we invent method names as part of the procedural decomposition process, we can invent new object types—beyond the ones already available to us—and absolutely should do so if it helps us model a complex piece of data. The Rational class doesn’t exist in core Java or in the ACM/CS106A libraries, so we’ll need to implement it ourselves. We can, however, pretend the class exists and further pretend objects of the invented class type respond to a small number of sensible, relevant methods. public class HarmonicNumbers extends ConsoleProgram { public void run() { println("This program prints out the first 20 Harmonic numbers."); println(); Rational sum = new Rational(0); for ( int d = 1; d <= 20; d++) { Rational unitFraction = new Rational(1, d); sum = sum.add(unitFraction); println(" " + d + ".) " + sum); } } } We make use of a Rational constructor, an add method, and an ability to implicitly serialize a Rational to string form so it can be printed. You should also note that we can construct a Rational in two ways—with one parameter, or with two. And without thinking about it too much, we just assume that the presentation of the rational in strong form will print it in reduced form (yes to 2/3, no to 6/9, as an example). When inventing new class types, it’s best to think how you’d like objects of that type to interact with your program.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sample Run This program prints out the first 20 Harmonic numbers. 1.) 1 2.) 3/2 3.) 11/6 4.) 25/12 5.) 137/60 6.) 49/20 7.) 363/140 8.) 761/280 9.) 7129/2520 10.) 7381/2520 11.) 83711/27720 12.) 86021/27720 13.) 1145993/360360 14.) 1171733/360360 15.) 1195757/360360 16.) 2436559/720720 17.) 42142223/12252240 18.) 14274301/4084080 19.) 275295799/77597520 20.) 29285141/35271600 Egyptian Fractions Here’s a more sophisticated use of this Rational class we’re dreaming of. It relies on the notion of an Egyptian fraction, which is a representation of a number between 0 and 1 using only unit reciprocal fractions. The notation was, according to Wikipedia, developer in the Middle Kingdom of Egypt between 3500 and 4000 years ago. Save for the fractions 2/3 and 3/4, which were common enough that they had their own
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/28/2011 for the course CS 106A taught by Professor Sahami,m during the Spring '08 term at Stanford.

Page1 / 9

19-Designing-and-Implementing-Classes-1 - CS106A Handout 19...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online