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

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE116: Introduction to Computer Science 2 Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
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:
Fall 2005CSE 115/503 Introduction to Computer Science I1 Lecture #4 Agenda Announcements Review Questions? Classes and objects UML class diagrams Creating.
Java Syntax, Java Conventions, CSE 115 Conventions (Part 2) CSE 115 Spring 2006 January 30, February 1 & 3, 2006.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2005CSE 115/503 Introduction to Computer Science I1 Association Also called “knows a”. A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail)
Week 5 Recap CSE 115 Spring Composition Informally called “has a” Represented in UML with a diamond- headed arc In code: Declare an instance variable.
CSE 115 Week 3 January 28 – February 1, Monday Announcements Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Software Installation.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 End of Week Five! Questions? First part of essay assignment (choosing a topic) is posted.
Week 3 Recap CSE 115 – Spring Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration 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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Thanks, Andrew. Reminders: –wear the wireless mike –combination is 1-31 –speak slowly, pause for questions –I’d rather you get through less material than.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object Oriented Programming in java
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall

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

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

4 Dog – Tail example in Java package cse115; public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }

UML Unified Modeling Language (Class Diagrams)

UML Unified Modeling Language –express design without reference to an implementation language For example

Composition in UML

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

package cse115; public class Dog { public Dog() { } package cse115; public class Tail { public Tail() { }

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

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

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

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.

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

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.

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.

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.

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

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

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 example remember:argument list  method call parameter list  method definition

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

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.