Sei sulla pagina 1di 23

Computer Programming

IE (3799-017)
(4) Nov. 25, 2008

Lecturer
Ying CHEN
ying@sys.t.u-tokyo.ac.jp
Department of Systems Innovation

Teaching Assistant
Jeffrey TAN
jeffrey@robot.t.u-tokyo.ac.jp
Department of Precision Engineering
1 CP-IE (4), Nov. 25, 2008

1
Content of Java Programming

1. Getting Started (3) 11/18

2. Language Basics
3. Control Flow Statement
4. Basic of Object-Oriented Programming
(4) 11/25

Now, Please login to your Unix …

2 CP-IE (4), Nov. 25, 2008

2
4. Basic of Object-Oriented Programming (OOP)

Basic concepts

Procedural-Oriented programming:
algorithm + data structure → code
(functions)

Object-Oriented programming:
data structure + algorithm → code
(methods)

3 CP-IE (4), Nov. 25, 2008

3
4. Basic of Object-Oriented Programming (OOP)
Basic Concept

Object: basic unit of OOP

Instance
Class
Real
describes theWorld
Software Object
Object
generality of objects
State Behavior
↓ ↓
Variables Methods

4 CP-IE (4), Nov. 25, 2008

4
4. Basic of Object-Oriented Programming (OOP)

Bird Class
State → Color, Weight, Size ... → Variables
Behaviors → Fly( ), Walk( ), Eat( ) ... → Methods

Object
Bird A Bird B
Color = gray Color = yellow
Weight = 89g Weight = 61g
Fly( ) Eat( )

5 CP-IE (4), Nov. 25, 2008

5
4. Basic of Object-Oriented Programming (OOP)

Basic Concept

Message

6 CP-IE (4), Nov. 25, 2008

6
4. Basic of Object-Oriented Programming (OOP)

Basic Concept

Characteristics of
Object-Oriented programming

• Encapsulation
• Inheritance
• Polymophism

7 CP-IE (4), Nov. 25, 2008

7
4. Basic of Object-Oriented Programming (OOP)

Basic Structure main Class<name>


member variables
of program main method
local variables
method body
Same! other method<name>
local variables
method body
File.java other Class<name>
member variables
method<name>
local variables
Variable Scope method body

8 CP-IE (4), Nov. 25, 2008

8
4. Basic of OOP Method

• Specify the behavior of an object.


• A unit of codes that do something
you need.
• main is an special method, and you
can also define your own methods.
• Can be referred from anywhere
(or almost).

9 CP-IE (4), Nov. 25, 2008

9
4. Basic of OOP Method
Definition of a Method
methodDeclaration {
local variables
method body
}
public,

Declaration of a Method
accessLevel modifier returnType methodName
(type name [type name[…] ] ) { double,
…… static, int,
} … void,

10 CP-IE (4), Nov. 25, 2008

10
4. Basic of OOP Method

eg. 1
int summation(int a, int b ) {
return(a+b);
}

