Presentation is loading. Please wait.

Presentation is loading. Please wait.

6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.

Similar presentations


Presentation on theme: "6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus."— Presentation transcript:

1 6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus

2 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

3 Software Development Steps Analyze and Establish Requirements Analyze and Establish Requirements What are needed? Not, how to do them. What are needed? Not, how to do them. Develop Software Design Develop Software Design How to accomplish goals; How to divide the solution; What classes are needed? How to accomplish goals; How to divide the solution; What classes are needed? Implement Programs Implement Programs Coding, using programming language(s) Coding, using programming language(s) Test Programs Test Programs Debug logic error; check against requirements; test usablility Debug logic error; check against requirements; test usablility

4 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

5 Identifying Classes & Objects Object-Oriented Design Object-Oriented Design What kind of objects are involved in the problem? What kind of objects are involved in the problem? Start with nouns in the problem description Start with nouns in the problem description

6 Identifying Classes & Objects A partial requirements document: A partial requirements document: Of course, not all nouns will correspond to class objects in the final solution. Of course, not all nouns will correspond to class objects in the final solution. The user must be allowed to specify each product by its primary characteristics, including its name and product number. If the bar code does not match the product, then an error should be generated to the message window and entered into the error log. The summary report of all transactions must be structured as specified in section 7.A.

7 Possible Classes User User Product (name, productNumber) Product (name, productNumber) BarCode (value, productNumber) BarCode (value, productNumber) MessageWindow (message) MessageWindow (message) ErrorLog (date, time, message) ErrorLog (date, time, message) SummaryReport (date, header, message) SummaryReport (date, header, message) Transactions (date, transactionType, amount) Transactions (date, transactionType, amount)

8 Identifying Classes & Objects Remember, a class represents a group (classification) of objects with similar characteristics and behaviors. Remember, a class represents a group (classification) of objects with similar characteristics and behaviors. Generally, use a singular for class name: Student, Report, Window, BankAccount. Generally, use a singular for class name: Student, Report, Window, BankAccount. Once a class is defined, many objects of that class can be instantiated. Once a class is defined, many objects of that class can be instantiated.

9 Identifying Classes & Objects Sometimes, question arises: Should a name become a class or attribute? Sometimes, question arises: Should a name become a class or attribute? E.g.: Should address be represented by a set of instance variables in the Student class, or as an object of Address class? E.g.: Should address be represented by a set of instance variables in the Student class, or as an object of Address class? Ordinarily, address should be attribute. But sometimes, it may be worth making it a class. The decision depends on the amount of detail required by the solution Ordinarily, address should be attribute. But sometimes, it may be worth making it a class. The decision depends on the amount of detail required by the solution

10 Identifying Classes & Objects Part of identifying the classes is assigning responsibilities to each class. Which class does what? Part of identifying the classes is assigning responsibilities to each class. Which class does what? Every action in the program must be represented by a method assigned to one of the classes. Every action in the program must be represented by a method assigned to one of the classes. In the beginning of the design, verbs in the problem description give hints on identifying methods. In the beginning of the design, verbs in the problem description give hints on identifying methods. E.g., …”generate report” suggests that the class Report should have a method “generate.” E.g., …”generate report” suggests that the class Report should have a method “generate.”

11 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

12 Static Class Members Static Method Static Method Is invoked with the class name, not object name. E.g., result = Math.sqrt(11). Is invoked with the class name, not object name. E.g., result = Math.sqrt(11). Static Variable Static Variable Single value for the entire class, unlike instance variables Single value for the entire class, unlike instance variables Important design consideration Important design consideration Static Methods and Variables Static Methods and Variables Also called class method & class variable Also called class method & class variable Use modifier static Use modifier static

