Cse 142-Object Oriented Programming: Lecture # 6 Uml Diagrams
Cse 142-Object Oriented Programming: Lecture # 6 Uml Diagrams
LECTURE # 6
UML DIAGRAMS
3-5
TYPES OF CLASSES
• Ones found during analysis:
–people, places, events, and things about which
the system will capture information
–ones found in application domain
• Ones found during design
–specific objects like windows and forms that
are used to build the system
2 KINDS OF CLASSES
DURING ANALYSIS
• Concrete
– Class from application domain
– Example: Customer class and Employee class
• Abstract
– Useful abstractions
– Example: Person class
ATTRIBUTES
• The data elements of a class define the object to be instantiated from the class.
• The attributes must be specific to the class and define it completely.
• Example: A rectangle is defined by
– length
– width.
• The attributes are then accessed by methods within the class.
3-8
ATTRIBUTES IN A CLASS
• Properties of the class about which we
want to capture information
• Represents a piece of information that is
relevant to the description of the class
within the application domain
ATTRIBUTES IN A CLASS
• Only add attributes that are primitive or
atomic types
• Derived attribute
–attributes that are calculated or derived from
other attributes
METHODS
3-11
ACCESSORS AND MUTATORS
3-12
ACCESSORS AND MUTATORS
3-13
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.
3-14
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.
Variable types are
Rectangle placed after the variable
name, separated by a
colon.
- width : double
3-15
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.
3-16
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.
Method parameters
are shown inside the
parentheses using the Rectangle
same notation as
variables.
- width : double
3-17
CONVERTING THE UML DIAGRAM TO CODE
• Putting all of this information together, a Java class file can be built easily
using the UML diagram.
• The UML diagram parts match the Java class file structure.
class header
ClassName
{
Attributes Attributes
Methods Methods
}
3-18
CONVERTING THE UML DIAGRAM TO CODE
The structure of the class can be public class Rectangle
{
compiled and tested without having
private double width;
bodies for the methods. Just be sure to private double length;
put in dummy return values for methods
that have a return type other than void. public void setWidth(double w)
{
}
public void setLength(double len)
{
}
public double getWidth()
{ return 0.0;
}
public double getLength()
{ return 0.0;
}
public double getArea()
{ return 0.0;
}
}
3-19
Rectangle
- width : double
- length : double
Rectangle
Notice there is no
return type listed
- width : double for constructors.
- length : double
+Rectangle(len:double, w:double)
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
3-22
CLASS LAYOUT CONVENTIONS
3-23
A DRIVER PROGRAM
3-24
A DRIVER PROGRAM
public class RectangleDemo public class Rectangle
{ {
public static void main(String[] args) private double width;
{ private double length;
Rectangle r = new Rectangle();
r.setWidth(10); public void setWidth(double w)
r.setLength(10); {width = w;
System.out.println("Width = " }
+ r.getWidth()); public void setLength(double len)
System.out.println("Length = " {length = len;
+ r.getLength()); }
System.out.println("Area = " public double getWidth()
+ r.getArea()); {return width;
} }
} public double getLength()
{return length;
This RectangleDemo class is a }
public double getArea()
Java application that uses the {return length * width;
Rectangle class. }
}
Another Example:
LengthWidthDemo.java
3-25
Example of a Class Diagram
Video Rental System
visibility multiplicity class name
Write the code for class myPoint. Also write a test program (called TestMyPoint) to test
all the methods defined n the class.