Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 1 Chapter 7 Defining Your Own Classes Part 2 Animated Version.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 1 Chapter 7 Defining Your Own Classes Part 2 Animated Version."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 1 Chapter 7 Defining Your Own Classes Part 2 Animated Version

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 2 Objectives After you have read and studied this chapter, you should be able to –Describe how objects are returned from methods –Describe how the reserved word this is used –Define overloaded methods and constructors –Define class methods and variables –Describe how the arguments are passed to the parameters using the pass-by-value scheme –Class Relationships –Interface

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 3 Returning an Object from a Method As we can return a primitive data value from a method, we can return an object from a method also. We return an object from a method, we are actually returning a reference (or an address) of an object. –This means we are not returning a copy of an object, but only the reference of this object

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 4 Sample Object-Returning Method Here's a sample method that returns an object: public Fraction simplify( ) { Fraction simp; int num = getNumberator(); int denom = getDenominator(); int gcd = gcd(num, denom); simp = new Fraction(num/gcd, denom/gcd); return simp; } Return type indicates the class of an object we're returning from the method. Return an instance of the Fraction class

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 5 A Sample Call to simplify f1 = new Fraction(24, 26); f2 = f1.simplify(); public Fraction simplify( ) { int num = getNumerator(); int denom = getDenominator(); int gcd = gcd(num, denom); Fraction simp = new Fraction(num/gcd, denom/gcd); return simp; } f1 : Fraction numerator denominator 36 24 f2 simp : Fraction numerator denominator 3 2

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 6 A Sample Call to simplify (cont'd) public Fraction simplify( ) { int num = getNumerator(); int denom = getDenominator(); int gcd = gcd(num, denom); Fraction simp = new Fraction(num/gcd, denom/gcd); return simp; } f1 = new Fraction(24, 26); : Fraction numerator denominator 3 2 f2 = f1.simplify(); f1 : Fraction numerator denominator 36 24 f2 simp : Fraction numerator denominator 3 2 The value of simp, which is a reference, is returned and assigned to f2.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 7 Reserved Word this The reserved word this is called a self-referencing pointer because it refers to an object from the object's method. : Object this The reserved word this can be used in three different ways. We will see all three uses in this chapter.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 8 The Use of this in the add Method public Fraction add(Fraction frac) { int a, b, c, d; Fraction sum; a = this.getNumerator(); //get the receiving b = this.getDenominator(); //object's num and denom c = frac.getNumerator(); //get frac's num d = frac.getDenominator(); //and denom sum = new Fraction(a*d + b*c, b*d); return sum; }

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 9 f3 = f1.add(f2) Because f1 is the receiving object (we're calling f1's method), so the reserved word this is referring to f1.

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 10 f3 = f2.add(f1) This time, we're calling f2's method, so the reserved word this is referring to f2.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 11 Using this to Refer to Data Members In the previous example, we showed the use of this to call a method of a receiving object. It can be used to refer to a data member as well. class Person { int age; public void setAge(int val) { this.age = val; }... }

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 12 Overloaded Methods Methods can share the same name as long as –they have a different number of parameters (Rule 1) or –their parameters are of different data types when the number of parameters is the same (Rule 2) public void myMethod(int x, int y) {... } public void myMethod(int x) {... } Rule 1 public void myMethod(double x) {... } public void myMethod(int x) {... } Rule 2

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 13 Overloaded Constructor The same rules apply for overloaded constructors –this is how we can define more than one constructor to a class public Person( ) {... } public Person(int age) {... } Rule 1 public Pet(int age) {... } public Pet(String name) {... } Rule 2

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 14 Constructors and this To call a constructor from another constructor of the same class, we use the reserved word this. public Fraction( ) { //creates 0/1 this(0, 1); } public Fraction(int number) { //creates number/1 this(number, 1); } public Fraction(Fraction frac) { //copy constructor this(frac.getNumerator(), frac.getDenominator()); } public Fraction(int num, int denom) { setNumerator(num); setDenominator(denom); }

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 15 Class Methods We use the reserved word static to define a class method. public static int gcd(int m, int n) { //the code implementing the Euclidean algorithm } public static Fraction min(Fraction f1, Fraction f2) { //convert to decimals and then compare }

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 16 Static Variable Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class is first referenced All objects instantiated from the class share its static variables Changing the value of a static variable in one object changes it for all others

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 17 Static Class Members Recall that the main method is static – it is invoked by the Java interpreter without creating an object Static methods cannot reference instance variables because instance variables don't exist until an object exists However, a static method can reference static variables or local variables

18 Static Class Members ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 18 public class Slogan { private String phrase; private static int count = 0; public Slogan (String str) { phrase = str; count++; } public static int getCount () { return count; } public class SloganCounter { public static void main (String[] args) { Slogan obj; obj = new Slogan ("Remember the Alamo."); obj = new Slogan ("Don't Worry."); obj = new Slogan ("Live Free or Die."); System.out.println ("Slogans created: " + Slogan.getCount()); }

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 19 Call-by-Value Parameter Passing When a method is called, –the value of the argument is passed to the matching parameter, and –separate memory space is allocated to store this value. This way of passing the value of arguments is called a pass-by-value or call-by-value scheme. Since separate memory space is allocated for each parameter during the execution of the method, –the parameter is local to the method, and therefore –changes made to the parameter will not affect the value of the corresponding argument.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 20 Call-by-Value Example class Tester { public void myMethod(int one, double two ) { one = 25; two = 35.4; } Tester tester; int x, y; tester = new Tester(); x = 10; y = 20; tester.myMethod(x, y); System.out.println(x + " " + y); produces 10 20

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 21 Memory Allocation for Parameters

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 22 Memory Allocation for Parameters (cont'd)

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 23 Parameter Passing: Key Points 1. Arguments are passed to a method by using the pass-by- value scheme. 2. Arguments are matched to the parameters from left to right.The data type of an argument must be assignment-compatible with the data type of the matching parameter. 3. The number of arguments in the method call must match the number of parameters in the method definition. 4. Parameters and arguments do not have to have the same name. 5. Local copies, which are distinct from arguments,are created even if the parameters and arguments share the same name. 6. Parameters are input to a method, and they are local to the method.Changes made to the parameters will not affect the value of corresponding arguments.

24 Parameter Passing: Example ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 24 public class ParameterTester { public static void main (String[] args) { ParameterModifier modifier = new ParameterModifier(); int a1 = 111; Num a2 = new Num (222); Num a3 = new Num (333); System.out.println("Before changeValues:"); System.out.println ("a1\ta2\ta3"); System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); modifier.changeValues (a1, a2, a3); System.out.println ("After changeValues:"); System.out.println ("a1\ta2\ta3"); System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); } public class ParameterModifier { public void changeValues (int f1, Num f2, Num f3) { System.out.println ("Before changing the values:"); System.out.println ("f1\tf2\tf3"); System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); f1 = 999; f2.setValue(888); f3 = new Num (777); System.out.println ("After changing the values:"); System.out.println ("f1\tf2\tf3"); System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); } public class Num { private int value; public Num (int update) { value = update; } public void setValue (int update) {value = update; } public String toString () { return value + "“; } }

25 Method Design As we've discussed, high-level design issues include: –identifying primary classes and objects –assigning primary responsibilities After establishing high-level design issues, its important to address low-level issues such as the design of key methods For some methods, careful planning is needed to make sure they contribute to an efficient and elegant system design

26 Method Design An algorithm is a step-by-step process for solving a problem Examples: a recipe, travel directions Every method implements an algorithm that determines how the method accomplishes its goals An algorithm may be expressed in pseudocode, a mixture of code statements and English that communicate the steps to take

27 Method Decomposition A method should be relatively small, so that it can be understood as a single entity A potentially large method should be decomposed into several smaller methods as needed for clarity A public service method of an object may call one or more private support methods to help it accomplish its goal Support methods might call other support methods if appropriate

28 © 2004 Pearson Addison-Wesley. All rights reserved 6-28 Method Decomposition Example Let's look at an example that requires method decomposition – translating English into Pig Latin Pig Latin is a language in which each word is modified by moving the initial sound of the word to the end and adding "ay" Words that begin with vowels have the "yay" sound added on the end Constant blends such as”ch” and “st” at the beginning of a word are moved to the end before adding the “ay” bookookbaytableabletay itemitemyaychairairchay

29 © 2004 Pearson Addison-Wesley. All rights reserved 6-29 Method Decomposition The primary objective (translating a sentence) is too complicated for one method to accomplish Therefore we look for natural ways to decompose the solution into pieces Translating a sentence can be decomposed into the process of translating each word The process of translating a word can be separated into translating words that: –begin with vowels –begin with consonant blends (sh, cr, th, etc.) –begin with single consonants

30 © 2004 Pearson Addison-Wesley. All rights reserved 6-30 Method Decomposition See PigLatin.java (page 320)PigLatin.java See PigLatinTranslator.java (page 323)PigLatinTranslator.java PigLatin + main (args : String[]) : void + translate (sentence : String) : String - translateWord (word : String) : String - beginsWithVowel (word : String) : boolean - beginsWithBlend (word : String) : boolean PigLatinTranslator

31 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 31 Class Relationships Classes in a software system can have various types of relationships to each other Three of the most common relationships: –Dependency: A uses B –Aggregation: A has-a B –Inheritance: A is-a B Let's discuss dependency and aggregation further Inheritance is discussed in detail in Chapter 8

32 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 32 Dependency A dependency exists when one class relies on another in some way, usually by invoking the methods of the other We don't want many or complex dependencies among classes Nor do we want complex classes that don't depend on others A good design strikes the right balance

33 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 33 Dependency Some dependencies occur between objects of the same class A method of the class may accept an object of the same class as a parameter For example, the concat method of the String class takes as a parameter another String object str3 = str1.concat(str2);

34 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 34 Aggregation An aggregate is an object that is made up of other objects Therefore aggregation is a has-a relationship –A car has a chassis In software, an aggregate object contains references to other objects as instance data The aggregate object is defined in part by the objects that make it up This is a special kind of dependency – the aggregate usually relies on the objects that compose it

35 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 35 Aggregation In the following example, a Student object is composed, in part, of Address objects A student has an address (in fact each student has two addresses) See StudentBody.java (page 304)StudentBody.java See Student.java (page 306)Student.java See Address.java (page 307)Address.java An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end

36 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 36 Aggregation in UML StudentBody + main (args : String[]) : void + toString() : String Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + toString() : String - streetAddress : String - city : String - state : String - zipCode : long Address

37 Iterators An iterator is an object that allows you to process a collection of items one at a time It lets you step through each item in turn and process it as needed An iterator object has a hasNext method that returns true if there is at least one more item to process The next method returns the next item Iterator objects are defined using the Iterator interface ; ( we can define iterator interface and there are classes as iterator )

38 Iterators Several classes in the Java standard class library are iterators The Scanner class is an iterator –the hasNext method returns true if there is more data to be scanned –the next method returns the next scanned token as a string The Scanner class also has variations on the hasNext method for specific data types (such as hasNextInt, hasNextDouble ) The Scanner class can read from (output screen, existing file, or a String)

39 Iterators The fact that a Scanner is an iterator is particularly helpful when reading input from a file Suppose we wanted to read and process a list of URLs stored in a file One scanner can be set up to read each line of the input until the end of the file is encountered Another scanner can be set up for each URL to process each part of the path See URLDissector.java (page 240)URLDissector.java

40 foreach Loops for statement was created especially for iterating over collections and arrays. Example: int[] arrayOfInts = { 32, 87, 3, 589, 12}; for (int element : arrayOfInts) { System.out.print(element + " "); } for (Book myBook : BookList) { System.out.print(myBook + " "); } Book myBook; While(BookList.hasNext()) { myBook = BookList.next(); System.out.println(myBook); } BookList is an iterator object that manage Book objects.

41 foreach Loops This style of for loop can be read for each int element in arrayOfInts ; Therefore the iterator version of the for loop is sometimes referred to as the foreach loop It eliminates the need to call the hasNext and next methods explicitly

42 © 2004 Pearson Addison-Wesley. All rights reserved 6-42 Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off. An interface is used to establish a set of methods that a class will implement.

43 © 2004 Pearson Addison-Wesley. All rights reserved 6-43 Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

44 © 2004 Pearson Addison-Wesley. All rights reserved 6-44 Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by: –stating it in the class header –providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

45 © 2004 Pearson Addison-Wesley. All rights reserved 6-45 Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

46 © 2004 Pearson Addison-Wesley. All rights reserved 6-46 Interfaces A class that implements an interface can implement other methods as well See Complexity.java (page 310)Complexity.java See Question.java (page 311)Question.java See MiniQuiz.java (page 313)MiniQuiz.java In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

47 © 2004 Pearson Addison-Wesley. All rights reserved 6-47 Interfaces A class can implement multiple interfaces The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

48 © 2004 Pearson Addison-Wesley. All rights reserved 6-48 Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains one abstract method called compareTo, which is used to compare two objects We discussed the compareTo method of the String class in Chapter 5 The String class implements Comparable, giving us the ability to put strings in lexicographic order

49 © 2004 Pearson Addison-Wesley. All rights reserved 6-49 The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is less than obj2"); The value returned from compareTo should be negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer designs a class that implements the Comparable interface, it should follow this intent

50 © 2004 Pearson Addison-Wesley. All rights reserved 6-50 The Comparable Interface It's up to the programmer to determine what makes one object less than another For example, you may define the compareTo method of an Employee class to order employees by name (alphabetically) or by employee number The implementation of the method can be as straightforward or as complex as needed for the situation

51 © 2004 Pearson Addison-Wesley. All rights reserved 6-51 The Iterator Interface As we discussed in Chapter 5, an iterator is an object that provides a means of processing a collection of objects one at a time An iterator is created formally by implementing the Iterator interface, which contains three methods –The hasNext method returns a boolean result – true if there are items left to process –The next method returns the next object in the iteration –The remove method removes the object most recently returned by the next method Scanner implements iterator interface.

52 © 2004 Pearson Addison-Wesley. All rights reserved 6-52 The Iterator Interface By implementing the Iterator interface, a class formally establishes that objects of that type are iterators The programmer must decide how best to implement the iterator functions Once established, the for-each version of the for loop can be used to process the items in the iterator

53 © 2004 Pearson Addison-Wesley. All rights reserved 6-53 Interfaces You could write a class that implements certain methods (such as compareTo ) without formally implementing the interface ( Comparable ) However, formally establishing the relationship between a class and an interface allows Java to deal with an object in certain ways Interfaces are a key aspect of object-oriented design in Java We discuss this idea further in Chapter 9

54 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 54 Problem Statement Write an application that computes the total charges for the overdue library books. For each library book, the user enters the due date and (optionally) the overdue charge per day,the maximum charge, and the title. If the optional values are not entered, then the preset default values are used. A complete list of book information is displayed when the user finishes entering the input data.The user can enter different return dates to compare the overdue charges.

55 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 55 Overall Plan Tasks: 1.Get the information for all books 2.Display the entered book information 3.Ask for the return date and display the total charge. Repeat this step until the user quits.

56 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 56 Required Classes OverdueChecker Scanner LibraryBook helper class BookTracker

57 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 57 Development Steps We will develop this program in five steps: 1. Define the basic LibraryBook class. 2. Explore the given BookTracker class and integrate it with the LibraryBook class. 3. Define the top-level OverdueChecker class. Implement the complete input routines. 4. Complete the LibraryBook class by fully implementing the overdue charge computation. 5. Finalize the program by tying up loose ends.

58 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 58 Step 1 Design Develop the basic LibraryBook class. The key design task is to identify the data members for storing relevant information. We will include multiple constructors for ease of creating LibraryBook objects. –Make sure that an instance will be initiated correctly no matter which constructor is used.

59 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 59 Step 1 Code Directory: Chapter7/Step1 Source Files: LibraryBook.java Step1Main.java (test program) Directory: Chapter7/Step1 Source Files: LibraryBook.java Step1Main.java (test program) Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.

60 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 60 Step 1 Test In the testing phase, we run the test main program Step1Main and confirm that we get the expected output:

61 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 61 Step 2 Design Explore the helper BookTracker class and incorporate it into the program. Adjust the LibraryBook class to make it compatible with the BookTracker class.

62 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 62 Step 2 Code Directory: Chapter7/Step2 Source Files: LibraryBook.java Step2Main.java (test program) Directory: Chapter7/Step2 Source Files: LibraryBook.java Step2Main.java (test program)

63 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 63 Step 2 Test In the testing phase, we run the test main program Step2Main and confirm that we get the expected output. We run the program multiple times trying different variations each time.

64 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 64 Step 3 Design We implement the top-level control class OverdueChecker. The top-level controller manages a single BookTracker object and multiple LibraryBook objects. The top-level controller manages the input and output routines –If the input and output routines are complex, then we would consider designing separate classes to delegate the I/O tasks.

65 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 65 Step 3 Pseudocode GregorianCalendar returnDate; String reply, table; double totalCharge; inputBooks(); //read in all book information table = bookTracker.getList(); System.out.println(table); //try different return dates do { returnDate = read return date ; totalCharge = bookTracker.getCharge(returnDate); displayTotalCharge(totalCharge); reply = prompt the user to continue or not; } while ( reply is yes );

66 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 66 Step 3 Code Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java

67 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 67 Step 3 Test Now we run the program multiple times, trying different input types and values. We confirm that all control loops are implemented and working correctly. –At this point, the code to compute the overdue charge is still a stub, so we will always get the same overdue charge for the same number of books. After we verify that everything is working as expected,we proceed to the next step.

68 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 68 Step 4: Compute the Charge To compute the overdue charge, we need two dates: the due date and the date the books are or to be returned. The getTimeInMillis method returns the time elasped since the epoch to the date in milliseconds. By subtracting this since-the-epoch milliseconds value of the due date from the same of the return date, we can find the difference between the two. –If the difference is negative, then it’s not past due, so there’s no charge. –If the difference is positive, then we convert the milliseconds to the equivalent number of days and multiply it by the per- day charge to compute the total charge.

69 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 69 Step 4 Code Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java

70 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 70 Step 4 Test We run the program mutiple times again, possibly using the same set of input data. We enter different input variations to try out all possible cases for the computeCharge method. –Try cases such as the return date and due date are the same, the return date occurs before the due date, the charge is beyond the maximum, and so forth. After we verify the program,we move on to the next step.

71 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 71 Step 5: Finalize / Extend Program Review –Are all the possible cases handled? –Are the input routines easy to use? –Will it be better if we allow different formats for entering the date information? Possible Extensions –Warn the user, say, by popping a warning window or ringing an alarm, when the due date is approaching. –Provide a special form window to enter data (Note: To implement these extensions, we need techniques not covered yet.)


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 - 1 Chapter 7 Defining Your Own Classes Part 2 Animated Version."

Similar presentations


Ads by Google