22
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 [email protected] 1

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 [email protected] 1

Embed Size (px)

Citation preview

Page 1: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

1

CSE115: Introduction to Computer Science I

Dr. Carl Alphonce343 Davis Hall

[email protected]

Page 2: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Agenda• Announcements

– Cell phones / laptops off & away / Name signs out• Last time

– Composition relationship– Lifetime/Scope

• Today– Unified Modeling Language (UML)

• composition

– Naming conventions– Methods

Page 3: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

whole/part creation in code(elaborated)

• Whole creates instance of part in its constructor

• In Java code, involves 3 changes to whole class:– Declaration of instance variable of part type– Instantiation of part class in whole class

constructor– Assignment of new part instance to instance

variable

Page 4: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

4

Dog – Tail example in Java

package cse115;

public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}

Page 5: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

UML

Unified Modeling Language

(Class Diagrams)

Page 6: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

UML

• Unified Modeling Language– express design without reference to

an implementation language

• For example

Page 7: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Composition in UML

Page 8: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Binary Class Relationships:directional

• binary two classes are involved– source class has code modification– target class does not

• composition– source: WHOLE– target: PART

• in diagram:– line decoration is on source/WHOLE– show only detail that’s needed/desired

Page 9: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

package cse115;

public class Dog {

public Dog() {

}

}

package cse115;

public class Tail {

public Tail() {

}

}

Page 10: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

package cse115;

public class Dog {private Tail _tail;public Dog() {

_tail = new Tail();

}

}

package cse115;

public class Tail {

public Tail() {

}

}

Page 11: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Naming conventions

• packages: all lower case• classes:

– first character: upper case letter– camel case afterwards

• instance variables– first character: underscore ‘_’– camel case afterwards

• local variables– first character: lower case– camel case afterwards

Page 12: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

package lab2;

public class EcoSystem {private example1.Terrarium _t;

public EcoSystem() {_t = new example1.Terrarium();

}

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}}

Constructor definition

Method definition

Page 13: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

package lab2;

public class EcoSystem {private example1.Terrarium _t;

public EcoSystem() {_t = new example1.Terrarium();

}

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}}

The scope of an instance variable declaration is the entire class body.

The variable can (and should) be assigned an initial value (initialized)

in…

The variable can be used in any method of

the class.

Here’s a method definition.

Both the constructor and this method are in

the scope of the instance variable

declaration.

The instance variable can (and should) be assigned an initial

value (initialized) in… the constructor.

It can be used in any method in class body.

Page 14: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

method definition

Page 15: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

Remember that method definition consists of both a method header a method body.

Page 16: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

Here’s the method body.

Page 17: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

The method header begins with an access control modifier, public in this case.

Page 18: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

return type specification‘void’ is a special return type specification, indicating that no value is returned by the method

Page 19: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

Next comes the name of the method. Method names follow the same convention as local variables

Page 20: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

parameter list – empty in this exampleremember: argument list method call

parameter list method definition

Page 21: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

method body – delimited by braces

Page 22: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

Method definition syntax

public void addTwoCaterpillars() {example1.Caterpillar c1 = new

example1.Caterpillar();example1.Caterpillar c2 = new

example1.Caterpillar();_t.add(c1);_t.add(c2);c1.start();c2.start();

}

statements & (local variable) declarations – consists of local variable declarations and statements, which in this case are all method calls.