Aalborg Media LabApr 18, 2023
Software Design
Lecture 7
“Object Oriented Design”
Aalborg Media LabApr 18, 2023
Object Oriented Design
• Chapter 6
– Relationships among classes– The static modifier– Formal object interfaces
Aalborg Media LabApr 18, 2023 3
6.1 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 LabApr 18, 2023 4
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 LabApr 18, 2023 5
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 LabApr 18, 2023 6
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 LabApr 18, 2023 7
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 LabApr 18, 2023
6.2 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 LabApr 18, 2023 9
6.3 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 LabApr 18, 2023 10
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 LabApr 18, 2023 11
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 LabApr 18, 2023 12
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 LabApr 18, 2023
6.4 Class Relationships
• “uses-a” relationship(one object uses methods of another)
• “has-a” relationship (one object contain another)
• “is-a” relationship(inheritance)
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
UML Class Diagrams
• A UML class diagram for the Rolling Dices program:
RolingDices
main (args : String[]) : void
Die
faceValue : int
roll() : voidgetFaceValue() : booleantoString() : String
1 2
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
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 Bookwrites
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
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
1 2Student
- firstName : String- lastName : String- homeAddress : Address- schoolAddress : Address
+ toString() : String
- streetAddress : String- city : String- state : String- zipCode : long
Address
Aalborg Media LabApr 18, 2023
6.5 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 LabApr 18, 2023
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 wordNone of the methods inan interface are given
a definition (body)
A semicolon immediatelyfollows each method header
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
Interfacespublic class CanDo implements Doable{ public void doThis () { // whatever }
public void doThat () { // whatever }
// etc.}
implements is areserved word
Each method listedin Doable is
given a definition
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
6.7 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 LabApr 18, 2023 30
6.8 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 LabApr 18, 2023
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 LabApr 18, 2023 32
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 LabApr 18, 2023 33
Overloading Methods
• Constructors can be overloaded
• Overloaded constructors provide multiple ways to initialize a new object
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
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 LayoutBorder LayoutCard LayoutGrid LayoutGridBag LayoutBox LayoutOverlay Layout
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
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 LabApr 18, 2023
Demo – Flow Layout
Aalborg Media LabApr 18, 2023
Border Layout
• A border layout defines five areas to which components can be added
North
South
Center EastWest
Aalborg Media LabApr 18, 2023
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 LabApr 18, 2023
LayoutDemo - Border
Aalborg Media LabApr 18, 2023
Containment Hierarchies
Aalborg Media LabApr 18, 2023
Exercises
• Identify Objects including behavior and features
• 6.3, 6.14
Top Related