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

Post on 15-Jan-2016

218 views 0 download

transcript

Lecture #5Agenda

• 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)

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

UML class diagrams (2)

• Class box– Class name– Properties– Behaviors

• Only as much detail as is needed

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

UML class diagrams (3)

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• ()

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.

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

On to Eclipse

package lecture;

class EcoSystem { public EcoSystem() { }}

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

Package declaration is shown in green:

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

package is a reserved word:

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):

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The class definition is shown in green:

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The class definition consists of a header . . .

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a body:

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . the reserved word class . . .

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a class name:

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

The constructor definitons consists of a header . . .

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a body:

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

. . . and a parameter list:

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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

Syntax

package lecture;

public class EcoSystem {public EcoSystem() {}

}

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