Presentation is loading. Please wait.

Presentation is loading. Please wait.

DESIGNING CLASSES. SPECIFICATIONS FOR THE CAR CLASS Professional programmers carefully design the classes they need before any coding is done. With well-

Similar presentations


Presentation on theme: "DESIGNING CLASSES. SPECIFICATIONS FOR THE CAR CLASS Professional programmers carefully design the classes they need before any coding is done. With well-"— Presentation transcript:

1 DESIGNING CLASSES

2 SPECIFICATIONS FOR THE CAR CLASS Professional programmers carefully design the classes they need before any coding is done. With well- designed classes programming is much easier and the program has fewer bugs. Object oriented design consists of deciding what classes are needed, what data they will hold, and how they will behave. All these decisions are documented (written up) and then examined. If something doesn't look right, it is fixed before any programming is done. Let us do that with our Car class.

3 Car A class that calculates miles per gallon. Variables Number startMiles; // Starting odometer reading Number endMiles; // Ending odometer reading Number gallons; // Gallons of gas used between the readings Constructors Car(Number startOdo, Number endingOdo, Number gallons ) Creates a new instance of a Car object with the starting and ending odometer readings and the number of gallons of gas consumed. Methods Number calculateMPG() calculates and returns the miles per gallon for the car.

4 PROGRAM TO USE THE CAR CLASS var line:String; var startMiles, endMiles:Number; var gallons:Number; Alert.show("Enter first reading:" ); line = txtFirstReading.text; startMiles = Number ( line ); Alert.show("Enter second reading:" ); line = txtSecondReading.text; endMiles = Number ( line ); Alert.show("Enter gallons:" ); line = txtGallons.text; gallons = Number ( line ); var car:Car = new Car( startMiles, endMiles, gallons ); Alert.show( "Miles per gallon is " + car.calculateMPG() ); Of course, the program still cannot be run because the class Car has not yet been defined.