13 Static Variable Each object has its own variable space: E.g., class Student { String name; int age; … } Each object has its own variable space: E.g., class Student { String name; int age; … } Static variable is common to all objects: class Student { String name; int age; public static int count; // count of // students … } Static variable is common to all objects: class Student { String name; int age; public static int count; // count of // students … }

14 Static Variable (cont.) Static variable is common to all objects: class Student { private String name; private int age; private static int count; // count of // students … // Constructor Static variable is common to all objects: class Student { private String name; private int age; private static int count; // count of // students … // Constructor Student(String aName, int anAge){ name = aName; age = anAge; count++ } Student(String aName, int anAge){ name = aName; age = anAge; count++ }

15 Static Variable (cont.) public void setName(String aName){ … } public void setName(String aName){ … } public String getName(){ … } //Static method public String getName(){ … } //Static method public static int getCount()( return count; } public String toString(){ … } public static int getCount()( return count; } public String toString(){ … } }

16 Invoking Static Method Class TestStudent { Student john = new Student(“John”, 21); Student mary = new Student(“Mary”, 20); Student sam = new Student(“Sam”, 19); System.out.println(john); System.out.println(mary); System.out.println(sam); System.out.println(Student.getCount() + “ students were created.”); Class TestStudent { Student john = new Student(“John”, 21); Student mary = new Student(“Mary”, 20); Student sam = new Student(“Sam”, 19); System.out.println(john); System.out.println(mary); System.out.println(sam); System.out.println(Student.getCount() + “ students were created.”);

17 Static Method class Helper { public static int cube (int num) { return num * num * num; } } class Helper { public static int cube (int num) { return num * num * num; } } Because it is declared as static, the method can be invoked as int value = Helper.cube(5); Because it is declared as static, the method can be invoked as int value = Helper.cube(5);

18 Static Class Members By convention, visibility modifier comes first, then static modifier. By convention, visibility modifier comes first, then static modifier. Recall, method main() is a static method— invoked by the Java interpreter without an object instantiation. Recall, method main() is a static method— invoked by the Java interpreter without an object instantiation. Static method can reference static variables. Static method can reference static variables. Static method cannot reference instance variables—because they do not exist until an object is created. Static method cannot reference instance variables—because they do not exist until an object is created. Example Program Example Program SloganCounter.java SloganCounter.java SloganCounter.java Slogan.java Slogan.java Slogan.java

19 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

20 Class Relationship Classes in a software system can have different types of relationship. Classes in a software system can have different types of relationship. Dependency: A uses B Dependency: A uses B Aggregation: A has-a B Aggregation: A has-a B Inheritance: A is-a B Inheritance: A is-a B

21 Dependency Class A depends on class B when objects of class A relies on the services of objects of class B; e.g., by invoking methods of class B. Class A depends on class B when objects of class A relies on the services of objects of class B; e.g., by invoking methods of class B. This is reusability of code. Instead of writing a complex code in class A, make use of what’s in class B. This is reusability of code. Instead of writing a complex code in class A, make use of what’s in class B. On the other hand, we don’t want to have class A depend on too many classes so that it becomes too complex. On the other hand, we don’t want to have class A depend on too many classes so that it becomes too complex. We need a balance in design. We need a balance in design.

22 Dependency Some dependencies occur between objects of the same class. Fraction f1, f2, sum; … sum = f1.add(f2); Some dependencies occur between objects of the same class. Fraction f1, f2, sum; … sum = f1.add(f2); RationalTester.java RationalTester.java RationalTester.java RationalNumber.java RationalNumber.java RationalNumber.java

23 Aggregation An aggregate is an object which is made up of objects of other classes. An aggregate is an object which is made up of objects of other classes. Car has the following parts: chassis, engine, transmission, electricSystem, fuelSystem, etc. Car has the following parts: chassis, engine, transmission, electricSystem, fuelSystem, etc. Car has-a Chassis, Engine, etc. Car has-a Chassis, Engine, etc. An aggregate object contains objects of other classes as instance variables. class Car { private Chassis chasis; private Engine engine; … An aggregate object contains objects of other classes as instance variables. class Car { private Chassis chasis; private Engine engine; …

24 Example Program StudentBody.java StudentBody.java StudentBody.java Student.java Student.java Student.java Address.java Address.java Address.java

25 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

26 this Reference Inside a class object, this reference refers to itself. Inside a class object, this reference refers to itself. class Student { private String name; private int age; … Student (String name, int age) { this.name = name; this.age = age; } … class Student { private String name; private int age; … Student (String name, int age) { this.name = name; this.age = age; } …

27 Interface We have used “interface” in several contexts—GUI, set of public elements in a class, i.e., what the user sees. We have used “interface” in several contexts—GUI, set of public elements in a class, i.e., what the user sees. In Java, interface has a special meaning. In Java, interface has a special meaning. Interface Interface Collection of constants and abstract methods (no implementation) Collection of constants and abstract methods (no implementation) Like a class, but has no instance variables Like a class, but has no instance variables

28 Interface Abstract Method Abstract Method Has method header, but no body Has method header, but no body This means that that the user of an abstract method must provide an explicit implementation. This means that that the user of an abstract method must provide an explicit implementation. Interface provides a list of methods that can be used by multiply users, each providing different implementation. Interface provides a list of methods that can be used by multiply users, each providing different implementation.

29 Interface An interface contains only method headers. An interface contains only method headers. public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); }

30 Interface An interface cannot be instantiated. An interface cannot be instantiated. Methods in an interface have public visibility by defaul. Methods in an interface have public visibility by defaul. A class that uses an interface must implement all its abstract methods. A class that uses an interface must implement all its abstract methods.

31 Interface Class CanDo implements Doable { public DoThis { // whatever } public DoThat { // whatever } … } Class CanDo implements Doable { public DoThis { // whatever } public DoThat { // whatever } … } implements is a reserved word. implements is a reserved word.

32 Example Programs Complexity.java Complexity.java Complexity.java Question.java Question.java Question.java MiniQuiz.java MiniQuiz.java MiniQuiz.java Java Standard Class Library (Java API) contains many interfaces. E.g., Java Standard Class Library (Java API) contains many interfaces. E.g.,Java APIJava API Comparable Comparable Iterator Iterator

33 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

34 Method Design A method should be small enough to be understood easily (what it does) A method should be small enough to be understood easily (what it does) A complex method should invoke support methods (from the same class) or other methods (from other classes). A complex method should invoke support methods (from the same class) or other methods (from other classes).

35 PigLatinTranslater Pig Latin is a language whereby each English word is modified by moving the initial sound of the word to the end and adding "ay“. Pig Latin is a language whereby each English 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. Words that begin with vowels have the "yay" sound added on the end. book  ookbay table  abletay item  itemyay chair  airchay tree  reetay chicken  ickenchay book  ookbay table  abletay item  itemyay chair  airchay tree  reetay chicken  ickenchaybookookbay tableabletay itemitemyaychairairchay

36 PigLatin Translater Primary Method Primary Method String translate(String sentence) String translate(String sentence) This is complicated This is complicated Decompose method result  “” Repeat to end of sentence word  getNextWord(sentence) result  result + translateWord(word) End repeat Decompose method result  “” Repeat to end of sentence word  getNextWord(sentence) result  result + translateWord(word) End repeat

37 translateWord() Method private static String translateWord(String word) { String result = ""; if (beginsWithVowel(word)) if (beginsWithVowel(word)) result = word + "yay"; result = word + "yay"; else if (beginsWithBlend(word)) else if (beginsWithBlend(word))  result = word.substring(2) + word.substring(0,2) + "ay"; else else result = word.substring(1) + word.charAt(0) + "ay"; result = word.substring(1) + word.charAt(0) + "ay"; return result; return result;}  Consonant Blend: ch, th, sp, etc.

38 beginsWithVowel() Method private static boolean beginsWithVowel (String word) { String vowels = "aeiou"; String vowels = "aeiou"; char letter = word.charAt(0); char letter = word.charAt(0); return (vowels.indexOf(letter) != -1); return (vowels.indexOf(letter) != -1);}  PigLatin.java PigLatin.java  PigLatinTranslater.java PigLatinTranslater.java

39 Method Overloading Method Overloading Method Overloading Using same name for two or more methods which have different definitions Using same name for two or more methods which have different definitions Signature of a Method Signature of a Method Consists of the number, types, and order of parameters Consists of the number, types, and order of parameters A calling statement can distinguish two overloaded methods if they have different signatures. A calling statement can distinguish two overloaded methods if they have different signatures.

40 Method Overloading The compiler determines which method is invoked by analyzing its parameter list. The compiler determines which method is invoked by analyzing its parameter list. float tryMe(int x) { return x +.375; } float tryMe(int x, float y) { return x*y; } result = tryMe(25, 4.32) Invocation

41 Method Overloading System.out.println() is overloaded System.out.println() is overloaded println(“Hello”); // String println(5); // int println(2.32); // float println(“Hello”); // String println(5); // int println(2.32); // float Constructors are often overloaded. Constructors are often overloaded. Provides multiple ways of initializing an object. E.g., Provides multiple ways of initializing an object. E.g., Student = new Student(); Student = new Student(“John”, 23, “m”); Student = new Student(“John”); Student = new Student(); Student = new Student(“John”, 23, “m”); Student = new Student(“John”);

42 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

43 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout

44 Topics Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout


Download ppt "6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus."

Similar presentations


Ads by Google