10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape...

19
03/22/22 IT 179 1 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + getSurfaceArea(): double + getPerimeter(): double There are some details that are not as important to the concept of shape, hence leave them for the programmer to implement.

Transcript of 10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape...

04/19/23 IT 179 1

Java’s World in UML

Object

Shape{abstract}

This is done implicitly

Shape {abstract}

- ShapeName: String

# setShapeName(newShapeName: String): void

+ getShapeName(): String

+ getSurfaceArea(): double

+ getPerimeter(): double

There are some details that are not as important to the concept of shape, hence leave them for the programmer to implement.

04/19/23 IT 179 2

Shape

04/19/23 IT 179 3

Java’s World in UML

Object

Shape

Circle Rectangle

{abstract} extends

This is done implicitly

04/19/23 IT 179 4

Shape {abstract}

- ShapeName: String

# setShapeName(newShapeName: String): void

+ getShapeName(): String

+ getSurfaceArea(): double

+ getPerimeter(): double

Rectangle

- length: double

- height: double

+ setLength(newLength: double): void

+ getLength(): double

+ setHeight(newHeight: double): void

+ getHeight(): double

Circle

- radius: double

+ setRadius(newRadius: double): void

+ getRadius(): double

04/19/23 IT 179 5

package shapes;

public class Circle extends Shape { private double radius;

public Circle() { super( "Circle" ); setRadius( super.DEFAULT_SIZE ); }

public Circle( double theRadius ) { super( "Circle" ); if ( theRadius <= 0.0 ) { setRadius( Shape.DEFAULT_SIZE ); } else { setRadius( theRadius ); } }

......

......}

Circle (I)

04/19/23 IT 179 6

package shapes;

public class Circle extends Shape { private double radius;.......... public double getRadius() { return this.radius; }

public void setRadius( double theRadius ) { if ( theRadius <= 0 ) { return; } this.radius = theRadius; }

public double getSurfaceArea() { return this.radius * this.radius * Math.PI; }

public double getPerimeter() { return 2 * this.radius + Math.PI; }}

Circle (II)

04/19/23 IT 179 7

Cylinder

Extend to three-dimension

Shape

CircleRectangle

{abstract}

Cylinder

- depth: double

+ setDepth(double): void

+ getDepth(): double

+ getCapacity(): double

RectanglePrismRectanglePrism

- z: double

+ setZ(double): void

+ getZ(): double

+ getVolume(): double

04/19/23 IT 179 8

public double price(Cylinder k, double unitPrice) { return k.getCapacity()*unitPrice; }

public double price(RectanglePrism k, double unitPrice) { return k.getVolume()*unitPrice; }

Is there a better way?

/* Supposed that getVolumn is defined in Shape */ public double price(Shape k, double unitPrice) { return k.getVolume()*unitPrice; }

Is there an even better way?Yes, we use “interface”.How to unify the interface?

What’s the problem?

04/19/23 IT 179 9

Java’s World in UML

Object

Shape

CircleRectangle

RectanglePrism Cylinder

ThreeD{interface}

{abstract}

implements

extends

This is done implicitly

04/19/23 IT 179 10

interface Object

Shape

CircleRectangle

RectanglePrism Cylinder

{abstract}ThreeD

{interface}

+ setDepth(double): void

+ getDepth(): double

+ getVolume(): double

implements

04/19/23 IT 179 11

public double price(ThreeD k, double unitPrice) {return k.getVolum()*unitPrice;

}

public static void main(String[] args) {

Rectangle a;Circle b;RectanglePrism c;Cylinder d;

… … …

price(c,2.99)+price(d,3.99);

// price(a,2.99)+price(b,3.99); Not allowed

04/19/23 IT 179 12

package shapes;

public interface ThreeD { double getDepth(); void setDepth( double theDepth ); double getVolume();}

ThreeD

Any class implements the interface must implement all methods in the interface.

must be all abstract

Some interfaces don’t even have the bodycalled marker interface.

04/19/23 IT 179 13

package shapes;

public final class Cylinder extends Circle implements ThreeD { private double depth; public Cylinder() { this( Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE ); } public Cylinder( double theRadius, double theWidth ) { setShapeName( "Cylinder" ); if ( theRadius <= 0.0 ) { setRadius( Shape.DEFAULT_SIZE ); } else { setRadius( theRadius ); } if ( theWidth <= 0.0 ) { setDepth( Shape.DEFAULT_SIZE ); } else { setDepth( theWidth ); } }....}

Cylinder (I)

04/19/23 IT 179 14

Collection List

LinkedList

{interface}

AbstractList{abstract}

ArrayList

Java Provides a List interface for several implementations

{interface}

The Java Collections Framework (JCF)

RandomAccess{interface}

AbstractSequentialList{abstract}

Stack

Vector

04/19/23 IT 179 15

Object’s toString and equals methods

Shape

CircleRectangle

{abstract}

Object

+ toString(): String

+ equals(o: Object): boolean

the default methoduse identities to do the job.

Now, we have a better idea about how this job to be done.

04/19/23 IT 179 16

package shapes;

public class Rectangle extends Shape {........ public String toString() { return this.getShapeName() + ": length = " + this.length + ", height = " + this.height; }

public boolean equals( Object o ) {

if ( ( o == null ) || ( ! ( o instanceof Rectangle ) ) ) { return false; }

return ( ( ( ( Rectangle ) o ).getLength() == getLength() ) && ( ( ( Rectangle ) o ).getHeight() == getHeight() ) ); }

.....}

Rectangle

So, this method can take any object of any class.

Don’t make it Rectangle

Visibility

04/19/23 IT 179 17

Access Levels

Modifier Class Package Subclass World

public Y Y Y Y

protected Y Y Y N

no modifier Y Y N N

private Y N N N

04/19/23 IT 179 18

import shapes.*;

public static void main(String[] args) {

Rectangle r1 = new Rectangle(2,1);

Rectangle r2 = new Rectangle(3,2);

Circle c = new Circle(0.7);

RectanglePrism rp1 =

new RectanglePrism(1,1.5,1.3);

RetanglePrime rp2 = ... ;

Cylinder cd1 = ... ,cd2 = ... ;

...}

length = 2height = 1 getSurfaceArea() {…}….

length = 3height = 2 getSurfaceArea() {…}….

radius = 0.7getSurfaceArea() {…}….

radius = 1depth = 2getSurfaceArea() {…}getVolumn)_ {…}….

radius = 1.5depth = 3getSurfaceArea() {…}getVolumn)_ {…}….

length = 1height = 1.5z =1.3getSurfaceArea() {…}getVolumn)_ {…}….

length = 1.6height = 3z =1.6getSurfaceArea() {…}getVolumn)_ {…}….

r1

r2

c

rp1

rp2

cd1

cd2

04/19/23 IT 179 19

L1

L2

L3

L4

“Sean”SimpleListAry<Integer>datasize = 5void remove(int i) {….}void add(int I, Integer item) {…} Integer max() {…};…

“Leon”

“Tom”

“Mary”

20

45

5

14

24

SimpleListAry<String>datasize = 4void remove(int i) {….}void add(int I, String item) {…} Stringr max() {…};…

datanext

SimpleListLk<String>headsize = 2void remove(int i) {….}void add(int I, String item) {…} String max() {…};…

SimpleListLk<Integer>headsize = 3void remove(int i) {….}void add(int I, Integer item) {…} Integer max() {…};…

datanext

datanext

datanext

datanext

“ISU”

“IT179”

11 270 228