Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”

Slides:



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

Interfaces A Java interface is a collection
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes
Inheritance Inheritance Reserved word protected Reserved word super
Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Object Oriented Design and UML
Writing Classes (Chapter 4)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
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.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
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.
Lecture 4 b Writing Classes b class declarations b method declarations b constructors b instance variables b encapsulation b method overloading b program.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
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.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
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,
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Enhancing Classes
Chapter 9: Graphical User Interfaces
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
Object Oriented Programming
Chapter 4: Writing classes
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
Chapter 4 Writing Classes.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming Review
Outline Software Development Activities
Object-Oriented Design Part 2
Chapter 4: Writing Classes
Presentation transcript:

Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”

Aalborg Media Lab 28-Jun-15 Object Oriented Design Chapter 6 –Relationships among classes –The static modifier –Formal object interfaces

Aalborg Media Lab 28-Jun Program Development Activities The creation of software involves four basic activities: –establishing the requirements –creating a design –implementing the code –testing the implementation

Aalborg Media Lab 28-Jun-154 Requirements Software requirements specify the tasks a program must accomplish (what to do, not how to do it) They often include a description of the user interface An initial set of requirements often are provided, but usually must be critiqued, modified, and expanded Often it is difficult to establish detailed, unambiguous, complete requirements Careful attention to the requirements can save significant time and expense in the overall project

Aalborg Media Lab 28-Jun-155 Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal An algorithm may be expressed in pseudo code, which is code-like, but does not necessarily follow any specific syntax In object-oriented development, the design establishes the classes, objects, methods, and data that are required

Aalborg Media Lab 28-Jun-156 Implementation Implementation is the process of translating a design into source code Most novice programmers think that writing code is the heart of software development, but actually it should be the least creative step Almost all important decisions are made during requirements and design stages Implementation should focus on coding details, including style guidelines and documentation

Aalborg Media Lab 28-Jun-157 Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process of discovering the causes of problems and fixing them Programmers often think erroneously that there is "only one more bug" to fix Tests should consider design details as well as overall requirements

Aalborg Media Lab 28-Jun Identifying Objects/Classes Determining the classes contributing to the program is the fundamental part of OOP. Objects are generally nouns in the problem specification Similar objects  class Then specify class behavior and features

