8)
class AreaCalculator {
double calculateArea(double radius) {
return Math.PI * radius * radius;
}
double calculateArea(double length, double width) {
return length * width;
}
double calculateArea(double base, double height, boolean isTriangle) {
if (isTriangle) {
return 0.5 * base * height;
}
return 0.0;
}
public static void main(String[] args) {
AreaCalculator calculator = new AreaCalculator();
double circleArea = calculator.calculateArea(5.0);
System.out.println("Area of Circle: " + circleArea);
double rectangleArea = calculator.calculateArea(4.0, 6.0);
System.out.println("Area of Rectangle: " + rectangleArea);
double triangleArea = calculator.calculateArea(4.0, 6.0, true);
System.out.println("Area of Triangle: " + triangleArea);
}
}
9)
class Employee {
String name;
int age;
String phoneNumber;
String address;
double salary;
// Method to print the salary of the employee
void printSalary() {
System.out.println("Salary: " + salary);
}
}
class Officer extends Employee {
String specialization;
// Constructor to initialize Officer details
Officer(String name, int age, String phoneNumber, String address, double
salary, String specialization) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
this.specialization = specialization;
}
// Method to print officer details
void printDetails() {
System.out.println("Officer Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Address: " + address);
System.out.println("Specialization: " + specialization);
printSalary();
System.out.println();
}
}
class Manager extends Employee {
String department;
// Constructor to initialize Manager details
Manager(String name, int age, String phoneNumber, String address, double
salary, String department) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
this.department = department;
}
// Method to print manager details
void printDetails() {
System.out.println("Manager Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Address: " + address);
System.out.println("Department: " + department);
printSalary();
System.out.println();
}
}
public class Company {
public static void main(String[] args) {
// Creating an object of Officer and setting the details
Officer officer = new Officer("John Doe", 35, "123-456-7890", "123 Elm St",
50000.0, "IT");
// Creating an object of Manager and setting the details
Manager manager = new Manager("Jane Smith", 42, "987-654-3210", "456 Oak
St", 75000.0, "HR");
// Printing details of Officer and Manager
officer.printDetails();
manager.printDetails();
}
}
10)import java.util.*;
class Employee{
public void display(){
System.out.println("Name of class is Employee.");
}
public void CalcSalary(){
System.out.println("Salary of employee is 10000.");
}
}
class Engineer extends Employee{
public void calcSalary(){
System.out.println("Salary of employee is 20000.");
}
}
public class pr10{
public static void main(String args[]){
Engineer eng=new Engineer();
eng.display();
eng.calcSalary();
}
}