eg. 2
public static void main (String arg[ ] ) {
System.out.println(“My first java!");
}

11 CP-IE (4), Nov. 25, 2008

11
4. Basic of OOP Class

Structure of a Class
ClassDeclaration
member variables
Constructor
method1Declaration
local variables
method body
method2Declaration
local variables
method body

12 CP-IE (4), Nov. 25, 2008

12
4. Basic of OOP Class

Declaration of a Class

[public] [abstract/final] class ClassName[…] {


……
}

13 CP-IE (4), Nov. 25, 2008

13
Practice 5-1 Creating a class
/* Prac5-1, bird class, 2008. 11. 25. */
class Bird {
String color ;
int weight;
boolean hungry ;
Bird.java

public void figure( ) {


System.out.println("color :"+color);
System.out.println ("weight :"+weight);
}
public void meal ( ) {
if (hungry) {
System.out.println("looking for seeds to eat");
} else {
System.out.println ("finding toys to play");
}
}
}

14 CP-IE (4), Nov. 25, 2008

14
Practice 5-1 Creating a class

To complie the “Bird.java” → “Bird.class”,


and run,
→ To see what happens?

tying@as301> java Bird


Exception in thread "main"
java.lang.NoSuchMethodError: main

15 CP-IE (4), Nov. 25, 2008

15
4. Basic of OOP Object

The Life Cycle of an Object

Creating Using Cleaning Up

16 CP-IE (4), Nov. 25, 2008

A typical Java program creates many objects, which interact with one another
by sending each other messages. Through these object interactions, a Java
program can implement a GUI, run an animation, or send and receive
information over a network. Once an object has completed the work for which
it was created, it is garbage-collected and its resources are recycled for use by
other objects.
This section uses this example to describe the life cycle of an object within a
program. From this, you can learn how to write code that creates and uses an
object and how the system cleans it up.

16
4. Basic of OOP Object

Creating Objects from a class


•Declaration: define an object variable
className objectName;
•Instantiation: Creating a new object
objectName = new className ( );
OR
className objectName = new className ( );

eg. Bird BirdA= new Bird();

17 CP-IE (4), Nov. 25, 2008

17
4. Basic of OOP Object
Creating Objects from a class
•Initialization:
- objectName.variableName;
- objectName.methodName( );
objectName.methodName(arg);
- constructor

eg. Bird BirdA= new Bird();


BirdA.color = “gray”;
BirdA.weight = 89;
BirdA.meal(); BirdA.figure();
18 CP-IE (4), Nov. 25, 2008

Each statement has three parts:


1.Declaration: The code set in red in the previous listing are all variable
declarations that associate a name with a type. When you create an object,
you do not have to declare a variable to refer to it. However, a variable
declaration often appears on the same line as the code to create an object.
2.Instantiation: new is a Java operator that creates the new object (allocates
space for it).
3.Initialization: The new operator is followed by a call to a constructor. For
example, Point(23, 94) is a call to Point's only constructor. The constructor
initializes the new object.
The next three subsections discuss each of these actions in detail:

18
4. Basic of OOP Object

Using Objects

• Referencing an Object’s Variables


objectName.variableName

• Calling an Object’s Method


objectName.methodName( );
objectName.methodName(argumentList );

19 CP-IE (4), Nov. 25, 2008

19
4. Basic of OOP Object

Cleaning Up Unused Objects

• The garbage Collector

• Finalization

20 CP-IE (4), Nov. 25, 2008

Some object-oriented languages require that you keep track of all the objects
you create and that you explicitly destroy them when they are no longer needed.
Managing memory explicitly is tedious and error prone. The Java platform
allows you to create as many objects as you want (limited, of course, by what
your system can handle), and you don't have to worry about destroying them.
The Java runtime environment deletes objects when it determines that they are
no longer being used. This process is called garbage collection.
(An object is eligible for garbage collection when there are no more references
to that object. References that are held in a variable are usually dropped when
the variable goes out of scope. Or, you can explicitly drop an object reference
by setting the variable to the special value null. Remember that a program can
have multiple references to the same object; all references to an object must be
dropped before the object is eligible for garbage collection. )

The Java runtime environment has a garbage collector that periodically frees
the memory used by objects that are no longer referenced. The garbage
collector does its job automatically. in rare case, you might want to run the
finalize to release resource soon after a section of code that creates a large
amount of garbage or before a section of code that needs a lot of memory.
(finalize: before being collected, object has chance to clean itself by finalize)

20
Practice 5-2 Creating objects
/* Prac5-2, object/instance, 2008. 11. 25. */
public class Pract52 {
public static void main (String [ ] args ) {
Bird A = new Bird( );
pract52.java

Bird B = new Bird( );


A.color="gray" ; A.weight=10; A.hungry=true;
B.color ="white"; B.weight=24; B.hungry=false;
System.out.println("This is A bird");
A.figure( );
A.meal();
System.out.println("This is B bird");
B.figure( );
B.meal();
}
}

21 CP-IE (4), Nov. 25, 2008

21
Practice 5-2 (continue)
• Keep “Pract52.java” in a new directory:
To compile the “pract52.java
→ To see what happens?
tying@as301> javac pract52.java
Birdsample.java:3: cannot resolve symbol
symbol : class Bird
location: class Birdsample
Bird A; ….

• To confirm “Pract52.java” and Bird.class


are in same directory,
then run … →?
22 CP-IE (4), Nov. 25, 2008

22
Practice 5-3 Combining 2 classes

To write 2 classes into one program


Pract5.java
Compile, …
Run, …
This is A bird
color :gray
weight :89
looking for seeds to eat
This is B bird
color :yellow
weight :61
finding toys to play

23 CP-IE (4), Nov. 25, 2008

simply put 2 programs into one file.

23

Potrebbero piacerti anche