Java Programs for Job and Shapes
Java Programs for Job and Shapes
The principle demonstrated is inheritance and polymorphism. The Circle class sets up the structure with a default radius and a method to calculate circumference, while the Area class implements the CircleArea method to calculate the area using inheritance from the abstract Shape class. These concepts show polymorphism through the overriding of methods to provide specific implementations (e.g., for area calculation in derived classes) and code reusability by inheriting the common structure of shapes and specific methods tailored to particulars of the shape involved .
Abstract classes in this architecture serve as a foundation for implementing design patterns like Template Method and Factory patterns. They provide a structured framework where core behavior is defined in abstract classes and specific details are fleshed out in subclasses. This supports future extensibility by allowing new subclasses to be created with minimal changes to existing code, adhering to the open-closed principle. Abstract classes also enable a consistent interface while allowing various implementations, facilitating the addition of new functionalities without altering the abstract class itself .
The SoftwareDeveloper class overrides the getSalary method from the Job class to calculate the updated salary. If a developer has more than 3 years of experience, the salary is increased by 30%; otherwise, it's increased by 5%. Specifically, the new salary is computed as 'super.salary * (yearsOfExperience > 3 ? 1.3 : 1.05)'. For a developer with less than or equal to 3 years of experience, the salary is multiplied by 1.05, and for more than 3 years, it is multiplied by 1.3 .
The percentage calculation for StudentA and StudentB is defined in their respective classes, which extend the abstract Marks class. StudentA's percentage is calculated by averaging three marks (m1, m2, m3), resulting in the formula: (m1 + m2 + m3) / 3. In contrast, StudentB's percentage includes four marks (m1, m2, m3, m4), using the formula: (m1 + m2 + m3 + m4) / 4. This indicates that StudentB's calculation takes into account an additional mark, thus varying the implementation of the abstract method getPercentage .
Polymorphism is showcased through the Bank abstract class and its subclasses (BankA, BankB, BankC). Each subclass provides its own implementation of the getBalance method, which returns different balance amounts (100, 150, and 200, respectively). When objects of these subclasses are created and the getBalance method is called, the JVM invokes the subclass-specific method implementation. This exemplifies runtime polymorphism, where the method to be executed is identified during the run time depending on the object's class type .
The Circle class could be extended to include methods such as calculateArea alongside calculateCircumference, and implement setters and getters to manage radius changes dynamically, making it adaptable for different contexts (e.g., graphical applications). Implementing interfaces that define geometric behaviors or extending inheritance to accommodate various shapes (ellipse, sphere) can provide versatility and code reuse. This enables more comprehensive manipulation of circle properties, fostering scalability, and integration with diverse applications (e.g., animations, simulations).
The Bank simulation uses abstraction by defining a Bank abstract class with an abstract method getBalance, which each concrete subclass (BankA, BankB, BankC) implements to provide specific balances. This approach emphasizes abstraction by hiding the details of balance retrieval and only exposing an interface to get the balance value. The trade-off is increased complexity during implementation as new subclasses have to be consistently managed, and each subclass must provide a defined behavior, which could increase the burden of maintenance and reduce flexibility for rapid changes unless the entire hierarchy is reviewed .
The Triangle class calculates its area using Heron's formula by first computing the semi-perimeter 's' as (sideA + sideB + sideC) / 2 and then calculating the area using Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)). The perimeter is simply the sum of its sides: sideA + sideB + sideC. This approach is beneficial as it provides a precise calculation of area applicable to any triangle. However, it is limited for functionality enhancements such as handling invalid triangle dimensions or extending to incorporate additional functionalities like checking for right triangles or different calculation methods for special triangles .
Method overloading in SoftwareDeveloper affects readability by providing different ways to access similarly-named methods depending on context (e.g., getSalary before and after overriding). This can enhance maintenance by encapsulating logic specific to the subclass while keeping the naming consistent, reducing misunderstanding of method purpose. However, overloading too many methods can obscure vital functionalities, requiring developers to closely understand which variant applies, potentially impacting code clarity and necessitating additional documentation .
Keeping the bank balance as an integer return type simplifies the design by avoiding complexities of floating-point arithmetic operations, which can introduce precision errors. However, this design choice might impact real-world applications by limiting the ability to represent cents or smaller denominations in currencies, potentially complicating financial transactions or aggregations involving fractions of a currency. This requires careful consideration of use cases to ensure the design aligns with currency handling needs in financial software, where precision and accuracy are paramount .