0% found this document useful (0 votes)
21 views

Inheritance

Uploaded by

souvik.it222073
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)
21 views

Inheritance

Uploaded by

souvik.it222073
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
You are on page 1/ 8

Q.

1
class Employee {
String name;
int age;
String address;

Employee(String name, int age, String address) {


this.name = name;
this.age = age;
this.address = address;
}

void displayEmployeeDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " + address);
}
}

class Manager extends Employee {


String department;
double salary;

Manager(String name, int age, String address, String department, double salary) {
super(name, age, address);
this.department = department;
this.salary = salary;
}

void displayManagerDetails() {
displayEmployeeDetails();
System.out.println("Department: " + department);
System.out.println("Salary: " + salary);
}
}

class Worker extends Employee {


int daysWorked;
double dailyWages;
double totalSalary;

Worker(String name, int age, String address, int daysWorked, double dailyWages) {
super(name, age, address);
this.daysWorked = daysWorked;
this.dailyWages = dailyWages;
calculateTotalSalary();
}

void calculateTotalSalary() {
totalSalary = daysWorked * dailyWages;
}

void displayWorkerDetails() {
displayEmployeeDetails();
System.out.println("Days Worked: " + daysWorked);
System.out.println("Daily Wages: " + dailyWages);
System.out.println("Total Salary: " + totalSalary);
}
}
public class Main {
public sta c void main(String[] args) {
Manager manager = new Manager("Harry", 35, "123 Main St", "HR", 900000);
Worker worker = new Worker("Aman", 28, "456 Park Ave", 20, 150);

System.out.println("Manager Details:");
manager.displayManagerDetails();
System.out.println("\nWorker Details:");
worker.displayWorkerDetails();
}
}

Q.2
class Person {
private String name;

public Person(String name) {


this.name = name;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return name != null ? name.equals(person.name) : person.name == null;
}
}
class Employee extends Person {
private double annualSalary;
private int yearStarted;
private String na onalInsuranceNumber;

public Employee(String name, double annualSalary, int yearStarted, String na onalInsuranceNumber) {


super(name);
this.annualSalary = annualSalary;
this.yearStarted = yearStarted;
this.na onalInsuranceNumber = na onalInsuranceNumber;
}

public double getAnnualSalary() {


return annualSalary;
}

public void setAnnualSalary(double annualSalary) {


this.annualSalary = annualSalary;
}

public int getYearStarted() {


return yearStarted;
}

public void setYearStarted(int yearStarted) {


this.yearStarted = yearStarted;
}

public String getNa onalInsuranceNumber() {


return na onalInsuranceNumber;
}

public void setNa onalInsuranceNumber(String na onalInsuranceNumber) {


this.na onalInsuranceNumber = na onalInsuranceNumber;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!super.equals(obj)) return false;
if (getClass() != obj.getClass()) return false;
Employee employee = (Employee) obj;
return Double.compare(employee.annualSalary, annualSalary) == 0 &&
yearStarted == employee.yearStarted &&
(na onalInsuranceNumber != null ? na onalInsuranceNumber.equals(employee.na onalInsuranceNumber) :
employee.na onalInsuranceNumber == null);
}
}

public class EmployeeTest {


public sta c void main(String[] args) {
Employee emp1 = new Employee("Cid Kagenou", 50000.0, 2010, "AB123456C");
Employee emp2 = new Employee("Arthur Leywin", 60000.0, 2015, "XY987654D");
Employee emp3 = new Employee("Sung Jinwoo", 50000.0, 2010, "AB123456C");

System.out.println("Employee 1: " + emp1.getName() + ", Annual Salary: " + emp1.getAnnualSalary() + ", Year Started: " +
emp1.getYearStarted() + ", NI Number: " + emp1.getNa onalInsuranceNumber());
System.out.println("Employee 2: " + emp2.getName() + ", Annual Salary: " + emp2.getAnnualSalary() + ", Year Started: " +
emp2.getYearStarted() + ", NI Number: " + emp2.getNa onalInsuranceNumber());
System.out.println("Employee 3: " + emp3.getName() + ", Annual Salary: " + emp3.getAnnualSalary() + ", Year Started: " +
emp3.getYearStarted() + ", NI Number: " + emp3.getNa onalInsuranceNumber());
System.out.println("Employee 1 equals Employee 2? " + emp1.equals(emp2));
System.out.println("Employee 1 equals Employee 3? " + emp1.equals(emp3));
}
}

Q.3
class Shape {
private String color;
private boolean filled;

public Shape() {
this.color = "green";
this.filled = true;
}

public Shape(String color, boolean filled) {


this.color = color;
this.filled = filled;
}

public String getColor() {


return color;
}

public void setColor(String color) {


this.color = color;
}

public boolean isFilled() {


return filled;
}

public void setFilled(boolean filled) {


this.filled = filled;
}
@Override
public String toString() {
return "A Shape with color of " + color + " and " + (filled ? "filled" : "Not filled");
}
}

class Circle extends Shape {


private double radius;

public Circle() {
this.radius = 1.0;
}

public Circle(double radius) {


super();
this.radius = radius;
}

public Circle(double radius, String color, boolean filled) {


super(color, filled);
this.radius = radius;
}

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}

public double getArea() {


return Math.PI * radius * radius;
}

public double getPerimeter() {


return 2 * Math.PI * radius;
}

@Override
public String toString() {
return "A Circle with radius=" + radius + ", which is a subclass of " + super.toString();
}
}

class Rectangle extends Shape {


private double width;
private double length;

public Rectangle() {
this.width = 1.0;
this.length = 1.0;
}

public Rectangle(double width, double length) {


super();
this.width = width;
this.length = length;
}

public Rectangle(double width, double length, String color, boolean filled) {


super(color, filled);
this.width = width;
this.length = length;
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}

public double getLength() {


return length;
}

public void setLength(double length) {


this.length = length;
}
public double getArea() {
return width * length;
}
public double getPerimeter() {
return 2 * (width + length);
}
@Override
public String toString() {
return "A Rectangle with width=" + width + " and length=" + length + ", which is a subclass of " + super.toString();
}
}

class Square extends Rectangle {

public Square() {
super(1.0, 1.0);
}

public Square(double side) {


super(side, side);
}

public Square(double side, String color, boolean filled) {


super(side, side, color, filled);
}

public double getSide() {


return getWidth();
}
public void setSide(double side) {
setWidth(side);
setLength(side);
}
@Override
public void setWidth(double side) {
super.setWidth(side);
super.setLength(side);
}
@Override
public void setLength(double side) {
super.setLength(side);
super.setWidth(side);
}
@Override
public String toString() {
return "A Square with side=" + getSide() + ", which is a subclass of " + super.toString();
}
}
public class ShapeTest {
public sta c void main(String[] args) {
Shape shape = new Shape();
System.out.println(shape);

shape.setColor("blue");
shape.setFilled(false);
System.out.println(shape);

Circle circle = new Circle(2.5, "red", false);


System.out.println(circle);
System.out.println("Circle Area: " + circle.getArea());
System.out.println("Circle Perimeter: " + circle.getPerimeter());

Rectangle rectangle = new Rectangle(2.0, 4.0, "blue", true);


System.out.println(rectangle);
System.out.println("Rectangle Area: " + rectangle.getArea());
System.out.println("Rectangle Perimeter: " + rectangle.getPerimeter());

Square square = new Square(5.0, "yellow", true);


System.out.println(square);
System.out.println("Square Area: " + square.getArea());
System.out.println("Square Perimeter: " + square.getPerimeter());
}
}

You might also like