Modern Programming Concepts in Engineering

Modern Programming Concepts in Engineering
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
+ getArea(): double
+ isInside(px: double, py: double): boolean
+ isIntersecting(r: Rectangle): boolean
+ createUnion(r: Rectangle): Rectangle
Topic 3: Classes
Matthias Baitsch
Vietnamese-German Universit y
Summary of last lecture
I
A class defines a set of objects with common structure,
common behavior, common relationships and common
semantics
I
Objects are created during program execution
I
In the program code, objects are accessed by variables
Creating new classes. . .
Steps in programming a new class
Implementing new classes usually includes
1. a thorough analysis of the problem at hand. This comprises
I
I
deciding about suitable class names
getting clear about the purpose of respective objects
2. specifying the operations applicable to objects belonging to
the class. Result: List of methods including method names,
parameters and return types
3. identifying the data needed to store the state of the object.
Result: List of attributes including names and types
4. Collect results of steps 2 and 3 in a graphical description of
your class (kind of a blueprint)
5. Implement your class or classes in Java
This outline is an attempt to extract essential steps from a process
which in practice often is of iterative nature.
Example: A class for rectangles
point outside
h
point inside
(x, y)
y
x
intersection
w
Remarks
I
I
The class is for demonstration purposes only
Many new concepts will be introduced:
I
I
I
I
I
UML class diagrams
data types
methods, the call stack and method parameters
logical expressions
The class will be developed step by step
Problem analysis: The class name
point outside
h
point inside
(x, y)
y
x
intersection
w
Deciding about the class name
Often, class names can be identified by asking a simple question:
What does an object belonging to the class represent?
Answer:
Problem analysis: The class name
point outside
h
point inside
(x, y)
y
x
intersection
w
Deciding about the class name
Often, class names can be identified by asking a simple question:
What does an object belonging to the class represent?
Answer: One rectangle
Problem analysis: The class name
point outside
h
point inside
(x, y)
y
x
intersection
w
Deciding about the class name
Often, class names can be identified by asking a simple question:
What does an object belonging to the class represent?
Answer: One rectangle
However, rectangle is not a good class name with respect to Java
naming conventions
Java naming conventions
What are naming conventions?
I
Rules for choosing of names of classes, methods and variables
Why do we need such conventions
I
Because they simplifies programmer’s life by making
I
I
it easier to decide about names
program code is more readable
More information under http://java.sun.com/docs/codeconv
Choosing good class names
Cited from the Java web pages:
Class names should be nouns, in mixed case with the first letter of
each internal word capitalized. Try to keep your class names simple
and descriptive. Use whole words-avoid acronyms and
abbreviations (unless the abbreviation is much more widely used
than the long form, such as URL or HTML).
Examples for good and bad class names
English
rectangle
cross section
HTML Document
good name
Rectangle
CrossSection
HTMLDocument
bad names
rectangle, RECTANGLE, Rect
CROSS SECT, CrossSect
HTMLDoc, HTML DOCUMENT
Singular or plural?
Class names are (almost) never in plural
Although our class represents all kinds of rectangles, one object
belonging to the class represents one rectangle. Thus, the class
name is singular.
Problem analysis: The class name
The class name is
Rectangle
Problem analysis: The purpose of rectangle objects
point outside
h
point inside
(x, y)
y
x
We want to be able to
intersection
w
Problem analysis: The purpose of rectangle objects
point outside
h
point inside
(x, y)
y
x
intersection
w
We want to be able to
I
draw a rectangle on the screen
I
get the area of a rectangle
I
test if a point is inside a rectangle
I
test if a rectangle intersects another rectangle
I
create a new rectangle as intersection of a rectangle with
another rectangle
The Unified Modeling Language (UML)
What is the Unified Modeling Language?
I
I
A graphical language used to express software system designs
A set of 14 diagram types in two categories:
1. diagrams representing structural information
2. diagrams representing general types of behaviour
Why using the Unified Modeling Language?
I
I
Because complex software systems need thorough software
design (just as drawing are needed on a construction site)
During the design process, software architects have to
I
I
I
think about the software design
communicate with others about the software design
For these purposes, a language is required which is
I
I
more specific that natural languages such as English
less detailed than actual program code
UML class diagrams
Class Name
Attribute 1
Attribute 2
...
Method 1
Method 2
...
UML class diagrams
I
contain one or many classes. Each class is represented by a
box with three compartments. The first one for the class
name, the second one for the attributes and the third one for
the methods
I
are the most popular diagrams in the UML. We use mostly
class diagrams
Step 1: Creating rectangles
Creating rectangles
r2
y
r1
r3
x
What do we need to create the above rectangles?
Creating rectangles
r2
y
r1
r3
x
What do we need to create the above rectangles?
I
When creating a new rectangle, position and size have to be
specified
I
We must be able to store position and size of the new
rectangle
Properties of objects, attributes and the constructor
Properties of objects
I
One rectangle object represents one rectangle
I
For each rectangle, position and size have to be stored
I
Properties of objects are stored in attributes
Properties of objects, attributes and the constructor
Properties of objects
I
One rectangle object represents one rectangle
I
For each rectangle, position and size have to be stored
I
Properties of objects are stored in attributes
Attributes
I
Attributes are variables declared in the scope of a class
I
The variables can have any type
I
Attribute types have to be chosen carefully
I
When an object is created, values for attributes are specified
Properties of objects, attributes and the constructor
Properties of objects
I
One rectangle object represents one rectangle
I
For each rectangle, position and size have to be stored
I
Properties of objects are stored in attributes
Attributes
I
Attributes are variables declared in the scope of a class
I
The variables can have any type
I
Attribute types have to be chosen carefully
I
When an object is created, values for attributes are specified
The constructor
I
Objects are created by invoking the constructor
I
Usually, attributes are initialized inside the constructor
Creating rectangles: Test case
public c l a s s RectangleProgram {
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 ) {
R e c t a n g l e r 1 = new R e c t a n g l e ( 1 . 0 , 1 . 0 , 4 . 0 , 4 . 0 ) ;
R e c t a n g l e r 2 = new R e c t a n g l e ( 2 . 0 , 4 . 0 , 8 . 0 , 2 . 0 ) ;
R e c t a n g l e r 3 = new R e c t a n g l e ( 6 . 0 , 2 . 0 , 2 . 0 , 5 . 0 ) ;
}
}
I
Create three rectangle objects
I
Size and position of objects are specified as parameters to the
constructor
Creating rectangles: Objects
objects in computer memory
Rectangle
x = 1.0
y = 1.0
w = 4.0
h = 4.0
Rectangle
x = 2.0
y = 4.0
w = 8.0
h = 2.0
Rectangle
x = 6.0
y = 2.0
w = 2.0
h = 5.0
r1
r2
r3
variables in the main-method
I
After creation, objects with properties reside in memory
I
Each object has its own set of attributes
Object-type and primitive type variables
In the previous lecture, we used object-type variables. The types of
the variables have been classes such as Sphere or Viewer.
Object-type and primitive type variables
In the previous lecture, we used object-type variables. The types of
the variables have been classes such as Sphere or Viewer.
Rectangles’s position and size are floating point numbers. The
classes used so far are not suitable.
Object-type and primitive type variables
In the previous lecture, we used object-type variables. The types of
the variables have been classes such as Sphere or Viewer.
Rectangles’s position and size are floating point numbers. The
classes used so far are not suitable.
In addition to classes, Java defines 9 so called primitive types:
Variables of primitive type are used to store “simple” data like
numbers, boolean values or single characters.
Java primitive types
Type
byte
short
int
long
float
double
boolean
char
void1
Contains
integer
integer
integer
integer
floating-point
floating-point
boolean value
unicode character
nothing
Size
8 bits
16 bits
32 bits
64 bits
32 bits
64 bits
1 bit
16 bits
–
Range
-128 . . . 127
-32768 . . . 32767
-2147483648 . . . 2147483647
−263 . . . 263 − 1
ca. ±3.40282347 · 1038
ca. ±1.79769313486231570 · 10308
true or false
ISO Unicode character set
–
Other than object-type variables, primitve type variables
I
store “simple” content like numbers or single characters
I
directly refer to the variable’s content
I
do not require the new-operator
I
do not provide any operations
1
void can be used solely as method return type
Creating rectangles: UML diagram
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
...
I
Four attributes x, y, w, h of type double are used to store
position and size
I
The constructor has 4 corresponding parameters
I
Constructors always have the same name as the class and no
return type
Creating rectangles: UML diagram – annotated
Visibility modifier:
"-" indicates that access
is allowed only from
within the class
Visibility modifier:
"+" indicates that access is
allowed from everywhere
Rectangle
- x: double
- y: double
Attribute in the form
name: type
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
...
Parameter list: Parameters
are separated by commas
Parameter in the form
name: type
No return type
for constructor
I
Four attributes x, y, w, h of type double are used to store
position and size
I
The constructor has 4 corresponding parameters
I
Constructors always have the same name as the class and no
return type
Creating rectangles: Java code
public class Rectangle {
private
private
private
private
double
double
double
double
x;
y;
w;
h;
p u b l i c R e c t a n g l e ( double x , double y ,
do u b l e w , double h ) {
this . x = x ;
this . y = y ;
t h i s .w = w;
this .h = h;
}
}
I
Attributes are variables declared in the class body
I
The values of the attributes are initialized in the constructor
What is this?
The key-concept for the understanding of
object-oriented programming!
The this pointer
Rectangle
x = 1.0
y = 1.0
w = 4.0
h = 4.0
inside: Rectangle(double x, double y, ...)
this
inside main at: r1 = new Rectangle(1.0, 1.0, 4.0, 4.0)
x←1, y←1, w←4, h←4
I
Any “normal” operation is carried out by an object
I
this points to the object which carries out the operation
I
When execution jumps into a method, parameters are assigned
I
Therefore, this.x refers to the object and x to the parameter
Step 2: Drawing rectangles
Drawing rectangles: Method specification
What do we need to draw the rectangles?
Drawing rectangles: Method specification
What do we need to draw the rectangles?
I
The rectangle (this)
I
The color for the rectangle
I
The viewer to add the rectangle to
Drawing rectangles: Method specification
What do we need to draw the rectangles?
I
The rectangle (this)
I
The color for the rectangle
I
The viewer to add the rectangle to
What is the type of the result?
Drawing rectangles: Method specification
What do we need to draw the rectangles?
I
The rectangle (this)
I
The color for the rectangle
I
The viewer to add the rectangle to
What is the type of the result?
I
The method does not return anything of further use
Drawing rectangles: UML class diagram
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
...
I
The draw method takes the color as parameter. The type of
the color parameter is String (contained in the
comprehensive Java runtime library)
I
The method does not return anything and thus, the return
type is void.
Drawing rectangles: UML class diagram – annotated
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
...
Method name
Parameter in the form
name: type
Method return type. This method
does not return anything
I
The draw method takes the color as parameter. The type of
the color parameter is String (contained in the
comprehensive Java runtime library)
I
The method does not return anything and thus, the return
type is void.
Choosing method names
Cited from the Java web pages:
Methods should be verbs, in mixed case with the first letter
lowercase, with the first letter of each internal word capitalized.
Examples for good and bad method names
good name
run
getArea
computeDeadWeight
bad names
Run, RUN
get area, getarea
cmptDeadWght, compute dead weight
Drawing rectangles: Test case
import i n f . v3d . v i e w . V i e w e r ;
public c l a s s RectangleProgram {
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 i e w e r = new V i e w e r ( ) ;
R e c t a n g l e r 1 = new R e c t a n g l e ( 1 . 0 , 1 . 0 , 4 . 0 , 4 . 0 ) ;
R e c t a n g l e r 2 = new R e c t a n g l e ( 2 . 0 , 4 . 0 , 8 . 0 , 2 . 0 ) ;
R e c t a n g l e r 3 = new R e c t a n g l e ( 6 . 0 , 2 . 0 , 2 . 0 , 5 . 0 ) ;
r 1 . draw ( ” r e d ” , v i e w e r ) ;
r 2 . draw ( ” g r e e n ” , v i e w e r ) ;
r 3 . draw ( ” b l u e ” , v i e w e r ) ;
viewer . s e t V i s i b l e ( true ) ;
}
}
I
Invoke draw method with different colors
Visualizing polygons
A rectangle is a (very) special case of an unstructured mesh
I
We can use the UnstructuredMesh class from the view3D
package
I
Examine Javadoc of view3D package
Drawing rectangles: Java code
p u b l i c v o i d draw ( S t r i n g c o l o r , V i e w e r v ) {
U n s t r u c t u r e d M e s h um = new U n s t r u c t u r e d M e s h ( ) ;
um . a d d P o i n t ( t h i s . x , t h i s . y , 0 ) ;
um . a d d P o i n t ( t h i s . x + t h i s . w , t h i s . y , 0 ) ;
um . a d d P o i n t ( t h i s . x + t h i s . w , t h i s . y + t h i s . h , 0 ) ;
um . a d d P o i n t ( t h i s . x , t h i s . y + t h i s . h , 0 ) ;
um . a d d C e l l ( 0 , 1 , 2 , 3 ) ;
um . s e t C o l o r ( c o l o r ) ;
um . s e t O u t l i n e s V i s i b l e ( t r u e ) ;
v . addObject3D (um ) ;
}
Drawing rectangles: Executing the draw method
Rectangle
x = 1.0
y = 1.0
w = 4.0
h = 4.0
inside: draw(String color, Viewer v)
this
Viewer
v
this←r1, color←"red", v ← viewer
inside main at: r1.draw("red", viewer);
I
I
r1
viewer
The draw method is invoked for the object r1 points to
When jumping into the draw method
I
I
I
this is redirected to the object r1 points to
the parameter color is assigned the value ”red”
the parameter v is directed to the viewer object
Visibility
Besides public and private, Java defines two more levels of
visibility: protected and package (package is the default which
applies of no visibility modifier is specified).
In this course we use two simple rules for visibility:
I
Methods (including constructors) are public
I
Attributes are private
The Java runtime library
The Java Platform provides a comprehensive set of its own
standard class libraries containing more than 1500 classes including
I containers – store objects efficiently
I networking – access other computers
I graphical user interfaces – implement interactive programs
I database access – use database servers
I IO – read and write to hard disk and other devices
→ Using the documentation is indispensable (Shift+F2 in Eclipse)
Step 3: Get a rectangle’s area
Get a rectangle’s area: Method specification
What do we need to compute the area of a rectangle?
Get a rectangle’s area: Method specification
What do we need to compute the area of a rectangle?
I
The rectangle (this)
I
Nothing besides the rectangle itself
Get a rectangle’s area: Method specification
What do we need to compute the area of a rectangle?
I
The rectangle (this)
I
Nothing besides the rectangle itself
What is the type of the result?
Get a rectangle’s area: Method specification
What do we need to compute the area of a rectangle?
I
The rectangle (this)
I
Nothing besides the rectangle itself
What is the type of the result?
I
The result is a floating point number
Get area method: UML diagram
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
+ getArea(): double
...
I
The method does not need any input and therefore, the
parameter list is empty
I
Because the computed area is a floating point number, the
method returns a double
Get area method: UML diagram – annotated
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
+ getArea(): double
...
Method name
Empty
parameter list
Method return type: This method
returns a double
I
The method does not need any input and therefore, the
parameter list is empty
I
Because the computed area is a floating point number, the
method returns a double
Get area method: Test case
public c l a s s RectangleProgram {
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 ) {
R e c t a n g l e r 1 = new R e c t a n g l e ( 1 . 0 , 1 . 0 , 4 . 0 , 4 . 0 ) ;
R e c t a n g l e r 2 = new R e c t a n g l e ( 2 . 0 , 4 . 0 , 8 . 0 , 2 . 0 ) ;
R e c t a n g l e r 3 = new R e c t a n g l e ( 6 . 0 , 2 . 0 , 2 . 0 , 5 . 0 ) ;
dou b l e a1 = r 1 . g e t A r e a ( ) ;
dou b l e a2 = r 2 . g e t A r e a ( ) ;
dou b l e a3 = r 3 . g e t A r e a ( ) ;
System . o u t . p r i n t l n ( ” Area o f r e c t a n g l e 1 : ” + a1 ) ;
System . o u t . p r i n t l n ( ” Area o f r e c t a n g l e 2 : ” + a2 ) ;
System . o u t . p r i n t l n ( ” Area o f r e c t a n g l e 3 : ” + a3 ) ;
}
}
I
Local variables a1, a2, a3 of type double store results
I
System.out.println(...) is used to print on the console
Get area method: Java code
Version 1
p u b l i c double getArea ( ) {
dou b l e a = t h i s . w ∗ t h i s . h ;
return a ;
}
I
The area is stored in a local variable
I
At the end, the computed value is returned
Get area method: Java code
Version 1
p u b l i c double getArea ( ) {
dou b l e a = t h i s . w ∗ t h i s . h ;
return a ;
}
I
The area is stored in a local variable
I
At the end, the computed value is returned
Version 2
p u b l i c double getArea ( ) {
return t h i s .w ∗ t h i s . h ;
}
I
The area is returned directly
Step 4: Test if a point is inside
Test if a point is inside: Method specification
h
point inside
point outside
(x, y)
y
x
w
What do we need to test if a point is inside a rectangle?
Test if a point is inside: Method specification
h
point inside
point outside
(x, y)
y
x
w
What do we need to test if a point is inside a rectangle?
I
The rectangle (this)
I
The coordinates of the point
Test if a point is inside: Method specification
h
point inside
point outside
(x, y)
y
x
w
What do we need to test if a point is inside a rectangle?
I
The rectangle (this)
I
The coordinates of the point
What are the possible answers?
Test if a point is inside: Method specification
h
point inside
point outside
(x, y)
y
x
w
What do we need to test if a point is inside a rectangle?
I
The rectangle (this)
I
The coordinates of the point
What are the possible answers?
I
Either the point is inside or not: The result is a boolean value
Is inside: UML diagram
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
+ getArea(): double
+ isInside(px: double, py: double): boolean
...
I
Two double parameters
I
The return type is boolean return type
Is inside: Test case
r2
y
r1
r3
x
I
The point (3.5, 4.5) is inside r1 and r2 but not inside r3
Is inside: Test case
R e c t a n g l e 1 r 1 = new R e c t a n g l e 1 ( 1 . 0 , 1 . 0 , 4 . 0 , 4 . 0 ) ;
R e c t a n g l e 1 r 2 = new R e c t a n g l e 1 ( 2 . 0 , 4 . 0 , 8 . 0 , 2 . 0 ) ;
R e c t a n g l e 1 r 3 = new R e c t a n g l e 1 ( 6 . 0 , 2 . 0 , 2 . 0 , 5 . 0 ) ;
dou b l e px = 3 . 5 ;
dou b l e py = 4 . 5 ;
bo o l e a n b1 = r 1 . i s I n s i d e ( px , py ) ;
bo o l e a n b2 = r 2 . i s I n s i d e ( px , py ) ;
bo o l e a n b3 = r 3 . i s I n s i d e ( px , py ) ;
System . o u t . p r i n t l n ( ” R e c t a n g l e 1 c o n t a i n s p : ” + b1 ) ;
System . o u t . p r i n t l n ( ” R e c t a n g l e 2 c o n t a i n s p : ” + b2 ) ;
System . o u t . p r i n t l n ( ” R e c t a n g l e 3 c o n t a i n s p : ” + b3 ) ;
I
Local variables px, py of type double store point position
I
Local variables b1, b2, b3 of type boolean store results
I
System.out.println(...) is used to print on the console
Is inside: The logic
h
point inside
point outside
(x, y)
y
x
w
Is inside: The logic
h
point inside
point outside
(x, y)
y
x
w
Conditions: The point (px , py ) has to be located
1. above or on lower line
py ≥ y
2. below or on upper line
py ≤ y + h
3. right to or on of left line
4. left to or on right line
px ≥ x
px ≤ x + w
If all four conditions are satisfied, the point is inside the rectangle
Is inside: Java code – version 1
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o uble py ) {
return f a l s e ;
}
The code compiles but gives wrong results: Any point lies outside
the rectangle.
Required tools
I
Equality and relational operators
I
Conditional operators
Equality and relational operators
Operators for primitive type variables
Symbol
==
!=
>
>=
<
<=
Meaning
equal to
not equal to
greater than
greater than or equal to
less than
less than or equal to
Defined for
numbers and booleans
numbers and booleans
numbers
numbers
numbers
numbers
All operators are binary operators which require two parameters
and return a boolean value.
public c l a s s RelationalOperatorDemoProgram {
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 ) {
dou b l e a = 1 . 2 ;
dou b l e b = 5 . 9 ;
bo o l e a n b1 = a == b ;
bo o l e a n b2 = a != b ;
bo o l e a n b3 = a < b ;
bo o l e a n b4 = a <= b ;
bo o l e a n b5 = a > b ;
bo o l e a n b6 = a >= b ;
bo o l e a n b7 = b1 == b2 ;
bo o l e a n b8 = b1 != b2 ;
System . o u t . p r i n t l n ( ” b1 : ” + b1 ) ;
System . o u t . p r i n t l n ( ” b2 : ” + b2 ) ;
System . o u t . p r i n t l n ( ” b3 : ” + b3 ) ;
System . o u t . p r i n t l n ( ” b4 : ” + b4 ) ;
System . o u t . p r i n t l n ( ” b5 : ” + b5 ) ;
System . o u t . p r i n t l n ( ” b6 : ” + b6 ) ;
System . o u t . p r i n t l n ( ” b7 : ” + b7 ) ;
System . o u t . p r i n t l n ( ” b8 : ” + b8 ) ;
}
}
Conditional operators
Operators for booleans
Symbol
||
&&
Meaning
logical OR
logical AND
True if
one of the arguments is true
both arguments are true
All operators are binary operators which require two parameters
and return a boolean value.
Note
Logical AND (&&) has higher precedence than logical OR (||), i.e.
( b && b | | a ) == ( b && ( b | | a ) )
evaluates to false for some input a, b.
→ Always use parenthesis!
public c l a s s ConditionalOperatorDemoProgram {
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 ) {
bo o l e a n a = t r u e ;
bo o l e a n b = f a l s e ;
bo o l e a n c1 = a && a ;
bo o l e a n c2 = a && b ;
bo o l e a n c3 = b && b ;
bo o l e a n c4 = a | | a ;
bo o l e a n c5 = a | | b ;
bo o l e a n c6 = b | | b ;
bo o l e a n c7 = b && b | | a ;
bo o l e a n c8 = b && ( b | | a ) ;
System . o u t . p r i n t l n ( ” c1 : ” + c1 ) ;
System . o u t . p r i n t l n ( ” c2 : ” + c2 ) ;
System . o u t . p r i n t l n ( ” c3 : ” + c3 ) ;
System . o u t . p r i n t l n ( ” c4 : ” + c4 ) ;
System . o u t . p r i n t l n ( ” c5 : ” + c5 ) ;
System . o u t . p r i n t l n ( ” c6 : ” + c6 ) ;
System . o u t . p r i n t l n ( ” c7 : ” + c7 ) ;
System . o u t . p r i n t l n ( ” c8 : ” + c8 ) ;
}
}
Is inside: Java code – version 2
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
bo o l e a n c1 = px >= t h i s . x ;
bo o l e a n c2 = px <= t h i s . x + t h i s . w ;
bo o l e a n c3 = py >= t h i s . y ;
bo o l e a n c4 = py <= t h i s . y + t h i s . h ;
bo o l e a n r e s u l t = c1 && c2 && c3 && c4 ;
return r e s u l t ;
}
I
Local variables c1–c4 are the four conditions
I
result is only true if all four conditions are satisfied
Is inside: Java code – version 3
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
r e t u r n px >= t h i s . x && px <= t h i s . x + t h i s . w
&& py >= t h i s . y && py <= t h i s . y + t h i s . h ;
}
I
The condition can also be written in one statement
I
This is the style most programmers would prefer
Logical complement operator
Using an exclamation mark !, the value of a boolean is inverted.
p u b l i c c l a s s L o g i c a l C o m p l e m e n t O p e r a t o r D e m o Pr og ra m {
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 ) {
bo o l e a n a = f a l s e ;
dou b l e b = 1 . 2 ;
dou b l e c = 5 . 9 ;
bo o l e a n c1 = ! a ;
bo o l e a n c2 = ! ( b == c ) && c1 ;
bo o l e a n c3 = ! ( b != c ) ;
System . o u t . p r i n t l n ( ” c1 : ” + c1 ) ;
System . o u t . p r i n t l n ( ” c2 : ” + c2 ) ;
System . o u t . p r i n t l n ( ” c3 : ” + c3 ) ;
}
}
Is inside: Java code – version 4
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
r e t u r n ! ( ( px < t h i s . x ) | | ( px > t h i s . x + t h i s . w)
| | ( py < t h i s . y ) | | ( py > t h i s . y + t h i s . h ) ) ;
}
I
The point is contained in the rectangle if it is not outside
Step 5: Test if one rectangle intersects another
Test if rectangles intersect: Method specification
What do we need to test if a rectangle is intersecting?
Test if rectangles intersect: Method specification
What do we need to test if a rectangle is intersecting?
I
The rectangle (this)
I
Another rectangle
Test if rectangles intersect: Method specification
What do we need to test if a rectangle is intersecting?
I
The rectangle (this)
I
Another rectangle
What is the type of the result?
Test if rectangles intersect: Method specification
What do we need to test if a rectangle is intersecting?
I
The rectangle (this)
I
Another rectangle
What is the type of the result?
I
Either the rectangles intersect or not. The result is a boolean
Is intersecting: UML diagram
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
+ getArea(): double
+ isInside(px: double, py: double): boolean
+ isIntersecting(r: Rectangle): boolean
...
I
One parameter of type Rectangle
I
The return type is boolean
Is intersecting: Test case
r2
y
r1
x
Rectangles intersect:
I
r1 and r2: yes
I
r1 and r3: no
I
r2 and r3: yes
r3
Is intersecting: Test case
R e c t a n g l e r 1 = new R e c t a n g l e ( 1 . 0 , 1 . 0 , 4 . 0 , 4 . 0 ) ;
R e c t a n g l e r 2 = new R e c t a n g l e ( 2 . 0 , 4 . 0 , 8 . 0 , 2 . 0 ) ;
R e c t a n g l e r 3 = new R e c t a n g l e ( 6 . 0 , 2 . 0 , 2 . 0 , 5 . 0 ) ;
bo o l e a n b1 = r 1 . i s I n t e r s e c t i n g ( r 2 ) ;
bo o l e a n b2 = r 1 . i s I n t e r s e c t i n g ( r 3 ) ;
bo o l e a n b3 = r 2 . i s I n t e r s e c t i n g ( r 3 ) ;
System . o u t . p r i n t l n ( ” r 1 i n t e r s e c t s r 2 : ” + b1 ) ;
System . o u t . p r i n t l n ( ” r 1 i n t e r s e c t s r 3 : ” + b2 ) ;
System . o u t . p r i n t l n ( ” r 2 i n t e r s e c t s r 3 : ” + b3 ) ;
I
Local variables b1, b2, b3 of type boolean store results
I
System.out.println(...) is used to print on the console
Is intersecting: The logic
Is intersecting: The logic
Two conditions:
Is intersecting: The logic
Two conditions:
1. the x-ranges of both rectangles intersect
2. the y -ranges of both rectangles intersect
If both conditions are satisfied, the rectangles intersect
Is intersecting: The logic
Two conditions:
1. the x-ranges of both rectangles intersect
2. the y -ranges of both rectangles intersect
If both conditions are satisfied, the rectangles intersect
Two ranges [a, b] and [c, d] intersect, if
Is intersecting: The logic
Two conditions:
1. the x-ranges of both rectangles intersect
2. the y -ranges of both rectangles intersect
If both conditions are satisfied, the rectangles intersect
Two ranges [a, b] and [c, d] intersect, if
I
a ≤ d and b ≥ c
Is intersecting: Java code – version 1
p u b l i c boolean i s I n t e r s e c t i n g ( R e c t a n g l e r ) {
dou b l e xa = t h i s . x ;
dou b l e xb = t h i s . x + t h i s . w ;
dou b l e x c = r . x ;
dou b l e xd = r . x + r . w ;
bo o l e a n x r i = ( xa <= xd ) && ( xb >= x c ) ;
dou b l e ya =
dou b l e yb =
dou b l e y c =
dou b l e yd =
bo o l e a n y r i
this . y ;
this . y + this .h;
r.y;
r .y + r .h;
= ( ya <= yd ) && ( yb >= y c ) ;
bo o l e a n r e s u l t = x r i && y r i ;
return r e s u l t ;
}
Is intersecting: Java code – version 3
p u b l i c boolean i s I n t e r s e c t i n g ( R e c t a n g l e 1 r ) {
r e t u r n ( t h i s . x <= r . x + r . w)
&& ( t h i s . x + t h i s . w >= r . x )
&& ( t h i s . y <= r . y + r . h )
&& ( t h i s . y + t h i s . h >= r . y ) ;
}
Invoking the isIntersecting method – objects
Rectangle
x = 1.0
y = 1.0
w = 4.0
h = 4.0
inside: isIntersecting(Rectangle r)
inside main at: r1.isIntersecting(r2);
this
r1
Rectangle
x = 2.0
y = 4.0
w = 8.0
h = 2.0
this←r1, r←r2
r
r2
Step 6: Intersection of two rectangles
Create intersection: Method specification
What do we need to create the intersection of one rectangle
with another?
Create intersection: Method specification
What do we need to create the intersection of one rectangle
with another?
I
The rectangle (this)
I
Another rectangle
Create intersection: Method specification
What do we need to create the intersection of one rectangle
with another?
I
The rectangle (this)
I
Another rectangle
What is the type of the result?
Create intersection: Method specification
What do we need to create the intersection of one rectangle
with another?
I
The rectangle (this)
I
Another rectangle
What is the type of the result?
I
A new Rectangle
Create intersection: UML diagram
Rectangle
- x: double
- y: double
- w: double
- h: double
+ Rectangle(x: double, y: double, w: double , h: double)
+ draw(color: String, v: Viewer): void
+ getArea(): double
+ isInside(px: double, py: double): boolean
+ isIntersecting(r: Rectangle): boolean
+ intersection(r: Rectangle): Rectangle
I
One parameter of type Rectangle
I
The return type is Rectangle
Create intersection: Test case
V i e w e r v i e w e r = new V i e w e r ( ) ;
R e c t a n g l e r 1 = new R e c t a n g l e ( 1 . 0 ,
R e c t a n g l e r 2 = new R e c t a n g l e ( 2 . 0 ,
R e c t a n g l e r 3 = new R e c t a n g l e ( 6 . 0 ,
Rectangle r4 = r1 . i n t e r s e c t i o n ( r2
Rectangle r5 = r2 . i n t e r s e c t i o n ( r3
1.0 , 4.0 , 4.0);
4.0 , 8.0 , 2.0);
2.0 , 2.0 , 5.0);
);
);
r 1 . draw ( ” r e d ” , v i e w e r ) ;
r 2 . draw ( ” g r e e n ” , v i e w e r ) ;
r 3 . draw ( ” b l u e ” , v i e w e r ) ;
r 4 . draw ( ” c y a n ” , v i e w e r ) ;
r 5 . draw ( ” magenta ” , v i e w e r ) ;
viewer . s e t V i s i b l e ( true ) ;
I
Local variables r4 and r5 point to the new rectangles
Create intersection: Desired result
Create intersection: Objects
objects in computer memory
Rectangle
x = 1.0
y = 1.0
w = 4.0
h = 4.0
Rectangle
x = 2.0
y = 4.0
w = 8.0
h = 2.0
Rectangle
x = 6.0
y = 2.0
w = 2.0
h = 5.0
Rectangle
x = 2.0
y = 4.0
w = 3.0
h = 1.0
Rectangle
x = 6.0
y = 4.0
w = 2.0
h = 2.0
r1
r2
r3
r4
r5
variables in the main-method
Create intersection: The logic
Create intersection: The logic
Point coordinates
Create intersection: The logic
Point coordinates
I
I
Lower left point:
x1 = max(x1 , x2 ),
y1 = max(y1 , y2 )
Upper right point:
x2 = min(x1 + w1 , x2 + w2 ),
y2 = min(y1 + h1 , y2 + h2 )
Create intersection: The logic
Point coordinates
I
I
Lower left point:
x1 = max(x1 , x2 ),
y1 = max(y1 , y2 )
Upper right point:
x2 = min(x1 + w1 , x2 + w2 ),
y2 = min(y1 + h1 , y2 + h2 )
What if the two rectangles don’t intersect?
Create intersection: The logic
Point coordinates
I
I
Lower left point:
x1 = max(x1 , x2 ),
y1 = max(y1 , y2 )
Upper right point:
x2 = min(x1 + w1 , x2 + w2 ),
y2 = min(y1 + h1 , y2 + h2 )
What if the two rectangles don’t intersect?
I
Width: w = max(0, x2 − x1 )
I
Height: h = max(0, y2 − y1 )
→ Avoid rectangles of negative size
Mathematical functions – The Math class
The Math class
I
is part of the Java runtime
I
provides math functions such as sin, abs, pow, sqrt
I
uses radians for trigonometric functions
I
defines constants E and PI
Using the Math class
import s t a t i c j a v a . l a n g . Math . ∗ ;
p u b l i c c l a s s MathClassDemoProgram {
public static
dou b l e x1 =
dou b l e x2 =
dou b l e x3 =
v o i d main ( S t r i n g [ ] a r g s ) {
c o s ( PI / 2 ) ;
l o g ( pow ( E , 2 ) ) ;
a t a n 2 ( −1 , 1 ) + PI / 4 ;
System . o u t . p r i n t l n ( ” x1 : ” + x1 ) ;
System . o u t . p r i n t l n ( ” x2 : ” + x2 ) ;
System . o u t . p r i n t l n ( ” x3 : ” + x3 ) ;
}
}
I
Use import static java.lang.Math.*; to make methods
available
Create intersection: Java code
public Rectangle i n t e r s e c t i o n ( Rectangle r ) {
dou b l e x1 = max ( t h i s . x , r . x ) ;
dou b l e y1 = max ( t h i s . y , r . y ) ;
dou b l e x2 = min ( t h i s . x + t h i s . w , r . x + r . w ) ;
dou b l e y2 = min ( t h i s . y + t h i s . h , r . y + r . h ) ;
dou b l e w = max ( 0 , x2 − x1 ) ;
dou b l e h = max ( 0 , y2 − y1 ) ;
R e c t a n g l e r e s u l t = new R e c t a n g l e ( x1 , y1 , w , h ) ;
return r e s u l t ;
}
The this pointer is optional. . .
this can be omitted in many cases
Instead of
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
r e t u r n px >= t h i s . x && px <= t h i s . x + t h i s . w
&& py >= t h i s . y && py <= t h i s . y + t h i s . h ;
}
we could also write
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
r e t u r n px >= x && px <= x + w
&& py >= y && py <= y + h ;
}
this can be omitted in many cases
Instead of
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
r e t u r n px >= t h i s . x && px <= t h i s . x + t h i s . w
&& py >= t h i s . y && py <= t h i s . y + t h i s . h ;
}
we could also write
p u b l i c b o o l e a n i s I n s i d e ( d o u b l e px , d o u ble py ) {
r e t u r n px >= x && px <= x + w
&& py >= y && py <= y + h ;
}
However, we always use this for the sake of clarity
Things worth remembering
I
Creating a class includes
I
I
I
problem analysis
software design using the UML
Java programming
I
Classes consist of attributes and methods
I
Attributes are variables in the scope of a class
I
Primitive types are used for “elementary” items like numbers
or single characters
I
Naming conventions help to decide about names for classes,
methods and variables
I
For us, methods are public and attributes private
I
The Math class provides mathematical functions
I
Methods are always executed by objects
I
The this pointer refers to the current object
I
Passing a method parameter involves an assignment