Download presentation
Presentation is loading. Please wait.
Published byMarybeth Bond Modified over 6 years ago
1
Java’s World in UML Object Shape {abstract} This is done implicitly
ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + draw(Graphics g):void + erase(Graphics g):void + 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. 7/24/2018 IT 179
2
Java’s World in UML Object Shape {abstract} Rectangle Circle ThreeD
This is done implicitly Shape {abstract} extends Rectangle Circle ThreeD {interface} RectanglePrism Cylinder implements 7/24/2018 IT 179
3
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 shapeName; public Shape() { this.shapeName = DEFAULT_NAME; } public Shape( String name ) { setShapeName( name ); protected void setShapeName( String name ) { shapeName = new String( name ); public abstract double getSurfaceArea(); …………………………… 7/24/2018 IT 179
4
Java’s World in UML Object Shape {abstract} Circle Rectangle
This is done implicitly Shape {abstract} extends Circle Rectangle 7/24/2018 IT 179
5
Shape {abstract} Rectangle Circle
ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + draw(Graphics g):void + erase(Graphics g):void + 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 7/24/2018 IT 179
6
public class Circle extends Shape { private double radius;
package shapes; public class Circle extends Shape { private double radius; public Circle() { super( "Circle" ); setRadius( super.DEFAULT_SIZE ); } public Circle( double theRadius ) { if ( theRadius <= 0.0 ) { setRadius( Shape.DEFAULT_SIZE ); else { setRadius( theRadius ); ...... 7/24/2018 IT 179
7
public class Circle extends Shape { private double radius; .....
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; 7/24/2018 IT 179
8
Extend to three-dimension
Shape {abstract} Rectangle Circle RectanglePrism z: double + setZ(double): void + getZ(): double + getVolume(): double RectanglePrism Cylinder depth: double + setDepth(double): void + getDepth(): double + getCapacity(): double Cylinder 7/24/2018 IT 179
9
Java’s World in UML Object Shape {abstract} Rectangle Circle
extends RectanglePrism Cylinder 7/24/2018 IT 179
10
Is there an even better way?
public double price(Cylinder k, double unitPrice) { return k.getCapacity()*unitPrice; } public double price(RectanglePrism k, double unitPrice) { return k.getVolume()*unitPrice; overloading What’s the problem? 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? 7/24/2018 IT 179
11
Java’s World in UML Object Shape {abstract} Rectangle Circle ThreeD
This is done implicitly Shape {abstract} extends Rectangle Circle ThreeD {interface} RectanglePrism Cylinder implements 7/24/2018 IT 179
12
interface Object Shape {abstract} Rectangle Circle RectanglePrism
ThreeD {interface} + setDepth(double): void + getDepth(): double + getVolume(): double Rectangle Circle RectanglePrism Cylinder implements 7/24/2018 IT 179
13
polymorphism public double price(ThreeD k, double unitPrice) {
return k.getVolum()*unitPrice; } polymorphism 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 7/24/2018 IT 179
14
ThreeD must be all abstract
package shapes; public interface ThreeD { double getDepth(); void setDepth( double theDepth ); double getVolume(); } 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. 7/24/2018 IT 179
15
Cylinder (I) 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 ); setDepth( theWidth ); .... 7/24/2018 IT 179
16
Object’s toString and equals methods
+ toString(): String + equals(o: Object): boolean the default method use identities to do the job. Shape {abstract} Now, we have a better idea about how this job to be done. Rectangle Circle 7/24/2018 IT 179
17
Rectangle Don’t make it Rectangle
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() ) ); ..... Don’t make it Rectangle So, this method can take any object of any class. 7/24/2018 IT 179
18
r1 r2 c rp1 rp2 cd1 cd2 7/24/2018 IT 179 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 = 1 height = 1.5 z =1.3 getSurfaceArea() {…} getVolumn)_ {…} …. length = 2 height = 1 getSurfaceArea() {…} …. length = 3 height = 2 getSurfaceArea() {…} …. r1 r2 c rp1 rp2 cd1 cd2 radius = 0.7 getSurfaceArea() {…} …. length = 1.6 height = 3 z =1.6 getSurfaceArea() {…} getVolumn)_ {…} …. radius = 1.5 depth = 3 getSurfaceArea() {…} getVolumn)_ {…} …. radius = 1 depth = 2 getSurfaceArea() {…} getVolumn)_ {…} …. 7/24/2018 IT 179
19
Visibility Access Levels Modifier Class Package Subclass
Every one in the World public Y protected N no modifier private 7/24/2018 IT 179
20
Comparable<T> {interface}
Object Comparable<T> {interface} + compareTo(o:T): int Shape {abstract} ThreeD {interface} + setDepth(double): void + getDepth(): double + getVolume(): double Rectangle Circle RectanglePrism Cylinder 7/24/2018 IT 179
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.