Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s topics UML Diagramming review of terms

Similar presentations


Presentation on theme: "Today’s topics UML Diagramming review of terms"— Presentation transcript:

1

2 Today’s topics UML Diagramming review of terms
stale data or why we don’t store derived data this no-arg or default constructors copy constructors equals or using this and other

3 UML Diagram Unified Modeling Language (UML) provides a set of standard diagrams for graphically depicting object-oriented systems. Class name goes here Fields are listed here Methods are listed here

4 UML Diagram for Rectangle class
length width setLength() setWidth() getLength() getWidth() getArea()

5 UML Data Type and Parameter Notation
UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc. Rectangle - width : double + setWidth(w : double) : void Access modifiers are denoted as: + public - private

6 Converting the UML Diagram to Code
public class Rectangle { private double width; private double length; public void setWidth(double w) } public void setLength(double len) public double getWidth() { return 0.0; public double getLength() public double getArea() The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void. Rectangle width : double length : double + setWidth(w : double) : void + setLength(len : double): void + getWidth() : double + getLength() : double + getArea() : double

7 Converting the UML Diagram to Code
public class Rectangle { private double width; private double length; public void setWidth(double w) { width = w; } public void setLength(double len) { length = len; public double getWidth() { return width; public double getLength() { return length; public double getArea() { return length * width; Once the class structure has been tested, the method bodies can be written and tested. Rectangle width : double length : double + setWidth(w : double) : void + setLength(len : double): void + getWidth() : double + getLength() : double + getArea() : double

8 UML for the Car class that we worked on in lab. This is every-
year : int make : String model : String speed : int -MAX_SPEED: int = 150 UML for the Car class that we worked on in lab. This is every- thing that you need to make stubs. It is not everything you need to build the methods. +Car(inYear: int, make: int, model: int) + accelerate():void + accelerate(amt: int):void + brake(): void + brake(amt: int): void + getModel(): String + getSpeed(): int + getYear(): int + toString(): String

9 Class Layout Conventions
The layout of a source code file can vary by employer or instructor. A common layout is: Fields listed first public constants private constants private attributes Methods listed second Constructors first public next private last

10 Review of terminology Object – instance of a class
Class – provides a blueprint or template for a class of objects Attribute – instance variable – instance field private – public encapsulation data hiding accessor - mutator

11 Stale Data Some data is the result of a calculation.
Consider the area of a rectangle. length × width It would be impractical to use an area variable here. Data that requires the calculation of various factors has the potential to become stale. To avoid stale data, it is best to calculate the value of that data within a method rather than store it in a variable.

12 Stale Data Rather than use an area variable in a Rectangle class:
public double getArea() { return length * width; } This dynamically calculates the value of the rectangle’s area when the method is called. Now, any change to the length or width variables will not leave the area of the rectangle stale.

13 Constructors Constructors have a few special properties that set them apart from normal methods. Constructors have the same name as the class. Constructors have no return type (not even void). Constructors may not return any values. Constructors are typically public.

14 The Default Constructor
When an object is created, its constructor is always called. If you do not write a constructor, Java provides one when the class is compiled. The constructor that Java provides is known as the default constructor. It sets all of the object’s numeric fields to 0. It sets all of the object’s boolean fields to false. It sets all of the object’s reference variables to the special value null.

15 The Default Constructor
The default constructor is a constructor with no parameters, used to initialize an object in a default configuration. The only time that Java provides a default constructor is when you do not write any constructor for a class. See example: First version of Rectangle.java A default constructor is not provided by Java if a constructor is already written. See example: Rectangle.java with Constructor

16 Writing Your Own No-Arg Constructor
A constructor that does not accept arguments is known as a no-arg constructor. The default constructor (provided by Java) is a no-arg constructor. We can write our own no-arg constructor public Rectangle() { length = 1.0; width = 1.0; }

17 Overloaded Methods - Method Signature and Binding
A method signature consists of the method’s name and the data types of the method’s parameters, in the order that they appear. The return type is not part of the signature. add(int, int) add(String, String) The process of matching a method call with the correct method is known as binding. The compiler uses the method signature to determine which version of the overloaded method to bind the call to. Signatures of the add methods of previous slide

18 Scope of Instance Fields
Variables declared as instance fields in a class can be accessed by any instance method in the same class as the field. If an instance field is declared with the public access specifier, it can also be accessed by code outside the class, as long as an instance of the class exists.

19 Shadowing A parameter variable is, in effect, a local variable.
Within a method, variable names must be unique. A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field. Shadowing is discouraged and local variable names should not be the same as instance field names.

20 The this Reference – avoiding the ambiguity of shadowing
The this reference is simply a name that an object can use to refer to itself. The this reference can be used to overcome shadowing and allow a parameter to have the same name as an instance field. public void setFeet(int feet) { this.feet = feet; //sets the this instance’s feet field //equal to the parameter feet. } Local parameter variable feet Shadowed instance variable

21 The toString Method The toString method of a class can be called explicitly: Stock xyzCompany = new Stock ("XYZ", 9.62); System.out.println(xyzCompany.toString()); However, the toString method does not have to be called explicitly but is called implicitly whenever you pass an object of the class to println or print. System.out.println(xyzCompany);

22 The toString Method All objects have a toString method that returns the class name and a hash of the memory address of the object. We can override the default method with our own to print out more useful information. Examples: Stock.java, StockDemo1.java

23 The equals Method When the == operator is used with reference variables, the memory address of the objects are compared. The contents of the objects are not compared. All objects have an equals method. The default operation of the equals method is to compare memory addresses of the objects (just like the == operator).

24 The equals Method Instead of using the == operator to compare two Stock objects, we should use the equals method. Now, objects can be compared by their contents rather than by their memory addresses. See example: StockCompare.java public boolean equals(Stock object2) { boolean status; if(symbol.equals(Object2.symbol && sharePrice == Object2.sharePrice) status = true; else status = false; return status; }

25 Copy Constructors A copy constructor accepts an existing object of the same class and clones it public Stock(Stock object 2) { symbol = object2.symbol; sharePrice = object2.sharePrice; } // Create a Stock object Stock company1 = new Stock("XYZ", 9.62); //Create company2, a copy of company1 Stock company2 = new Stock(company1);


Download ppt "Today’s topics UML Diagramming review of terms"

Similar presentations


Ads by Google