Post on 30-Mar-2020
Department of Information Technology Uppsala University
User Interface Programming OOP/Java Primer
Step 1
Uppsala University
1/17/11 | #2 @ UU/IT
Object-Oriented Programming
Focus on OOP rather than Java to start with
What is an Object?
Uppsala University
1/17/11 | #3 @ UU/IT
Object-Oriented Programming
Focus on OOP rather than Java to start with
What is an Object?
”I Exist! Therefore I am!”
Uppsala University
1/17/11 | #4 @ UU/IT
Object An entity, a collection of data*
A blueprint for the creation of
INSTANCES
* And some other stuff too
Uppsala University
1/17/11 | #5 @ UU/IT
Objects (instances) Knows ”all” about themselves
• Data • Available ”messages” • What they need
Uppsala University
1/17/11 | #6 @ UU/IT
Inheritance
Objects can inherit from each other • Object hierarchies • X inherits from Y ==> ”X is a type of Y”
Inheritance is a strong programming concept • But can be overused
Uppsala University
1/17/11 | #7 @ UU/IT
Basic structure
public class <Name> { //class variables
//instance variables
//constructors
//accessor methods
//class methods }
Uppsala University
1/17/11 | #8 @ UU/IT
Concepts Class variables = common to all instances Instance variables = unique in every instance
Constructor = Initializing method Accessor = returns contents of variables
• Provides for encapsulation Methods = performs the ”real work”
Uppsala University
1/17/11 | #9 @ UU/IT
class Ship { private int speed = 0; private int course = 0;
public void setCourse(degree) { course = degree; } public int getCourse () { return course;}
public void move() { … } public report() {
System.out.println( ”Course is ” + course); …}
}
Uppsala University
1/17/11 | #10 @ UU/IT
class ShipMain1 { public static void main(String [] args){ Ship argo = new Ship(); argo.setCourse(90); argo.move(); argo.report(); } }
// ”setCourse” is an accessor method.
Uppsala University
1/17/11 | #11 @ UU/IT
OOP vs. Procedures An object encapsulates a certain ”aspect” of a
system It defines what we may do with this ”aspect” It has a limited scope
• only works with its own responsibility
It communicates with other objects to make things happen
Uppsala University
1/17/11 | #12 @ UU/IT
Good programming practice Make small objects
• Clear definition of what the object is. Make small methods
• Clear definition of what the method does
Give the object a simple structure • Don’t mix too much in a single object
Use inheritance (with some constraints)
Uppsala University
1/17/11 | #13 @ UU/IT
Private data
Modifiers: ensure class integrity
Private, Protected, Public – controls access Static – declares class variables and methods Final – declares constant values
Uppsala University
1/17/11 | #14 @ UU/IT
Good Programming Practice Do not let other classes modify variable
values directly! Use Accessor methods!
• boat.speed = 10; // Bad! • boat.setSpeed(10); // Good!
The accessor method can make integrity checks.
Uppsala University
1/17/11 | #15 @ UU/IT
Packages
A collection of classes that belong together
A package defines a special part of the application
Files within a package are stronger related to each other
Uppsala University
1/17/11 | #16 @ UU/IT
Relations
• Knows about
• Has – contains other objects
• Is – Inheritance relation
Uppsala University
1/17/11 | #17 @ UU/IT
Geometry Simple package to calculate geometric
shapes • Cylinders, balls, pyramids, cones and boxes
Simple arithmetic calculations In- and output
Uppsala University
1/17/11 | #18 @ UU/IT
Sphere radius : double;
volume : double; surface : double;
Cylinder radius : double; height : double; volume : double; surface : double;
Etc.
Uppsala University
1/17/11 | #19 @ UU/IT
public class Sphere {!!private double radius; !!private static final double PI = Math.PI;!
!!!public Sphere () { radius = 1; }!
public Sphere (double r) { radius = r;}!
!public void setRadius (double r) { // Accessor!! !radius = r; !
!} !! public double getRadius () {!
! !return radius;! !}!
public double volume () { !! !return 4 * PI * radius * radius * radius / 3;!
!}!!public static double volume(double r) { // Class method that can be used!! !return 4 * PI * r * r *r / 3; ! // directly without an instance!! ! ! !}!
} !
Uppsala University
1/17/11 | #20 @ UU/IT
Sphere Cylinder Cone Pyramid Box Kube
Geometric objects
Uppsala University
1/17/11 | #21 @ UU/IT
Sphere Cylinder Cone
Round objects
Square objects
Geometric objects
Pyramid Box
Kube
Uppsala University
1/17/11 | #22 @ UU/IT
Choose Shape
Start
Choose parameter
Requests >
< Param
eters
Sphere
Cylinder
Cone
Cube
.
.
.
Round O
bjects
Square O
bjects
Geo
metric O
bjects Object-Oriented
Design
Uppsala University
1/17/11 | #23 @ UU/IT
Good programming practice
Classes should be: • Correct – good overview • Efficient – small routines, little overhead • Reusable • Changeable