0% found this document useful (0 votes)
26 views5 pages

Java Programs for Job and Shapes

The document contains multiple Java classes demonstrating object-oriented programming concepts such as inheritance, abstraction, and method overriding. Key classes include Job and SoftwareDeveloper for job management, Triangle and Circle for geometric calculations, and various abstract classes for banking and marks management. Each class includes a main method to execute and display functionality, showcasing different programming techniques.

Uploaded by

cosmic.sus.73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Java Programs for Job and Shapes

The document contains multiple Java classes demonstrating object-oriented programming concepts such as inheritance, abstraction, and method overriding. Key classes include Job and SoftwareDeveloper for job management, Triangle and Circle for geometric calculations, and various abstract classes for banking and marks management. Each class includes a main method to execute and display functionality, showcasing different programming techniques.

Uploaded by

cosmic.sus.73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Complete Java Programs

Job and SoftwareDeveloper Classes

public class Job {


private double salary;
private int yearsOfExperience;
private String jobPosition;
private String companyName;

public Job(double salary, int yearsOfExperience, String jobPosition, String companyName) {


[Link] = salary;
[Link] = yearsOfExperience;
[Link] = jobPosition;
[Link] = companyName;
}

public void getSalary() {


[Link]("Base Salary: $" + salary);
}

public void displaySummaryDetails() {


[Link]("Position: " + jobPosition + " at " + companyName);
[Link]("Years of Experience: " + yearsOfExperience);
[Link]("Salary: $" + salary);
}
}

class SoftwareDeveloper extends Job {


public SoftwareDeveloper(double salary, int yearsOfExperience, String jobPosition, String companyName) {
super(salary, yearsOfExperience, jobPosition, companyName);
}

@Override
public void getSalary() {
double newSalary = [Link] * (yearsOfExperience > 3 ? 1.3 : 1.05);
[Link]("Updated Salary: $" + newSalary);
}
}

public class Main {


public static void main(String[] args) {
SoftwareDeveloper dev1 = new SoftwareDeveloper(70000, 5, "Senior Developer", "TechCorp");
SoftwareDeveloper dev2 = new SoftwareDeveloper(50000, 2, "Junior Developer", "CodeInc");
SoftwareDeveloper dev3 = new SoftwareDeveloper(60000, 4, "Mid-Level Developer", "Innovatech");

[Link]();
[Link]();

[Link]();
[Link]();
[Link]();
[Link]();
}
}

Triangle Class

public class Triangle {


private double sideA, sideB, sideC;

public Triangle() {
[Link] = 3;
[Link] = 4;
[Link] = 5;
}

public void calculateArea() {


double s = (sideA + sideB + sideC) / 2;
double area = [Link](s * (s - sideA) * (s - sideB) * (s - sideC));
[Link]("Triangle Area: " + area);
}

public void calculatePerimeter() {


double perimeter = sideA + sideB + sideC;
[Link]("Triangle Perimeter: " + perimeter);
}

public static void main(String[] args) {


Triangle t = new Triangle();
[Link]();
[Link]();
}
}

Circle Class

public class Circle {


private double radius;
private final double pi = 3.1416;

public Circle() {
[Link] = 5; // Default radius
}

public void setRadius(double radius) {


[Link] = radius;
}

public void calculateCircumference() {


double circumference = 2 * pi * radius;
[Link]("Circle Circumference: " + circumference);
}

public static void main(String[] args) {


Circle c1 = new Circle();
[Link]();

Circle c2 = new Circle();


[Link](7);
[Link]();
}
}

Abstract Parent Class

abstract class Parent {


abstract void message();
}

class FirstSubclass extends Parent {


void message() {
[Link]("This is the first subclass.");
}
}

class SecondSubclass extends Parent {


void message() {
[Link]("This is the second subclass.");
}
}

public class Main {


public static void main(String[] args) {
Parent obj1 = new FirstSubclass();
Parent obj2 = new SecondSubclass();

[Link]();
[Link]();
}
}

Bank Abstract Class

abstract class Bank {


abstract int getBalance();
}

class BankA extends Bank {


int getBalance() {
return 100;
}
}
class BankB extends Bank {
int getBalance() {
return 150;
}
}

class BankC extends Bank {


int getBalance() {
return 200;
}
}

public class Main {


public static void main(String[] args) {
Bank a = new BankA();
Bank b = new BankB();
Bank c = new BankC();

[Link]("Bank A Balance: $" + [Link]());


[Link]("Bank B Balance: $" + [Link]());
[Link]("Bank C Balance: $" + [Link]());
}
}

Marks Abstract Class

abstract class Marks {


abstract double getPercentage();
}

class StudentA extends Marks {


private double m1, m2, m3;

public StudentA(double m1, double m2, double m3) {


this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
}

double getPercentage() {
return (m1 + m2 + m3) / 3;
}
}

class StudentB extends Marks {


private double m1, m2, m3, m4;

public StudentB(double m1, double m2, double m3, double m4) {


this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
this.m4 = m4;
}
double getPercentage() {
return (m1 + m2 + m3 + m4) / 4;
}
}

public class Main {


public static void main(String[] args) {
Marks studentA = new StudentA(80, 90, 85);
Marks studentB = new StudentB(75, 80, 95, 85);

[Link]("Student A Percentage: " + [Link]() + "%");


[Link]("Student B Percentage: " + [Link]() + "%");
}
}

Shape Abstract Class

abstract class Shape {


abstract void RectangleArea(double length, double breadth);
abstract void SquareArea(double side);
abstract void CircleArea(double radius);
}

class Area extends Shape {


void RectangleArea(double length, double breadth) {
[Link]("Rectangle Area: " + (length * breadth));
}

void SquareArea(double side) {


[Link]("Square Area: " + (side * side));
}

void CircleArea(double radius) {


[Link]("Circle Area: " + (3.1416 * radius * radius));
}

public static void main(String[] args) {


Area a = new Area();
[Link](10, 5);
[Link](4);
[Link](7);
}
}

Common questions

Powered by AI

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 .

You might also like