Two paradigms of solving programs using PLs Procedure



















![import shapes. *; public static void main(String[] args) { Rectangle r 1 = new import shapes. *; public static void main(String[] args) { Rectangle r 1 = new](https://slidetodoc.com/presentation_image_h/d16d9ec87d74f45bc5b10be1ec0b1b63/image-20.jpg)


- Slides: 22
Two paradigms of solving programs using PL’s: Procedure Oriented: Find a sequence of manipulations on data - • We design procedures • Programs = Statements + functions Object Oriented: Find a model/configuration for objects • We design and use objects. • Programs = Classes + Objects 12/2/2020 IT 179 1
Ludwig Wittgenstein (1889 -1951) • • • The world is the totality of facts. The facts in logical space are the world. A fact is the existence of states of affairs. A states of affairs is a combination of objects. The configuration of objects produces states of affairs. 1921 The configuration of objects produces the programming world. • • Objects are simple. In order to know an objects, one must know not its external but all its internal qualities. 12/2/2020 IT 179 2
Java’s World in UML Object This is done implicitly Shape {abstract} - Shape. Name: String # set. Shape. Name(new. Shape. Name: String): void + get. Shape. Name(): String + draw(Graphics g): void + erase(Graphics g): void + get. Surface. Area(): double + get. Perimeter(): double 12/2/2020 IT 179 There are some details that are not as important to the concept of shape, hence leave them for the programmer to implement. 3
Object Java’s World in UML This is done implicitly Shape {abstract} Three. D {interface} extends Rectangle Circle Rectangle. Prism Cylinder implements 12/2/2020 IT 179 4
Shape package shapes; public abstract class Shape { protected static final double DEFAULT_SIZE = ( double ) 1. 0; protected static final String DEFAULT_NAME = "Unknown"; private String shape. Name; public Shape() { this. shape. Name = DEFAULT_NAME; } public Shape( String name ) { set. Shape. Name( name ); } protected void set. Shape. Name( String name ) { shape. Name = new String( name ); } public abstract double get. Surface. Area(); public abstract double get. Perimeter(); ……………… } 12/2/2020 IT 179 5
Object Java’s World in UML This is done implicitly Shape {abstract} Circle 12/2/2020 extends Rectangle IT 179 6
Shape {abstract} - Shape. Name: String # set. Shape. Name(new. Shape. Name: String): void + get. Shape. Name(): String + draw(Graphics g): void + erase(Graphics g): void + get. Surface. Area(): double + get. Perimeter(): double Rectangle Circle - radius: double + set. Radius(new. Radius: double): void + get. Radius(): double 12/2/2020 IT 179 - length: double - height: double + set. Length(new. Length: double): void + get. Length(): double + set. Height(new. Height: double): void + get. Height(): double 7
package shapes; public class Circle extends Shape { private double radius; public Circle() { super( "Circle" ); set. Radius( super. DEFAULT_SIZE ); } public Circle( double the. Radius ) { super( "Circle" ); if ( the. Radius <= 0. 0 ) { set. Radius( Shape. DEFAULT_SIZE ); } else { set. Radius( the. Radius ); } }. . . } 12/2/2020 IT 179 8
package shapes; public class Circle extends Shape { private double radius; . . public double get. Radius() { return this. radius; } public void set. Radius( double the. Radius ) { if ( the. Radius <= 0 ) { return; } this. radius = the. Radius; } public double get. Surface. Area() { return this. radius * Math. PI; } public double get. Perimeter() { return 2 * this. radius * Math. PI; } } 12/2/2020 IT 179 9
Extend to three-dimension Shape {abstract} Rectangle. Prism Circle Cylinder - z: double - depth: double + set. Z(double): void + get. Z(): double + get. Volume(): double + set. Depth(double): void + get. Depth(): double + get. Capacity(): double 12/2/2020 IT 179 10
Object Java’s World in UML Shape {abstract} 12/2/2020 Rectangle Circle Rectangle. Prism Cylinder IT 179 extends 11
public double price(Cylinder k, double unit. Price) { return k. get. Capacity()*unit. Price; } overloading public double price(Rectangle. Prism k, double unit. Price) { return k. get. Volume()*unit. Price; } Any problem? Is there a better way? /* This requires that get. Volumn is defined in Shape public double price(Shape k, double unit. Price) { return k. get. Volume()*unit. Price; } Is there an even better way? How to unify the interface? 12/2/2020 IT 179 */ Yes, we use “interface”. 12
Object Java’s World in UML This is done implicitly Shape {abstract} Three. D {interface} extends Rectangle Circle Rectangle. Prism Cylinder implements 12/2/2020 IT 179 13
interface Object Shape {abstract} Three. D {interface} + set. Depth(double): void + get. Depth(): double + get. Volume(): double 12/2/2020 Rectangle Circle Rectangle. Prism Cylinder IT 179 implements 14
public double price(Three. D k, double unit. Price) { return k. get. Volum()*unit. Price; } polymorphism public static void main(String[] args) { Rectangle a; Circle b; Rectangle. Prism c; Cylinder d; … … … price(c, 2. 99)+price(d, 3. 99); // price(a, 2. 99)+price(b, 3. 99); 12/2/2020 Not allowed IT 179 15
Three. D package shapes; public interface Three. D { double get. Depth(); void set. Depth( double the. Depth ); double get. Volume(); must be all abstract } Any class implements the interface must implement all methods in the interface. Some interfaces don’t even have the body called marker interface. 12/2/2020 IT 179 16
Cylinder (I) package shapes; public final class Cylinder extends Circle implements Three. D { private double depth; public Cylinder() { this( Shape. DEFAULT_SIZE, Shape. DEFAULT_SIZE ); } public Cylinder( double the. Radius, double the. Width ) { set. Shape. Name( "Cylinder" ); if ( the. Radius <= 0. 0 ) { set. Radius( Shape. DEFAULT_SIZE ); } else { set. Radius( the. Radius ); } if ( the. Width <= 0. 0 ) { set. Depth( Shape. DEFAULT_SIZE ); } else { set. Depth( the. Width ); } }. . } 12/2/2020 IT 179 17
Object’s to. String and equals methods Object + to. String(): String + equals(o: Object): boolean Shape {abstract} Rectangle 12/2/2020 the default method use identities to do the job. Now, we have a better idea about how this job to be done. Circle IT 179 18
package shapes; Rectangle public class Rectangle extends Shape {. . . . public String to. String() { return this. get. Shape. Name() + ": length = " + this. length + ", height = " + this. height; } Don’t make it Rectangle So, this method can take any object of any class. public boolean equals( Object o ) { if ( ( o == null ) || ( ! ( o instanceof Rectangle ) ) ) { return false; } return ( ( Rectangle ) o ). get. Length() == get. Length() ) && ( ( ( Rectangle ) o ). get. Height() == get. Height() ) ); }. . . } 12/2/2020 IT 179 19
import shapes. *; public static void main(String[] args) { Rectangle r 1 = new Rectangle(2, 1); Rectangle r 2 = new Rectangle(3, 2); Circle c = new Circle(0. 7); Rectangle. Prism rp 1 = new Rectangle. Prism(1, 1. 5, 1. 3); Retangle. Prime rp 2 =. . . ; Cylinder cd 1 =. . . , cd 2 =. . . ; . . . } r 1 r 2 radius = 0. 7 get. Surface. Area() {…} …. length = 2 height = 1 get. Surface. Area() {…} …. length = 1 height = 1. 5 z =1. 3 get. Surface. Area() {…} get. Volumn)_ {…} …. length = 3 height = 2 get. Surface. Area() {…} …. c length = 1. 6 height = 3 z =1. 6 get. Surface. Area() {…} get. Volumn)_ {…} …. rp 1 rp 2 cd 1 cd 2 12/2/2020 radius = 1 depth = 2 get. Surface. Area() {…} get. Volumn)_ {…} …. IT 179 radius = 1. 5 depth = 3 get. Surface. Area() {…} get. Volumn)_ {…} …. 20
Visibility Access Levels Modifier Class Package Subclass Every one in the World public Y Y protected Y Y Y N no modifier Y Y N N private Y N N N 12/2/2020 IT 179 21
Object Comparable<T> {interface} + compare. To(o: T): int Shape {abstract} Three. D {interface} + set. Depth(double): void + get. Depth(): double + get. Volume(): double 12/2/2020 Rectangle Circle Rectangle. Prism Cylinder IT 179 22