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

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

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

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

1

CSE115: Introduction to Computer Science I

Dr. Carl Alphonce219 Bell Hall

[email protected]

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

Announcements

• No classes on Monday before 6:00 PM

• No office hours on Monday• New lab starting next week• Exam grading should be finished

tonight

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

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

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

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

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

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.

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

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)

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

• 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

Page 8: CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 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

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

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.access

ibility

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

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.

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

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:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

Package declaration is shown in green:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

package is a reserved word:

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

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

The class definition is shown in green:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

The class definition consists of a header . . .

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

. . . and a body:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

. . . the reserved word class . . .

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

. . . and a class name:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

The constructor definitions consists of a header . . .

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

. . . and a body:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

. . . and a parameter list:

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

Syntax

package lab2;

public class EcoSystem {public EcoSystem() {}

}

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

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

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.

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

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.

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

Variables’ properties

• name• location• type• value (contents)• scope• lifetime

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

Variable scope

• The scope of a variable is the part of a program where a variable declaration is in effect.

• Variables declared in different ways have different scope.

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

Local variables

• A variable declared within a constructor (method) is called a local variable.

• The scope of a local variable is from the point of the declaration to the end of the method body.

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

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:

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

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

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

• Every time class is instantiated, the constructor is executed.

• This creates a new Terrarium with a new moving Caterpillar.