Question: Programming Exercises: (From the Textbook) : - 13.1 (Triangle class) - Design a new Triangle class that extends the abstract GeometicObject class. Draw the UML

Programming Exercises: (From the Textbook): - 13.1 (Triangle class) - Design a new Triangle class that extends the abstract GeometicObject class. Draw the UML diagram for yourself. (please refer to textbook for full problem description).Programming Exercises: (From the Textbook): - 13.1 (Triangle class) - Design anew Triangle class that extends the abstract GeometicObject class. Draw the UMLdiagram for yourself. (please refer to textbook for full problem description). 1public abstract class GeometricObject { 2 private String color = "white"; 3

1 public abstract class GeometricObject { 2 private String color = "white"; 3 private boolean filled; 4 private java.util.Date dateCreated;

5 6 /** Construct a default geometric object */ 7 protected GeometricObject() {

VideoNote

Abstract GeometricObject class

abstract class

abstract method abstract modifier

abstract class

8

9} 10

11 12 13 14 15 16 } 17 

dateCreated = new java.util.Date();

18 19 20

/** Construct a geometric object with color and filled value */ 

protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled;

/** Return color */ 

public String getColor() { return color;

13.2 Abstract Classes 497 Abstract class name is italicized

GeometricObject

-color: String -filled: boolean -dateCreated: java.util.Date 
#GeometricObject() 
#GeometricObject(color: string, filled: boolean) 

+getColor(): String +setColor(color: String): void +isFilled(): boolean +setFilled(filled: boolean): void +getDateCreated(): java.util.Date +toString(): String

+getArea(): double

+getPerimeter(): double

The # sign indicates protected modifier

Abstract methods are italicized

Methods getArea and getPerimeter are overridden in Circle and Rectangle. Superclass methods are generally omitted in the UML diagram for subclasses.

Rectangle

Circle

-radius: double 

+Circle()

+Circle(radius: double) 
+Circle(radius: double, color: string, filled: boolean) 
+getRadius(): double +setRadius(radius: double): void +getDiameter(): double 

23 24 25 26 } 27

28 29 30 31 32 } 33 

34 35 36 37 } 38

39 40 41 42 }

/** Set a new color */ 

public void setColor(String color) { this.color = color;

/** Return filled. Since filled is boolean, * the get method is named isFilled */ 

public boolean isFilled() { return filled;

/** Set a new filled */ 

public void setFilled(boolean filled) { this.filled = filled;

/** Get dateCreated */ 

public java.util.Date getDateCreated() { return dateCreated;

-width: double -height: double 
+Rectangle() 

FIGURE 13.1

21 } 22

 +Rectangle(width: double, height: double) 
 +Rectangle(width: double, height: double, color: string, filled: boolean) 

+getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(height: double): void

The new GeometricObject class contains abstract methods.

Abstract Classes and Interfaces

43 44 45 46 47 48 } 49 

@Override public String toString() {

return "created on " + dateCreated + " color: " + color + " and filled: " + filled;

50 /** Abstract method getArea */ 51 public abstract double getArea(); 52 53 /** Abstract method getPerimeter */ 54 public abstract double getPerimeter(); 55 }

Preview File Edit View Go Tools Window Help it4) 37% CO Wed Jul 5 8:40:01 PM Q.E Introduction to java programming_by Y Daniel Liang.10th edition.pdf (page 521 of 1,345) ! Q Search 39 **Get dateCreated/ 40 public java.util.Date getDateCreatedO f 41 42 Introduct return dateCreated; 498 Chapter 13 Abstract Classes and Interfaces 43 44 Override 45 public String toString) 46 47 48 49 return "created on"+dateCreated +"ncolor: "color " and fi1led: fil1ed 497 50* Abstract method getArea/ 51 public abstract double getArea); 52 53 Abstract method getPerimeter 54 public abstract double getPerimeterO; abstract method abstract method PM Abstract classes are like regular classes, but you cannot create instances of abstract classes using the new operator. An abstract method is defined without implementation. Its implemen- tation is provided by the subclasses. A class that contains abstract methods must be defined as abstract The constructor in the abstract class is defined as protected, because it is used only by subclasses. When you create an instance of a concrete subclass, its superclass's constructor is invoked to initialize data fields defined in the superclass. why protected constructor? PM The Geometricobject abstract class defines the common features (data and methods) for geometric objects and provides appropriate constructors. Because you don't know how to compute areas and perimeters of geometric objects, getAreaO and getPerimeterO are defined as abstract methods. These methods are implemented in the subclasses. The imple- mentation of Circle and Rectangle is the same as in Listings 13.2 and 13.3, except that they extend the Geometricobject class defined in this chapter. You can see the complete code for these two programs from www.cs.armstrong.eduliangintro10e/html/Circle.html and www.cs.armstr PM 498 implement Circle lement Rectangle edu/liang/introl0e/html/Rectangle.html ely

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!