You are on page 1of 32

Modern Programming Concepts in Engineering

objects in computer memory

Viewer Sphere Sphere Sphere

v oxygen hydrogen1 hydrogen2


variables in the main-method

Topic 2: Objects

Matthias Baitsch

Vietnamese-German Universit y
Example program: Visualize water molecule H2 O

r = 0.675

104.45°
Å
5 84
0.9

r=1

Using the water molecule example, we will introduce:


I The anatomy of a Java program
I Objects
Example program: Executing
WaterMoleculeProgram.java
import i n f . v3d . o b j . ∗ ;
import i n f . v3d . v i e w . ∗ ;

p u b l i c c l a s s WaterMoleculeProgram {

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
V i e w e r v = new V i e w e r ( ) ;
S p h e r e o x y g e n = new S p h e r e ( 0 , 0 , 0 ) ;
S p h e r e h y d r o g e n 1 = new S p h e r e ( 0 . 7 5 , 0 . 5 9 , 0 ) ;
S p h e r e h y d r o g e n 2 = new S p h e r e ( − 0 . 7 5 , 0 . 5 9 , 0 ) ;

oxygen . s e t C o l o r ( ” red ” ) ;
hydrogen1 . s e t Ra d iu s ( 0 . 6 7 5 ) ;
hydrogen2 . s e t Ra d iu s ( 0 . 6 7 5 ) ;

v . addObject3D ( o x y g e n ) ;
v . addObject3D ( h y d r o g e n 1 ) ;
v . addObject3D ( h y d r o g e n 2 ) ;

v . s e t V i s i b l e ( true ) ;
}
}
The class
Class Class declaration Class body
public c l a s s WaterMoleculeProgram {

...

Class consists of a class declaration and a class body


Class declaration gives after the keywords public class the
name of the class. In the example, the name is
“WaterMoleculeProgram”
Class body contains the elements of the class. In the example,
this is only one method

Classes are the building blocks of Java programs


Classnames and filenames

Classnames and filenames have to match:


A class WaterMoleculeProgram has to be in a file
WaterMoleculeProgram.java 1

Implications
I There can be only one class per file
I If you rename the class, you have to rename the file too

1
To be precise, this rule applies to public toplevel classes only
The main-method
Method Method declaration Method body

public static void main ( S t r i n g [ ] a r g s ) {

...

}
... Class body (may contain more elements)

I Every method consists of a method declaration (also called


the signature) and the method body
I If a method has a signature exactly as in the example, it is
called main-method
I The main-method is the entry point to your program

A main-method is required to run a program


Creating objects – the new operator
Variable type Parameter list
Variable name Object creation
V i e w e r v = new V i e w e r ( ) ;
S p h e r e o x y g e n = new S p h e r e ( 0 , 0 , 0 ) ;
S p h e r e h y d r o g e n 1 = new S p h e r e ( 0 . 7 5 , 0 . 5 9 , 0 ) ;
S p h e r e h y d r o g e n 2 = new S p h e r e ( − 0 . 7 5 , 0 . 5 9 , 0 ) ;

I Variables are symbolic names for a chunks of memory


I Java is a strongly typed language: each variable has a type
I Here, the types are classes named Viewer and Sphere
I Objects are created using new followed by the class name
I Sometimes, parameters are required (here: coordinates)
I The result of creating an object is assigned to a variable
I In Java, each instruction is terminated by a semicolon
Import packages

import i n f . v3d . o b j . S p h e r e ;
import i n f . v3d . v i e w . V i e w e r ;

I In Java, classes are organized in packages in order to avoid


naming conflicts
I If you want to use classes from packages other than
java.lang, you have to import them
I Import statements appear before the class
Variables and memory
The viewer object takes several bytes (the Variable v denotes some bytes (the green
violet ones) in RAM ones) in RAM (8 on a 64bit system)
The address of the first byte is for example The content of variable v decodes to the
b32e13d (usually, hex-encoding is used) address b32e13d
first byte of object at address b32e13d one byte
... 0 1 0 1 0 1 1 0 0 0 0 1 1 0 ... 0 1 0 0 1 1 1 1 0 1 0 ... 0 1 1 1 0 1 0 1 0 0 1 1 0 0 0 0 ... 0 0 0 0 1 0 0 1 ...

the viewer object variable v = b32e13d

I The memory of a computer (random access memory (RAM))


can be imagined as a very long strip of boxes where each box
contains either 0 or 1. One such box is called a bit
I 8 bits make a byte. A byte is the smallest addressable unit
I Each byte has an address in RAM. An address is a number
I Creating an object via new reserves some bytes for the object
and returns the address of the object’s first byte
I After assigning the result of new to a variable, the variable
contains the address of the object. Variables containing
addresses are called pointers
Object-type variables are pointers to objects
objects in computer memory

@b32e13d @59f4c9e @e890acc @ffacbbe


Viewer Sphere Sphere Sphere

v = b32e13d oxygen = 59f4c9e hydrogen1 = e890acc hydrogen2 = ffacbbe


variables in the main-method

I Variables referring to objects are called object-type variables


(later you will hear also primitive-type variables)
I When invoking the new operator, a new object is created in
computer memory
I Assigning the result creating an object to a variable lets the
variable point to the object just created
Object-type variables are pointers to objects
objects in computer memory

Viewer Sphere Sphere Sphere

v oxygen hydrogen1 hydrogen2


variables in the main-method

I Variables referring to objects are called object-type variables


(later you will hear also primitive-type variables)
I When invoking the new operator, a new object is created in
computer memory
I Assigning the result creating an object to a variable lets the
variable point to the object just created
→ Addresses will be omitted
Invoking methods – the ”.”-operator
Variable Method name Parameter

oxygen . s e t C o l o r ( ” red ” ) ;
hydrogen1 . setRadius ( 0 . 6 7 5 ) ;
hydrogen2 . setRadius ( 0 . 6 7 5 ) ;
v . s e t V i s i b l e ( true ) ;

I Using methods, we let objects perform actions. Examples: Set


color, change radius, show up on screen
I Parameters can be passed inside the parenthesis
I The method is executed by the object the variable points to.
E.g., the first line reads: Let the object, oxygen points to,
execute the setColor method using "red" as parameter
I If a method needs more than one parameters, parameters are
separated by commas (see object creation)
What is the meaning of the assignment operator =?
public c l a s s AssignmentOperatorProgram {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Sphere s ;
V i e w e r v = new V i e w e r ( ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =0, y =0 , y=0
S p h e r e s 1 = new S p h e r e ( 0 , 0 , 0 ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =2, y =0 , y=0
S p h e r e s 2 = new S p h e r e ( 2 , 0 , 0 ) ;

v . addObject3D ( s 1 ) ;
v . addObject3D ( s 2 ) ;

s = s2 ;
s . setColor (” red ” ) ;
s2 = s1 ;
s2 . s e t R a d i u s ( 0 . 5 ) ;
s = s2 ;
s . setColor (” blue ” ) ;

v . s e t V i s i b l e ( true ) ;
}
}
What will we see?

A B

C D
public c l a s s AssignmentOperatorProgram {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Sphere s ;
V i e w e r v = new V i e w e r ( ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =0 , y =0 , y=0
S p h e r e s 1 = new S p h e r e ( 0 , 0 , 0 ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =2 , y =0 , y=0
S p h e r e s 2 = new S p h e r e ( 2 , 0 , 0 ) ;

v . addObject3D ( s 1 ) ;
v . addObject3D ( s 2 ) ;

s = s2 ;
s . setColor ( ” red ” ) ;
s2 = s1 ;
s2 . s e t R a d i u s ( 0 . 5 ) ;
s = s2 ;
s . setColor (” blue ” ) ;

v . s e t V i s i b l e ( true ) ;
}
}
public c l a s s AssignmentOperatorProgram {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Sphere s ;
V i e w e r v = new V i e w e r ( ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =0 , y =0 , y=0
S p h e r e s 1 = new S p h e r e ( 0 , 0 , 0 ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =2 , y =0 , y=0
S p h e r e s 2 = new S p h e r e ( 2 , 0 , 0 ) ;

v . addObject3D ( s 1 ) ;
v . addObject3D ( s 2 ) ;

s = s2 ;
s . setColor ( ” red ” ) ;
s2 = s1 ;
s2 . s e t R a d i u s ( 0 . 5 ) ;
s = s2 ;
s . setColor (” blue ” ) ;

v . s e t V i s i b l e ( true ) ;
}
}
public c l a s s AssignmentOperatorProgram {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Sphere s ;
V i e w e r v = new V i e w e r ( ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =0 , y =0 , y=0
S p h e r e s 1 = new S p h e r e ( 0 , 0 , 0 ) ;
// c r e a t e s p h e r e o f r a d i u s r =1 a t x =2 , y =0 , y=0
S p h e r e s 2 = new S p h e r e ( 2 , 0 , 0 ) ;

v . addObject3D ( s 1 ) ;
v . addObject3D ( s 2 ) ;

s = s2 ;
s . setColor ( ” red ” ) ;
s2 = s1 ;
s2 . s e t R a d i u s ( 0 . 5 ) ;
s = s2 ;
s . setColor (” blue ” ) ;

v . s e t V i s i b l e ( true ) ;
}
}
Assigning object-type variables redirects pointers
Objects and classes. . .
Object-oriented programs

Monitor Window Program


FileWriter
Keyboard/Mouse Table
Persistent storage
FileReader (HDD)
Network device Socket

Sensor Logger Connector


object

An executing object-oriented program is an immaterial machine


which consists of parts, which themselves are smaller immaterial
machines. These smaller immaterial machines are called objects.
During program execution
I there can be a large number of objects (millions)
I the number of objects may change
I objects can invoke methods on each other
What is an object?
In general, we deal with three kinds of objects:
Physical objects reflect material objects of the modeled world or
system. Examples: a wall, a slab, . . .
Abstract objects describe abstract notions from the modeled world
or system. Examples: a domain, a displacement
function . . .
Software objects represent pure software notions. A software
object is an immaterial machine allowing programs to
access and modify a collection of data. Often,
software objects are related to physical or abstract
objects
→ From now on, object means software object
A key attraction of object-oriented software development is its
modeling power: Connect software objects to objects of the
problem domain.
Objects are immaterial machines
operations

state operations
An object is characterized by
a set of methods which can be invoked for this object in order to
carry operations. In the example, we changed color
and radius of spheres or let a Viewer object show up
on the screen
attributes defining the state of the object. In the example,
spheres have a radius and a color
Objects have an implementation

I The implementation defines how objects behave


Objects have an implementation

I The implementation defines how objects behave


I Information hiding: The implementation is not visible to
clients of objects
Objects have an implementation

I The implementation defines how objects behave


I Information hiding: The implementation is not visible to
clients of objects

The specification of the implementation is in the class, the specific


object belongs to.
What is a class?

Each object belongs to a certain class

A class is a collection of program code (Java) which


I defines the set of operations which can be carried out by an
object
I specifies what these operations do2
I defines the attributes which are used to store the state of an
object

2
Operations might do something simple like changing the color or something
complicated like an algorithm to solve a linear system
Documentation for existing classes: Javadoc

Javadoc documentation
I comes with any (decent) library
I documents all methods of all classes in the library
I is automatically generated from Java source code
Classes vs. objects

Classes exist only in the software text:


I Defined by class text in the Java programming language
I Describe properties of associated objects. Such objects are
also called instances of a class

Objects exist only during program execution:


I Visible in program text through variables pointing to run-time
objects
Two views on a program

Static – The program on the hard disk drive


I Java code as provided by the programmer(s)
I Classes
I Does not change

Dynamic – The executing program


I Depends on the actual input to the program
I Objects
I Changes over the lifetime of the program
Summary
I An executing object-oriented program consists of many
collaborating objects
I Objects can be viewed as immaterial machines
I Objects are created using new
I Methods are invoked on objects using the ”.”-operator
I Properties and applicable operations of objects are defined in
classes
I Classes are static and exist only in software text
I Objects are dynamic and exist during program execution

You might also like