Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction...

29
Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!
  • date post

    15-Jan-2016
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction...

Page 1: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Lecture #5Agenda

• Cell phones off & name signs out• Review• Questions?• UML class diagram introduction• Our first class definition!

Page 2: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Review

• Software development is an iterative and incremental process.

• OO software systems are systems of interacting objects.

• Objects have – properties (think of the counting object last class)– behaviors (think of the “jumping jack” object)

Page 3: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

UML class diagrams (1)

• UML = Unified Modeling Language• We use only class diagrams, not other UML

diagrams• Purpose:

– keep OO concepts separate from implementation language

– operate at a more abstract level than programming language, to avoid making implementation decisions when designing code

Page 4: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

UML class diagrams (2)

• Class box– Class name– Properties– Behaviors

• Only as much detail as is needed

• Relationships– inheritance– implements– composition– dependency– association

Page 5: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

UML class diagrams (3)

Page 6: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Where do objects come from?

• Objects are instances of classes• We instantiate classes:

– e.g. new chapter1.Terrarium()

– There are three parts to this expression:• new• chapter1.Terrarium• ()

Page 7: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

new chapter1.Terrarium()

‘new’ is a “reserved word” in Java. This means that the word ‘new’ has a special meaning in

the Java language.

‘new’ is the name of an operator whose job it is to create an instance of a given class

chapter1.Terrarium is the name of the class we are instantiating. It is a compound name, consisting of a package name (chapter1) and the name of the class’ constructor (Terrarium), separated by a dot

‘.’ A constructor initializes the state of a newly created object.

The parentheses delimit the argument list of the constructor call. In this case there are no

arguments being passed along to the constructor, so the argument list is empty.

Page 8: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Packages• A package is an organizational mechanism• One analogy:

– package::class– area code::phone number

• A class’ fully qualified name consists of its package name and its (unqualified) class name:– chapter1.Terrarium is a fully qualified class name– Terrarium is an unqualified class name

Page 9: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

On to Eclipse

package lecture;

class EcoSystem { public EcoSystem() { }}

Page 10: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

Package declaration is shown in green:

Page 11: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

package is a reserved word:

Page 12: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

lecture is the name of the package – you choose this (we’ll cover naming rules and conventions later):

Page 13: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

A semicolon ‘;’ marks the end of the declaration:

Page 14: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The class definition is shown in green:

Page 15: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The class definition consists of a header . . .

Page 16: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a body:

Page 17: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The class header consists of an access control modifier . . .

Page 18: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . the reserved word class . . .

Page 19: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a class name:

Page 20: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The class body begins with an opening brace ‘{’ . . .

Page 21: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and ends with the matching closing brace ‘}’ :

Page 22: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

In this example, the body consists of a single constructor definition:

Page 23: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The constructor definitons consists of a header . . .

Page 24: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a body:

Page 25: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The constructor header consists of an access control modifier . . .

Page 26: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . the constructor name (which is the same as the class name) . . .

Page 27: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a parameter list:

Page 28: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The constructor body begins with an opening brace ‘{’ . . .

Page 29: Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and ends with the matching closing brace ‘}’ :