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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
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
Introduction to C Programming
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 NEW   NEW 1.
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
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
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
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
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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:
Lecture #4 Agenda Cell phones off & name signs out Review Questions? Objects The birds-and-the-bees talk.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Lecture #4 Agenda Announcements Review Questions? Classes and objects UML class diagrams Creating.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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.
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:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE 116 Introduction to Computer Science For Majors II Carl Alphonce 219 Bell Hall.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Object Oriented Software Development
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall NEW   NEW 1.
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Introduction to Object-Oriented Programming Lesson 2.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Classes, Interfaces and Packages
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Chapter 3: Using Methods, Classes, and Objects
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 219 Bell Hall

Agenda Announcements – Cell phones / laptops off & away –Name signs out FAIR WARNING: First exam on Wednesday, Sept 28 Last time –expressions –references –variables –terminology Today –Object diagrams –More terminology –Class definitions

A bit of review example1.Terrarium t; example1.Caterpillar c; t = new example1.Terrarium(); c = new example1.Caterpillar(); t.add(c); c.start();

The variable declaration Consists minimally of: type & name What is a type? A class is a type. –Remember: objects are instantiated from classes. Examples of variable declarations: example1.Terrariumt; example1.Caterpillar c;

Syntax (a little more formal) variable declarations appear in many places in Java programs a variable declaration in Java –ALWAYS has: type identifer In our example –type identifier ; –semicolon is a terminator

assignment statement variable = expression ; ‘=’ is the ASSIGNMENT OPERATOR Example: t = new example1.Terrarium();

A bit of review example1.Terrarium t; example1.Caterpillar c; t = new example1.Terrarium(); c = new example1.Caterpillar(); t.add(c); c.start(); What happens in memory?

available 112 available 108 available used used Terrarium object Caterpillar object available Something like this!

Object diagram (corresponding to memory diagram on previous slide) example1.Terrarium example1.Caterpillar t c Boxes denote variables Ovals denote objects Arrows denote references This diagram is an abstraction of the one on the previous slide: it ignores irrelevant details, such as the addresses and sizes of the two objects being shown. An abstraction is thus a simplification. Simpler!

What is a class definition? A class definition is a description of the properties and behaviors that instances of the class will have. Recall that we said a running OO program is a system of interacting objects. Possible relationships between objects are determined by relationships between classes. (Important – we’ll return to this!)

Where do objects come from? Objects are instances of classes We instantiate classes: –e.g.new example1.Terrarium() –There are three parts to this expression: new example1.Terrarium ()

new example1.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 example1.Terrarium is the name of the class we are instantiating. It is a compound name, consisting of a package name (example1) 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. example1.Terrarium() is a constructor call. A constructor initializes the state of a newly created object.

Object creation (revisited & refined) new determines size of object reserves a block of memory for object calls constructor to initialize the object –we will consider a constructor to be a special case of a method returns the starting address of block of memory (a reference to the object)

A package is an organizational mechanism –related classes are grouped together allows class names to be re-used in different packages reduces chance of naming conflicts Packages – 1

Packages – 2 One analogy: –package::class –area code::phone number A class’ fully qualified name consists of its package name and its (unqualified) class name: –example1.Terrarium is a fully qualified class name –Terrarium is an unqualified class name

Folder structure on disk each package corresponds to a folder on the disk packages can be nested within each other –corresponds to nested folder on disk examples: – java.rmi.registry – javax.swing.text.html.parser – com.sun.accessibility.internal.resources.accessibility

Defining a class Let us define a class which, when instantiated, creates a Terrarium, adds a Caterpillar, and makes the Caterpillar move. This is similar to what you will do for lab 2. We start with a minimal class definition.

Our first class definition! package lab2; public class EcoSystem { public EcoSystem() { } Here’s a minimal class definition. We will label and discuss each part of it:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } Package declaration is shown in green:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } package is a reserved word:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } lab2 is the name of the package – you choose this (we’ll cover naming rules and conventions later):

Syntax package lab2; public class EcoSystem { public EcoSystem() { } A semicolon ‘;’ marks the end of the declaration:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The class definition is shown in green:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The class definition consists of a header...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... and a body:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The class header consists of an access control modifier...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... the reserved word class...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... and a class name:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The class body begins with an opening brace ‘{’...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... and ends with the matching closing brace ‘}’ :

Syntax package lab2; public class EcoSystem { public EcoSystem() { } In this example, the body consists of a single constructor definition:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The constructor definitions consists of a header...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... and a body:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The constructor header consists of an access control modifier...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... the constructor name (which is the same as the class name)...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... and a parameter list:

Syntax package lab2; public class EcoSystem { public EcoSystem() { } The constructor body begins with an opening brace ‘{’...

Syntax package lab2; public class EcoSystem { public EcoSystem() { }... and ends with the matching closing brace ‘}’ :

Let’s return to: defining a class We set out to define a class which, when instantiated, creates a Terrarium, adds a Caterpillar, and makes the Caterpillar move. We started with a minimal class definition, let’s move beyond that now.

Variable declaration package lab2; public class EcoSystem { public EcoSystem() { example1.Terrarium t; } A variable can be declared inside the body of a method – it is called a local variable.

Assignment statement package lab2; public class EcoSystem { public EcoSystem() { example1.Terrarium t; t = new example1.Terrarium(); } Any statement must be inside the body of a method:

package lab2; public class EcoSystem { public EcoSystem() { example1.Terrarium t; t = new example1.Terrarium(); example1.Caterpillar c; c = new example1.Caterpillar(); t.add(c); c.start(); } The complete solution

Every time class is instantiated, the constructor is executed. This creates a new Terrarium with a new moving Caterpillar.