Chapter 8 Designing Classes Chapter 8 Designing Classes
- Slides: 78
Chapter 8 Designing Classes Chapter 8 Designing Classes 1
Chapter Goals q To learn how to choose appropriate classes to implement q To understand cohesion and coupling q To minimize the use of side effects q Consider call by value vs call by reference q Use of preconditions and postconditions Chapter 8 Designing Classes 2
Chapter Goals q q To understand the difference between instance methods and static methods To introduce the concept of static fields To understand the scope rules for local variables and instance fields To learn about packages Chapter 8 Designing Classes 3
Choosing Classes q A class represents a single concept from the problem domain q Name for a class should be a noun that describes concept q Concepts from mathematics Point Rectangle Ellipse q Concepts from real life Bank. Account Cash. Register Chapter 8 Designing Classes 4
Choosing Classes q Actors (end in -er, -or): objects do some kinds of work for you Scanner Random // better name: Random. Number. Generator q Utility classes: no objects, only static methods and constants Math q Program starters: only have a main method q Don't turn actions into classes: Paycheck is better name than Compute. Paycheck Chapter 8 Designing Classes 5
Self Test 1. What is the rule of thumb for finding classes? 2. Your job is to write a program that plays chess. Might Chess. Board be an appropriate class? How about Next. Move? Chapter 8 Designing Classes 6
Answers 1. Look for nouns in the problem description 2. Yes (Chess. Board) and no (Next. Move) Chapter 8 Designing Classes 7
Cohesion q q A class should represent a single concept The public interface of a class is cohesive if all of its features are related to the concept that the class represents Chapter 8 Designing Classes 8
Cohesion q This class lacks cohesion public class Cash. Register { public void enter. Payment(int dollars, int quarters, int dimes, int nickels, int pennies). . . public static final double NICKEL_VALUE = 0. 05; public static final double DIME_VALUE = 0. 1; public static final double QUARTER_VALUE = 0. 25; . . . } Chapter 8 Designing Classes 9
Cohesion q Problem: Cash. Register, as described above, involves two concepts: cash register and coin q Solution: Make two classes, Cash. Register and Coin public class Coin { public Coin(double a. Value, String a. Name){. . . } public double get. Value(){. . . } public class Cash. Register { public void enter. Payment(int coin. Count, Coin coin. Type) {. . . } Chapter 8 Designing Classes 10
Coupling q q A class depends on another if it uses objects of that class Cash. Register depends on Coin to determine the value of the payment q Coin does not depend on Cash. Register q High coupling == many class dependencies q q Minimize coupling to minimize the impact of interface changes To visualize relationships draw class diagrams Chapter 8 Designing Classes 11
Coupling q UML --- Unified Modeling Language q Notation for object-oriented analysis and design q Useful to show dependencies between classes Chapter 8 Designing Classes 12
Couping Figure 1 Dependency Relationship Between the Cash. Register and Coin Classes Chapter 8 Designing Classes 13
High and Low Coupling Between Classes Figure 2 High and Low Coupling Between Classes Chapter 8 Designing Classes 14
Self Check 3. Why is the Cash. Register class from Chapter 4 not cohesive? 4. Why does the Coin class not depend on the Cash. Register class? 5. Why should coupling be minimized between classes? Chapter 8 Designing Classes 15
Answers 3. Some of its features deal with payments, others with coin values 4. None of the coin operations require the Cash. Register class 5. If a class doesn't depend on another, it is not affected by interface changes in the other class Chapter 8 Designing Classes 16
Accessors, Mutators, and Immutable Classes q Accessor: does not change the state of the implicit parameter double balance = account. get. Balance(); q Mutator: modifies the object on which it is invoked account. deposit(1000); Chapter 8 Designing Classes 17
Accessors, Mutators, and Immutable Classes q Immutable class: has no mutator methods String name = "John Q. Public"; String uppercased = name. to. Upper. Case(); // name is not changed q It is safe to give out references to objects of immutable classes since no code can modify the object at an unexpected time Chapter 8 Designing Classes 18
Self Check 6. Is the substring method of the String class an accessor or a mutator? 7. Is the Rectangle class immutable? Chapter 8 Designing Classes 19
Answers 6. It is an accessor, calling substring doesn't modify the string on which the method is invoked. In fact, all methods of the String class are accessors 7. No, translate is a mutator Chapter 8 Designing Classes 20
Side Effects q Side effect of a method: any externally observable data modification public void transfer(double amount, Bank. Account other) { balance = balance - amount; other. balance = other. balance + amount; // Modifies explicit parameter } q Modifying explicit parameter can be surprising to programmers, it is best to avoid if possible Chapter 8 Designing Classes 21
Side Effects q Another example of a side effect is output public void print. Balance() // Not recommended { System. out. println("The balance is now $" + balance); } Bad idea: message is in English, and relies on System. out It is best to decouple input/output from the actual work of your classes q You should minimize side effects (try not to make changes beyond modification of the implicit parameter) Chapter 8 Designing Classes 22
Self Check 8. If a refers to a bank account, then the call a. deposit(100) modifies the bank account object. Is that a side effect? 9. Consider the Data. Set class of Chapter 7. Suppose we add a method void read(Scanner in) { while (in. has. Next. Double()) add(in. next. Double()); } Does this method have a side effect? Chapter 8 Designing Classes 23
Answers 8. No, since a side effect of a method is any change outside the implicit parameter 9. Yes, since the method affects the state of the Scanner parameter Chapter 8 Designing Classes 24
Common Error – Trying to Modify Primitive Type Parameter q void transfer(double amount, double other. Balance) { balance = balance - amount; other. Balance = other. Balance + amount; } q Will not work q Why? Consider the following scenario double savings. Balance = 1000; harrys. Checking. transfer(500, savings. Balance); System. out. println(savings. Balance); q Java method cannot change explicit parameters primitive type Chapter 8 Designing Classes of 25
Modifying a Numeric Parameter Has No Effect on Caller Figure 3(1): Modifying a Numeric Parameter Has No Effect on Caller Chapter 8 Designing Classes 26
Modifying a Numeric Parameter Has No Effect on Caller Figure 3(2): Modifying a Numeric Parameter Has No Effect on Caller Chapter 8 Designing Classes 27
Modifying a Numeric Parameter Has No Effect on Caller Figure 3(3): Modifying a Numeric Parameter Has No Effect on Caller Chapter 8 Designing Classes 28
Modifying a Numeric Parameter Has No Effect on Caller Figure 3(4): Modifying a Numeric Parameter Has No Effect on Caller Chapter 8 Designing Classes 29
Call By Value and Call By Reference q q q Call by value: Method parameters are copied into the parameter variables when a method starts Call by reference: Methods can modify parameters Java uses call by value Chapter 8 Designing Classes 30
Call By Value and Call By Reference q A method can change state of object reference (explicit) parameters, but cannot replace an object reference with another public class Bank. Account { public void transfer(double amount, Bank. Account other. Account) { balance = balance - amount; double new. Balance = other. Account. balance + amount; other. Account = new Bank. Account(new. Balance); // Won't work } } Chapter 8 Designing Classes 31
Call By Value Example harrys. Checking. transfer(500, savings. Account); Figure 4: Modifying an Object Reference Parameter Has No Effect on the Caller Chapter 8 Designing Classes 32
Preconditions q q Precondition: Requirement that caller of a method must meet Publish preconditions so the caller does not call methods with bad parameters q /** Deposits money into this account. @param amount the amount of money to deposit (Precondition: amount >= 0) */ Chapter 8 Designing Classes 33
Preconditions q Typical use § To restrict the parameters of a method § To require that a method is only called when the object is in an appropriate state q If precondition is violated, method is not responsible for computing the correct result § In fact, method is then free to do anything Chapter 8 Designing Classes 34
Preconditions q Method may “throw exception” if precondition violated (more on this in Chapter 11) if (amount < 0) throw new Illegal. Argument. Exception(); balance = balance + amount; q Method does not have to test for precondition (test may be costly) // if this makes the balance negative, it's caller's fault balance = balance + amount; Chapter 8 Designing Classes 35
Preconditions q Method can do an assertion check, such as assert amount >= 0; balance = balance + amount; q q To enable assertion checking: java -enableassertions My. Prog You can turn assertions off after you have tested your program (it will then run faster) Chapter 8 Designing Classes 36
Preconditions q Many beginning programmers silently return to the caller if (amount < 0) return; // Not recommended; hard to debug balance = balance + amount; Chapter 8 Designing Classes 37
Syntax 9. 1: Assertion assert condition; Example: assert amount >= 0; Purpose: To assert that a condition is fulfilled. If assertion checking is enabled and the condition is false, an assertion error is thrown. Chapter 8 Designing Classes 38
Postconditions q q Postcondition: Condition that is true after a method has completed If method call satisfies preconditions… § q …must ensure that postconditions are valid There are two kinds of postconditions 1. The return value is computed correctly 2. Object is in some specified state after the method call is completed Chapter 8 Designing Classes 39
Postconditions q /** Deposits money into this account. (Postcondition: get. Balance() >= 0) @param amount the amount of money to deposit (Precondition: amount >= 0) */ q Do not document trivial postconditions that simply repeat the @return clause Chapter 8 Designing Classes 40
Postconditions q Formulate pre-/post-conditions only in terms of the interface of the class amount <= get. Balance() // this is the way to state a postcondition amount <= balance // wrong postcondition formulation q Contract: If caller fulfills precondition, method must fulfill postcondition Chapter 8 Designing Classes 41
Self Check 10. Why might you want to add a precondition to a method that you provide for other programmers? 11. When you implement a method with a precondition and you notice that the caller did not fulfill the precondition, do you have to notify the caller? Chapter 8 Designing Classes 42
Answers 10. Then you don't have to worry about checking for invalid values since it is the caller's responsibility 11. No, you can take any action that is convenient for you Chapter 8 Designing Classes 43
Static Methods q Every method must be in a class q A static method is not invoked on an object q Why write a method that does not operate on an object? § Usually, computation involves only numbers § Numbers are not objects, so you cannot invoke methods on them § For example, x. sqrt() is never legal in Java Chapter 8 Designing Classes 44
Static Methods q public class Financial { public static double percent. Of(double p, double a) { return (p / 100) * a; } // More financial methods can be added here. } q Call static methods with class name instead of object q doublemain tax =is. Financial. percent. Of(tax. Rate, total); Note: static since there are no objects Chapter 8 Designing Classes 45
Self Check 12. Suppose Java had no static methods. Then all methods of the Math class would be instance methods. How would you compute the square root of x? 13. Harry turns in his homework assignment, a program that plays tic-tac-toe. His solution consists of a single class with many static methods. Why is this not an object-oriented solution? Chapter 8 Designing Classes 46
Answers 12. 13. Math m = new Math(); y = m. sqrt(x); In an object-oriented solution, the main method would construct objects of classes Game, Player, etc. , and most methods would be instance methods that depend on the state of these objects Chapter 8 Designing Classes 47
Static Fields q q A static field belongs to the class, not to any particular object of the class (also called class field) public class Bank. Account {. . . private double balance; private int account. Number; private static int last. Assigned. Number = 1000; } If last. Assigned. Number was not static, each instance of Bank. Account would have its own value of last. Assigned. Number Chapter 8 Designing Classes 48
Static Fields q q public Bank. Account() { // Generates next account number to be assigned last. Assigned. Number++; // Updates the static field // Assigns field to account number of this bank account. Number = last. Assigned. Number; // Sets the instance field } Minimize the use of static fields § Except for static final fields (constants) Chapter 8 Designing Classes 49
Static Fields q Three ways to initialize 1. Do nothing --- field initialized with 0 (for numbers), false (for boolean values), or null (for objects) 2. Use an explicit initializer, such as public class Bank. Account {. . . private static int last. Assigned. Number = 1000; // Executed once, // when class is loaded 3. } Use a static initialization block Chapter 8 Designing Classes 50
Static Fields q Static fields should always be declared as private q Exception: static constants may be either private or public class Bank. Account {. . . public static final double OVERDRAFT_FEE = 5; // Refer to it as // Bank. Account. OVERDRAFT_FEE } Chapter 8 Designing Classes 51
A Static Field and Instance Fields Figure 5: A Static Field and Instance Fields Chapter 8 Designing Classes 52
Self Check 14. Name two static fields of the System class 15. Harry tells you that he has found a great way to avoid those pesky objects: Put all code into a single class and declare all methods and fields static. Then main call the other static methods, and all of them can access the static fields. Will Harry's plan work? Is it a good idea? Chapter 8 Designing Classes 53
Answers 14. System. in and System. out 15. Yes, it works. Static methods can access static fields of the same class. But it is a terrible idea. As your programming tasks get more complex, you will want to use objects and classes to organize your programs. Chapter 8 Designing Classes 54
Scope of Local Variables q q Scope of variable is region of program in which the variable can be accessed Scope of local variable extends from its declaration to end of block that encloses it Chapter 8 Designing Classes 55
Scope of Local Variables q What if same variable name used in two methods of same class? public class Rectangle. Tester { public static double area(Rectangle rect) { double r = rect. get. Width() * rect. get. Height(); return r; } public static void main(String[] args) { Rectangle r = new Rectangle(5, 10, 20, 30); double a = area(r); System. out. println(r); } } Chapter 8 Designing Classes 56
Scope of Local Variables q q On previous slide, variable r appears twice These variables are independent from each other since their scopes are disjoint Chapter 8 Designing Classes 57
Scope of Local Variables q Scope of a local variable cannot contain the definition of another variable of same name Rectangle r = new Rectangle(5, 10, 20, 30); if (x >= 0) { double r = Math. sqrt(x); // Error–can't declare another variable called r here. . . } Chapter 8 Designing Classes 58
Scope of Local Variables q However, can have local variables with identical names if scopes do not overlap if (x >= 0) { double r = Math. sqrt(x); . . . } // Scope of r ends here else { Rectangle r = new Rectangle(5, 10, 20, 30); // OK–it is legal to declare another r here. . . } Chapter 8 Designing Classes 59
Scope of Class Members q Private has class scope § Can access in any method of the class § But cannot be accessed outside of class q Public can be accessed outside of class § Must qualify public members outside scope Math. sqrt harrys. Checking. get. Balance Chapter 8 Designing Classes 60
Scope of Class Members q q Inside a method, no need to qualify fields or methods that belong to the same class An unqualified instance field or method name refers to the this parameter public class Bank. Account { public void transfer(double amount, Bank. Account other) { withdraw(amount); // i. e. , this. withdraw(amount); other. deposit(amount); }. . . } Chapter 8 Designing Classes 61
Overlapping Scope q Local variable can shadow field of same name § In this case, local scope “wins” § Then how to access shadowed field? public class Coin {. . . public double get. Exchange. Value(double exchange. Rate) { double value; // Local variable. . . return value; } private String name; private double value; // Field with the same name } Chapter 8 Designing Classes 62
Overlapping Scope q Access shadowed fields by qualifying them with the this reference value = this. value * exchange. Rate; q Best to avoid confusion, so avoid shadowing Chapter 8 Designing Classes 63
Self Check 6. In the deposit method of the Bank. Account class, what is the scope of the parameter variable amount and the local variable new. Balance? 1. public void deposit(double amount) 2. { 3. 7. 4. double new. Balance = balance + amount; balance = new. Balance; What is the scope of the balance field of the Bank. Account class? 5. } Chapter 8 Designing Classes 64
Answers 6. The scope of amount is the entire deposit method while the scope of new. Balance starts at the point at which the variable is defined and extends to the end of the method 7. It starts at the beginning of the class and ends at the end of the class Chapter 8 Designing Classes 65
Organizing Related Classes Into Packages q q Package is a set of related classes To put classes in a package, put package as the first instruction in the source file containing the classes, for example… package. Name; q Package name consists of one or more identifiers separated by periods Chapter 8 Designing Classes 66
Organizing Related Classes Into Packages q For example, to put the Financial class in a package named com. horstmann. bigjava, the Financial. java file must start with package com. horstmann. bigjava; q public class Financial {. . . } Default package has no name, so package statement is not required Chapter 8 Designing Classes 67
Organizing Related Classes Into Packages Package Purpose Sample Class java. lang Language Support Math java. util Utilities Random java. io Input and Output Print. Screen Java. awt Abstract Windowing Toolkit Color Java. applet Applets Applet Java. net Networking Socket Java. sql Database Access Result. Set Java. swing Swing user interface JButton Org. omg. COBRA Common Object Request Broker Architecture Int. Holder Chapter 8 Designing Classes 68
Syntax 9. 2: Package Specification package. Name; Example: package com. horstmann. bigjava; Purpose: To declare that all classes in this file belong to a particular package Chapter 8 Designing Classes 69
Importing Packages q Can always use class without importing java. util. Scanner in = new java. util. Scanner(System. in); q But, tedious to use fully qualified name q Use import, then use shorter class name import java. util. Scanner; . . . Scanner in = new Scanner(System. in) Chapter 8 Designing Classes 70
Importing Packages q Can import all classes in a package import java. util. *; q q Never need to import java. lang You do not need to import other classes in the same package Chapter 8 Designing Classes 71
Package Names and Locating Classes q Use packages to avoid name clashes java. util. Timer vs. javax. swing. Timer q Package names should be unambiguous q Recommendation: start with reversed domain name com. horstmann. bigjava q Use edu. sjsu. cs. walters for Bertha Walters' classes (assuming email is walters@cs. sjsu. edu) Chapter 8 Designing Classes 72
Package Names and Locating Classes q Path name should match package name com/horstmann/bigjava/Financial. java q Path name starts with class path export CLASSPATH=/home/walters/lib: . set CLASSPATH=c: homewalterslib; . q Class path contains the base directories that may contain package directories Chapter 8 Designing Classes 73
Base Directories and Subdirectories for Packages Figure 6: Base Directories and Subdirectories for Packages Chapter 8 Designing Classes 74
Self Check 18. Which of the following are packages? a. b. c. d. 19. java. lang java. util java. lang. Math Can you write a Java program without ever using import statements? Chapter 8 Designing Classes 75
Self Check 20. Suppose your homework assignments are located in the directory /home/me/cs 101 (c: mecs 101 on Windows). Your instructor tells you to place your homework into packages. In which directory do you place the class hw 1. problem 1. Tic. Tac. Toe. Tester? Chapter 8 Designing Classes 76
Answers 18. a. b. c. d. No Yes No 19. Yes, if you use fully qualified names for all classes, such as java. util. Random and java. awt. Rectangle 20. /home/me/cs 101/hw 1/problem 1 or, on Windows, c: mecs 101hw 1problem 1 Chapter 8 Designing Classes 77
The Explosive Growth of Personal Computers Figure 7: The Visi. Calc Spreadsheet Running on the Apple 2 Chapter 8 Designing Classes 78
- Solidworks mirror block
- Classe e a subclasse
- Pre ap classes vs regular classes
- Product design and development plan example
- How to designing and managing service processes
- Chapter 7 designing organizational structure
- Chapter 7 designing organizational structure
- Matched pair design
- Services are typically produced and consumed simultaneously
- Channel design decisions with examples
- Chapter 4 designing studies
- Chapter 2 asset classes and financial instruments
- Task centered approach example
- Isi dan struktur pesan kreatif
- Design a secure network
- Patrick baudisch
- Rpi rpd
- Designing customer oriented marketing channels
- Egalitarian vs hierarchical pay structure
- The process of designing and maintaining an environment
- Human resources defintion
- Interdisciplinary thematic units
- You are designing a sticker to advertise your band
- Computer aided embroidery and designing
- Steps of channel design
- Ros smith
- Pay level and pay mix
- Designing motivating jobs adalah
- Designing marketing program to build brand equity
- Output design
- Designing effective hrd programs pdf
- Designing classroom language tests
- Service marketing triangle of pizza hut
- Designing and managing products
- Designing and delivering oral and online presentation
- Zip line design formula
- Flat roof steel truss design
- Customer driven marketing strategy steps
- Quality design process
- An organized method for making good buying decisions
- Chapter 4 product and service design
- Fleishman job analysis system
- Where were sneakers manufactured until the 1960s
- Designing and managing services
- Designing effective input
- Writing territories examples
- Designing interfaces and dialogues
- Designing and implementing branding strategies
- Designing and implementing brand marketing programs
- Web designing dmis
- The art and science of designing and constructing buildings
- Designing and managing integrated marketing communication
- First step in designing an experiment
- Which is not an objective of designing interfaces?
- The hand warmer design challenge
- Processfig
- When to use product oriented based assessment
- Designing adaptive organizations
- Test of detail
- Designing product families
- Designing learner-centric moocs
- Web interface design in hci
- Textbook development process
- Designing embedded systems with pic microcontrollers
- Designing efficient workbooks
- Pay structure architecture
- Supporting multiple window systems in design patterns
- Obero spm
- A pedagogy of multiliteracies designing social futures
- Arrangement of element of curriculum
- Designing organization for international environment
- Layout
- Impulse invariant method iir filter design
- Factors influencing distribution network design
- The act of designing the company's offering and image
- Designing and managing value networks
- Marketing channels and value networks
- Designing and managing value networks
- How to divide sales territories