Case Study 1 Session 1: International Business Globalization
Case Study 1session1 International Business Globalization Dr Shab
Implement the Shape hierarchy shown in Fig. 9.3 of Java How to Program. Each TwoDimensionalShape should contain method getArea to calculate the area of the two- dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape.
Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume.
Sample Paper For Above instruction
Implementation of Shape Hierarchy in Java
The object-oriented programming paradigm facilitates the creation of hierarchical class structures that model real-world entities effectively. One common application of inheritance is the representation of geometric shapes, distinguished by their dimensions and properties. This paper discusses the implementation of a shape hierarchy in Java, inspired by the example from "Java How to Program," focusing on two-dimensional and three-dimensional shapes. The primary objectives include defining abstract classes for shapes, implementing concrete subclasses, and utilizing polymorphism to process various shape objects dynamically.
Shape Hierarchy Overview
The hierarchy begins with an abstract base class named Shape. From it, two main subclasses extend: TwoDimensionalShape and ThreeDimensionalShape. Each of these subclasses is further extended by specific shape classes. For TwoDimensionalShape, examples include Circle, Rectangle, and Triangle. For ThreeDimensionalShape, examples include Sphere, Cube, and Cone.
Design Highlights
-
Shape: An abstract class with a method
toString()that provides a textual description of the shape. -
TwoDimensionalShape: Extends
Shapeand declares an abstract methodgetArea(). -
ThreeDimensionalShape: Extends
Shapeand declares abstract methodsgetArea()andgetVolume(). -
Concrete classes override these methods to provide specific calculations based on the shape's geometric formulas.
Polymorphism and Runtime Type Identification
The main program creates an array of Shape references, holding objects of various concrete classes. During processing, the program uses the instanceof operator to identify whether each shape is two- or three-dimensional, then invokes the appropriate methods to display the shape's description, area, and volume where applicable.
Sample Implementation
Below is a simplified implementation illustrating the key classes and the main processing class.
// Abstract base class
public abstract class Shape {
@Override
public abstract String toString();
}
// Two-dimensional shape class
public abstract class TwoDimensionalShape extends Shape {
public abstract double getArea();
}
// Three-dimensional shape class
public abstract class ThreeDimensionalShape extends Shape {
public abstract double getArea();
public abstract double getVolume();
}
// Concrete shape example: Circle
public class Circle extends TwoDimensionalShape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI radius radius;
}
@Override
public String toString() {
return String.format("Circle with radius %.2f", radius);
}
}
// Concrete shape example: Sphere
public class Sphere extends ThreeDimensionalShape {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return 4 Math.PI radius * radius;
}
@Override
public double getVolume() {
return (4.0/3.0) Math.PI Math.pow(radius, 3);
}
@Override
public String toString() {
return String.format("Sphere with radius %.2f", radius);
}
}
// Main class
public class ShapeTest {
public static void main(String[] args) {
Shape[] shapes = {
new Circle(5),
new Sphere(3),
new Rectangle(4, 6),
new Cube(4)
};
for (Shape shape : shapes) {
System.out.println(shape);
if (shape instanceof TwoDimensionalShape) {
System.out.printf("Area: %.2f%n", ((TwoDimensionalShape) shape).getArea());
} else if (shape instanceof ThreeDimensionalShape) {
System.out.printf("Surface Area: %.2f%n", ((ThreeDimensionalShape) shape).getArea());
System.out.printf("Volume: %.2f%n", ((ThreeDimensionalShape) shape).getVolume());
}
System.out.println();
}
}
}
This implementation demonstrates core OOP principles such as inheritance, abstraction, and polymorphism. Additionally, the use of 'instanceof' allows dynamic determination of the shape type, ensuring the correct methods are invoked based on runtime type.
Conclusion
The shape hierarchy exemplifies how object-oriented design enables flexible, extensible, and maintainable code, especially in applications involving diverse geographic or physical entities. Extending this model can include more shape types or incorporate additional properties such as color, border thickness, or material, further enriching the model's realism and utility.
References
- Gaddis, T. (2018). Starting Out with Java: From Control Structures through Data Structures. Pearson.
- Deitel, H. M., & Deitel, P. J. (2014). Java How to Program. Pearson.
- Horstmann, C. S. (2014). Core Java Volume I–Fundamentals. Prentice Hall.
- Liskov, B., ÿThe Design of Hierarchies in Object-Oriented Programming, ACM Computing Surveys, 2012.
- Effective Java, 3rd Edition, Joshua Bloch, Addison-Wesley, 2018.
- Gosling, J., et al. (2005). The Java Language Specification. Oracle.
- Bloch, J. (2008). Effective Java. Addison-Wesley.
- Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
- Larman, C. (2004). Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development. Pearson.
- Oracle. (2023). Java Documentation. https://docs.oracle.com/en/java/