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

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Interfaces A Java interface is a collection
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 6 Object-Oriented Design 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Chapter Day 16. © 2007 Pearson Addison-Wesley. All rights reserved6-2 Agenda Day 16 Problem set 3 posted  10 problems from chapters 5 & 6  Due in one.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Chapter Day 15. © 2007 Pearson Addison-Wesley. All rights reserved6-2 Agenda Day 15 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 7.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Object Oriented Design and UML
© 2004 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 : Object-Oriented Design Intermediate Java Programming Summer 2007.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 6 Object-Oriented Design Part 1. © 2004 Pearson Addison-Wesley. All rights reserved2/42 Object-Oriented Design Now we can extend our discussion.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Inheritance Reading for this Lecture: L&L 6.4 – 6.5.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Programming in Java (COP 2250) Lecture 14 & 15 Chengyong Yang Fall, 2005.
ELC 312 DAY 11 & 12. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Agenda Questions? Problem set 2 Corrected  2 A’s and 2 B’s Problem set 3 Posted.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Static class members.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Object-Oriented Design. Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of.
University of Turkish Aeronautical Association Computer Engineering Department CENG 112 COMPUTER PROGRAMMING 2 Tansel Dökeroglu, Ph.D.
Chapter 5: Enhancing Classes
Chapter 4: Writing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Chapter 6: Object-Oriented Design
Passing Objects to Methods
Object Oriented Programming
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 5: Enhancing Classes
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming Review
Object-Oriented Design
Object-Oriented Design Part 2
Chapter 4: Writing Classes
Presentation transcript:

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

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

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

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

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

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.

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)

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.

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

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.”

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

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

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 … }

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++ }

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(){ … } }

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

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);

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

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

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

Dependency class Address { private String streetNum: private String city; private String state; private String zip; … } class Address { private String streetNum: private String city; private String state; private String zip; … } class Student { private String name; private Address homeAddress; private Address dormAddress; … } class Student { private String name; private Address homeAddress; private Address dormAddress; … }

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.

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

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, Transmission, etc. Car has-a Chassis, Engine, Transmission, etc. An aggregate object contains objects of other classes as instance variables. An aggregate object contains objects of other classes as instance variables.

Aggregation class Engine { private in size; private String type; private int pistons; … } class Engine { private in size; private String type; private int pistons; … } class Transmission { private String type; private int gears; … } class Transmission { private String type; private int gears; … } class Car { private Engine engine; private Transmission transmission; private Chasis chasis; … } class Car { private Engine engine; private Transmission transmission; private Chasis chasis; … }

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

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 String

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; } …

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

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.

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); }

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.

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.

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

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

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).

PigLatinTranslater Pig Latin is a language whereby each English word is modified by moving the initial letter of the word to the end and adding "ay“. (e.g., book  ookbay) Pig Latin is a language whereby each English word is modified by moving the initial letter of the word to the end and adding "ay“. (e.g., book  ookbay) 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.

Pig Latin Translater 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 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 Pig Latin 101 Pig Latin 101 Pig Latin 101 Pig Latin 101

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

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.

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

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.

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

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”);

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

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

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