Aalborg Media Lab 28-Jun The static Modifier For example, the methods of the Math class are static: Math.sqrt (25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object

Aalborg Media Lab 28-Jun-1510 Static Variables Static variables are also called class variables 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 in which it is declared is loaded All objects created from the class share static variables Changing the value of a static variable in one object changes it for all others

Aalborg Media Lab 28-Jun-1511 Static Methods public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method can be invoked as: value = Helper.triple (5);

Aalborg Media Lab 28-Jun-1512 Static Methods The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system 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

Aalborg Media Lab 28-Jun Class Relationships “uses-a” relationship (one object uses methods of another) “has-a” relationship (one object contain another) “is-a” relationship (inheritance)

Aalborg Media Lab 28-Jun-15 UML Diagrams UML diagrams show relationships among classes and objects A UML class diagram consists of one or more classes, each with sections for the class name, attributes, and methods Lines between classes represent associations Associations can show multiplicity

Aalborg Media Lab 28-Jun-15 UML Class Diagrams A UML class diagram for the Rolling Dices program: RolingDices main (args : String[]) : void Die faceValue : int roll() : void getFaceValue() : boolean toString() : String 12

Aalborg Media Lab 28-Jun-15 UML Diagrams A UML object diagram consists of one or more instantiated objects. It is a snapshot of the objects during an executing program, showing data values die1 : Die faceValue = 5 die2 : Die Value = 1

Aalborg Media Lab 28-Jun-15 Use Relationship A general association, as we've seen in UML diagrams, is sometimes referred to as a use relationship A general association indicates that one object (or class) uses or refers to another object (or class) in some way We could even annotate an association line in a UML diagram to indicate the nature of the relationship Author Book writes

Aalborg Media Lab 28-Jun-15 Aggregation An aggregate object is an object that contains references to other objects For example, an Account object contains a reference to a String object (the owner's name) An aggregate object represents a has-a relationship A bank account has a name Likewise, a student may have one or more addresses

Aalborg Media Lab 28-Jun-15 Aggregation in UML An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end StudentBody + main (args : String[]) : void + toString() : String 12 Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + toString() : String - streetAddress : String - city : String - state : String - zipCode : long Address

Aalborg Media Lab 28-Jun 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, as a formal contract, a set of methods that a class will implement

Aalborg Media Lab 28-Jun-15 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

Aalborg Media Lab 28-Jun-15 Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by –stating so 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

Aalborg Media Lab 28-Jun-15 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

Aalborg Media Lab 28-Jun-15 Interfaces A class that implements an interface can implement other methods as well 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

Aalborg Media Lab 28-Jun-15 Interfaces A class can implement multiple interfaces, these 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 }

Aalborg Media Lab 28-Jun-15 Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains an abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order The Iterator interface contains methods that allow the user to move easily through a collection of objects

Aalborg Media Lab 28-Jun-15 Why use interfaces? When the user presses a button, the Java run-time system sends an actionPerformed() message to any object that is listening for the button’s events. If we want our listener to listen, it must listen for that particular message. When the user adjusts a slider, the Java run-time system sends an adjustmentValueChanged() message to any object that is listening for the slider’s events. If we want our listener to listen, it must listen for that particular message.

Aalborg Media Lab 28-Jun-15 More generally, why do we use interfaces? To allow our objects to work inside an existing framework. To allow programmers to create objects that fulfill responsibilities — without committing to how their objects do so! Sometimes called a protocol.

Aalborg Media Lab 28-Jun Method Design 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 service method of an object may call one or more support methods to accomplish its goal Support methods could call other support methods if appropriate

Aalborg Media Lab 28-Jun Overloading Methods Method overloading is the process of using the same method name for multiple methods The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The compiler determines which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature

Aalborg Media Lab 28-Jun-15 Overloading Methods float tryMe (int x) { return x +.375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32) Invocation

Aalborg Media Lab 28-Jun-1532 Overloaded Methods The println method is overloaded: println(String s),println(int i) … The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

Aalborg Media Lab 28-Jun-1533 Overloading Methods Constructors can be overloaded Overloaded constructors provide multiple ways to initialize a new object

Aalborg Media Lab 28-Jun-15 Testing Testing is no guarantee for the absence of errors Reviews –Presenting designs to each other in project-group Defect Testing –Test cases (how does the user normally use the program?) –White-Box vs. Black-Box Testing Do it, but with a concept!!!

Aalborg Media Lab 28-Jun-15 GUI Design The GUI designer should: –Know the users and their needs –Prevent user errors whenever possible –Optimize user abilities and make information readily available –Be consistent with placement of components and color schemes

Aalborg Media Lab 28-Jun-15 Layout Managers A layout manager is an object that determines the manner in which components are arranged in a container There are several predefined layout managers defined in the Java standard class library: Defined in the AWT Defined in Swing Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Box Layout Overlay Layout

Aalborg Media Lab 28-Jun-15 Layout Managers Every container has a default layout manager, but we can explicitly set the layout manager as well Each layout manager has its own particular rules governing how the components will be arranged Some layout managers pay attention to a component's preferred size or alignment, while others do not A layout manager attempts to adjust the layout as components are added and as containers are resized

Aalborg Media Lab 28-Jun-15 Layout Managers We can use the setLayout method of a container to change its layout manager JPanel panel = new JPanel(); panel.setLayout (new BorderLayout()); The following example uses a tabbed pane, a container which permits one of several panes to be selected

Aalborg Media Lab 28-Jun-15 Flow Layout Flow layout puts as many components as possible on a row, and then moves to the next row Rows are created as needed to accommodate all of the components Components are displayed in the order they are added to the container Each row of components is centered horizontally in the window by default, but could also be aligned left or right The horizontal and vertical gaps between the components can be explicitly set also

Aalborg Media Lab 28-Jun-15 Demo – Flow Layout

Aalborg Media Lab 28-Jun-15 Border Layout A border layout defines five areas to which components can be added North South CenterEastWest

Aalborg Media Lab 28-Jun-15 Border Layout Each area displays one component (which could be another container such as a JPanel ) Each of the four outer areas enlarges as needed to accommodate the component added to it If nothing is added to the outer areas, they take up no space and other areas expand to fill the void The center area expands to fill space as needed

Aalborg Media Lab 28-Jun-15 LayoutDemo - Border

Aalborg Media Lab 28-Jun-15 Containment Hierarchies

Aalborg Media Lab 28-Jun-15 Exercises Identify Objects including behavior and features 6.3, 6.14