Professional Documents
Culture Documents
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
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 {
...
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
...
}
... Class body (may contain more elements)
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 ;
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 ) ;
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
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
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