Download presentation
Presentation is loading. Please wait.
Published byLily Whitehead Modified over 9 years ago
1
10/7/2015IT 1791 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.
2
10/7/2015IT 1792 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(); …………………………… } Shape
3
10/7/2015IT 1793 Java’s World in UML Object Shape CircleRectangle {abstract} extends This is done implicitly
4
10/7/2015IT 1794 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
5
10/7/2015IT 1795 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)
6
10/7/2015IT 1796 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)
7
10/7/2015IT 1797 Cylinder Extend to three-dimension Shape CircleRectangle {abstract} Cylinder - depth: double + setDepth(double): void + getDepth(): double + getCapacity(): double RectanglePrism - z: double + setZ(double): void + getZ(): double + getVolume(): double
8
10/7/2015IT 1798 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?
9
10/7/2015IT 1799 Java’s World in UML Object Shape CircleRectangle RectanglePrismCylinder ThreeD {interface} {abstract} implements extends This is done implicitly
10
10/7/2015IT 17910 interface Object Shape CircleRectangle RectanglePrismCylinder {abstract} ThreeD {interface} + setDepth(double): void + getDepth(): double + getVolume(): double implements
11
10/7/2015IT 17911 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
12
10/7/2015IT 17912 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 body called marker interface.
13
10/7/2015IT 17913 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)
14
10/7/2015IT 17914 Collection List LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface for several implementations {interface} The Java Collections Framework (JCF) RandomAccess {interface} A bstractSequentialLis t {abstract} Stack Vector
15
10/7/2015IT 17915 O bject’s toString and equals methods Shape CircleRectangle {abstract} Object + toString(): String + equals(o: Object): boolean the default method use identities to do the job. Now, we have a better idea about how this job to be done.
16
10/7/2015IT 17916 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
17
Visibility 10/7/2015IT 17917 Access Levels ModifierClassPackageSubclassWorld publicYYYY protectedYYYN no modifierYYNN privateYNNN
18
10/7/2015 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 = 2 height = 1 getSurfaceArea() {…} …. length = 3 height = 2 getSurfaceArea() {…} …. radius = 0.7 getSurfaceArea() {…} …. radius = 1 depth = 2 getSurfaceArea() {…} getVolumn)_ {…} …. radius = 1.5 depth = 3 getSurfaceArea() {…} getVolumn)_ {…} …. length = 1 height = 1.5 z =1.3 getSurfaceArea() {…} getVolumn)_ {…} …. length = 1.6 height = 3 z =1.6 getSurfaceArea() {…} getVolumn)_ {…} …. r1 r2 c rp1 rp2 cd1 cd2
19
10/7/2015 IT 179 19 L1 L2 L3 L4 “Sean” SimpleListAry data size = 5 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … “Leon” “Tom” “Mary” 20 45 5 14 24 SimpleListAry data size = 4 void remove(int i) {….} void add(int I, String item) {…} Stringr max() {…}; … data next SimpleListLk head size = 2 void remove(int i) {….} void add(int I, String item) {…} String max() {…}; … SimpleListLk head size = 3 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … data next data next data next data next “ISU” “IT179” 11 270 228
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.