5 FILLING IN THE DEFINITION class Car{ // instance variables var startMiles:Number; // Stating odometer reading var endMiles:Number; // Ending odometer reading var gallons:Number; // Gallons of gas used between the readings // constructor function Car( first:Number, last:Number, gals:Number) { startMiles = first ; endMiles = last ; gallons = gals ; } // methods function calculateMPG():Number { return (endMiles - startMiles)/gallons ; }

6 USING THE CLASS var car:Car = new Car( 32456, 32810, 10.6 ); Alert.show( "Miles per gallon is " + car.calculateMPG() );

7 POSSIBLE ERRORS class Car{ // instance variables var startMiles:Number; // Stating odometer reading var endMiles:Number; // Ending odometer reading var gallons:Number; // Gallons of gas used between the readings // constructor function Car( first:Number:Number, last:Number, gals:Number) { startMiles = first ; endMiles = last ; gallons = gals ; } // methods function calculateMPG():Number { return (last - first)/gals ; }

8 ANSWER the calculateMPG() method can not even see the parameters of the constructor. Another way of saying this is that the scope of the parameters is limited to the body of the method. The compiler will complain that first, last, and gals are "undefined variables" because they are used outside of their scope.

9 FILL IN THE BLANKS Design of the Telescope Class. The three most important numbers describing a telescope are: the diameter of the main lens (the one in front), the focal length of the main lens, and the focal length of the eyepiece. From these values other characteristics of the telescope such as its magnification and the f- number of the main lens are calculated. Rewrite (especially where there are blanks) the code in the following design for the class: class Telescope A class that models a field telescope. Constructors Telescope (diameter:Number, mainFocal:Number, eyepieceFocal:Number ) Methods // calculate the magnification of the telescope double var mag:Number = mainFocal / eyepieceFocal; // calculate the f-number of the telescope double var fN:Number = mainFocal / diameter;

10 Checking the Design. To check the design, write a small program that uses the class to see if it works well. Write a program that creates a Telescope object with a main lens that has a diameter of 3.0 inches, a focal length of 6.5 inches, and an eyepiece focal length of 0.8 inches. Write out its magnification and f-number. var tele:Telescope = new Telescope(3.0, 6.5, 0.8) ; Alert.show( "Power: " + tele.mag+ " F-number: " + tele.fN);

11 Skeleton of the Class. Fill in the blanks that give the over-all design of the class. class Telescope{ // main diameter // main focal length // eyepiece focal length }

12 . Fill in Instance Variables. Fill in the data type of each instance variable. class Telescope{ // Instance Variables var diameter:Number; var mainLength:Number; var eyeLength:Number; // Constructors // Methods }

13 Complete the Constructor. The constructor will initialize the instance variables of the object being constructed. class Telescope{ // Instance Variables var diameter: Number; var mainLength: Number; var eyeLength: Number; // Constructors public function Telescope (diameter: Number, mainLength: Number, eyeLength: Number ) { this.diameter = diameter ; this.mainLength = mainLength ; this.eyeLength = eyeLength ; } // Methods } When an instance variable and a parameter use the same identifier, you specify the instance variable of the object by saying "this.identifier" as in the above.

14 Complete a Method. The documentation for the magnification() method says it looks like this: // calculate the magnification of the telescope double magnification() The formula to use is: magnification = mainLength/eyeLength class Telescope{ // Instance Variables var diameter: Number; var mainLength: Number; var eyeLength: Number; // Constructors function Telescope (diameter: Number, mainLength: Number, eyeLength: Number ) { this.diameter = diameter ; this.mainLength = mainLength ; this.eyeLength = eyeLength ; } // Methods function magnification():Number { return mainLength / _____________ ; } }

15 Complete the other Method. The documentation for the says it looks like this: // calculate the magnification of the telescope double fNumber() The formula to use is: fNumber = mainLength/diameter class Telescope{ // Instance Variables var diameter: Number; var mainLength: Number; var eyeLength: Number; // Constructors function Telescope (diameter: Number, mainLength:Number, eyeLength: Number ) { this.diameter = diameter ; this.mainLength = mainLength ; this.eyeLength = eyeLength ; } // Methods function magnification():Number { return mainLength / eyeLength ; } function fNumber():Number { return mainLength / diameter; } }

16 Programming Exercises Modify the Car class by adding two methods: boolean gasHog() evaluates to true if the miles per gallon is lower than 15.0. boolean economyCar() evaluates to true if the miles per gallon is higher than 30.0. The constructor and the calculateMPG() method remain unchanged. Each of these new methods should use the calculateMPG() to get the miles per gallon, not calculate it themselves. An if-else statement picks the correct boolean return value. You might be tempted to make one of these common design errors: Saving miles per gallon in an instance variable of the object along with startMiles, endMiles, and gallons. This almost seems logical, but is a poor design. Don't keep a permanent copy of a value that can be easily calculated from data. The reason for this is that it adds complexity to the object, but offers little advantage. Directly calculating miles per gallon inside each of the new methods. It is usually best to do a particular calculation in a method, and to use it whenever the calculation is needed. Now if there is a bug in the calculation, or the calculation must be modified, there is only one place to look. Test your program by designing a form to allow a user to enter the first and second readings along with the number of gallons. Display the answer in a label. Ex. IO Enter first reading:10000 Enter second reading:10400 Enter gallons:10 Miles per gallon: 40 Economy Car!

17 Change the constructor for the Car class so that it has only one parameter, the first reading of the odometer. The miles per gallon cannot yet be calculated. Now add a method to the class: function fillUp( int miles, double gallons ) This simulates filling up the tank at a gas station: miles is the current odometer reading and gallons is the number of gallons that filled the tank. Save these values in instance variables. With this information, miles per gallon can be calculated. Write the method so that it updates the instance variables each time it is called (simulating another visit to the pumps). After each call calculateMPG() will calculate the latest miles per gallon. Write a testing program that constructs a car and calls fillUp() and calculateMPG() a few times. Ex. IO New car odometer reading:00000 Filling Station Visit odometer reading 00350 gallons to fill tank 10 Miles per gallon: 35 Economy Car! Filling Station Visit odometer reading 00450 gallons to fill tank 10 Miles per gallon: 10 Gas Hog!


Download ppt "DESIGNING CLASSES. SPECIFICATIONS FOR THE CAR CLASS Professional programmers carefully design the classes they need before any coding is done. With well-"

Similar presentations


Ads by Google