ASS3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

12202040501019 Dharmik Rabadiya

Assignment 3
1]Employee and Salary Class

import java.util.Scanner;

class Employee {
String employee_id;
String employee_name;

public Employee(String employee_id, String employee_name) {


this.employee_id = employee_id;
this.employee_name = employee_name;
}
}

class Salary extends Employee {


String designation;
double monthly_salary;

public Salary(String employee_id, String employee_name, String


designation, double monthly_salary) {
super(employee_id, employee_name);
this.designation = designation;
this.monthly_salary = monthly_salary;
}

static void displaySalaryGreaterThan20k(Salary[] employees) {


for (Salary employee : employees) {
if (employee.monthly_salary > 20000) {
System.out.println("Employee ID: " +
employee.employee_id);
System.out.println("Employee Name: " +
employee.employee_name);
System.out.println("Designation: " +
employee.designation);
System.out.println("Monthly Salary: " +
employee.monthly_salary + "\n");
}
}
}

public static void main(String[] args) {


Salary[] employees = new Salary[3];
employees[0] = new Salary("EMP001", "John", "Manager", 25000);
employees[1] = new Salary("EMP002", "Alice", "Software
Developer", 18000);
employees[2] = new Salary("EMP003", "Bob", "Sales Executive",
30000);

System.out.println("Employees with Salary greater than 20000");


displaySalaryGreaterThan20k(employees);
}
}
Shape, Triangle, Rectangle, Circle
12202040501019 Dharmik Rabadiya

2]abstract class Shape {


abstract double area();
}

class Triangle extends Shape {


double base;
double height;

public Triangle(double base, double height) {


this.base = base;
this.height = height;
}
double area() {
return 0.5 * base * height;
}
}

class Rectangle extends Shape {


double width;
double height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}
double area() {
return width * height;
}
}

class Circle extends Shape {


double radius;

public Circle(double radius) {


this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
}

class TestShapes {
public static void main(String[] args) {
Triangle triangle = new Triangle(5, 10);
Rectangle rectangle = new Rectangle(5, 10);
Circle circle = new Circle(5);

System.out.println("Area of Triangle: " + triangle.area());


System.out.println("Area of Rectangle: " + rectangle.area());
System.out.println("Area of Circle: " + circle.area());
}
}

4]class Fan {
final int SLOW = 1;
final int MEDIUM = 2;
12202040501019 Dharmik Rabadiya

final int FAST = 3;

private int speed;


private boolean f_on;
private double radius;
private String color;

public Fan() {
speed = SLOW;
f_on = false;
radius = 4;
color = "blue";
}

public Fan(int speed, boolean f_on, double radius, String color) {


this.speed = speed;
this.f_on = f_on;
this.radius = radius;
this.color = color;
}

void display() {
System.out.println("Speed: " + (f_on ? speed : "Fan is off"));
System.out.println("Color: " + color);
System.out.println("Radius: " + radius);
}

public static void main(String[] args) {


Fan fan1 = new Fan();
Fan fan2 = new Fan(2, true, 6, "brown");

System.out.println("Fan 1:");
fan1.display();
System.out.println("\nFan 2:");
fan2.display();
}
}

5]Rectangle Class

class Rectangle {
double x;
double y;
double width;
double height;

public Rectangle() {
this(0, 0, 1, 1);
}

public Rectangle(double x, double y, double width, double height) {


this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
12202040501019 Dharmik Rabadiya

double getArea() {
return width * height;
}

double getPerimeter() {
return 2 * (width + height);
}

boolean contains(double x, double y) {


return (x >= this.x - width / 2 && x <= this.x + width / 2 && y
>= this.y - height / 2 && y <= this.y + height / 2);
}

public static void main(String[] args) {


Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle(1, 1, 2, 2);

System.out.println("Area of Rectangle 1: " +


rectangle1.getArea());
System.out.println("Perimeter of Rectangle 1: " +
rectangle1.getPerimeter());
System.out.println("Rectangle 1 contains point (0,0): " +
rectangle1.contains(0, 0));

System.out.println("\nArea of Rectangle 2: " +


rectangle2.getArea());
System.out.println("Perimeter of Rectangle 2: " +
rectangle2.getPerimeter());
System.out.println("Rectangle 2 contains point (0,0): " +
rectangle2.contains(0, 0));
}
}

6]Vegetable Hierarchy

class Vegetable {
String color;

public Vegetable(String color) {


this.color = color;
}
public String toString() {
return "Vegetable: " + getClass().getSimpleName() + ", Color: " +
color;
}
}

class Potato extends Vegetable {


public Potato(String color) {
super(color);
}
}

class Brinjal extends Vegetable {


12202040501019 Dharmik Rabadiya

public Brinjal(String color) {


super(color);
}
}

class Tomato extends Vegetable {


public Tomato(String color) {
super(color);
}
}

public class VegetableTest {


public static void main(String[] args) {
Vegetable potato = new Potato("Brown");
Vegetable brinjal = new Brinjal("Purple");
Vegetable tomato = new Tomato("Red");

System.out.println(potato);
System.out.println(brinjal);
System.out.println(tomato);
}
}

7]Employee Hierarchy

import java.util.Date;

abstract class Employee {


String Name;
String Address;
long mob;
float Basic;
float HRA; // 10% of basic
float DA; // 50% of basic
float gross;
Date joining_date;

abstract void getPersonalDetails();

abstract void calculateGross();

abstract void displayDetails();


}

class Principal extends Employee {


float travelling_allowance; // 20% of basic
float book_allowance; // 20%of basic

public Principal() {
this.Name = "Principal";
this.Address = "Address";
this.mob = 1234567890;
this.Basic = 50000;
this.HRA = (10 * Basic) / 100;
this.DA = (50 * Basic) / 100;
12202040501019 Dharmik Rabadiya

this.travelling_allowance = (20 * Basic) / 100;


this.book_allowance = (20 * Basic) / 100;
}
void getPersonalDetails() {
// No need to implement, default values are set in constructor
}
void calculateGross() {
gross = Basic + HRA + DA + travelling_allowance + book_allowance;
}
void displayDetails() {
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Mobile: " + mob);
System.out.println("Basic: " + Basic);
System.out.println("HRA: " + HRA);
System.out.println("DA: " + DA);
System.out.println("Travelling Allowance: " +
travelling_allowance);
System.out.println("Book Allowance: " + book_allowance);
System.out.println("Gross Salary: " + gross);
}
}

class Lecturer extends Employee {


float book_allowance; // 10% of basic

public Lecturer() {
this.Name = "Lecturer";
this.Address = "Address";
this.mob = 1234567890;
this.Basic = 35000;
this.HRA = (10 * Basic) / 100;
this.DA = (50 * Basic) / 100;
this.book_allowance = (10 * Basic) / 100;
}
void getPersonalDetails() {
// No need to implement, default values are set in constructor
}
void calculateGross() {
gross = Basic + HRA + DA + book_allowance;
}
void displayDetails() {
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Mobile: " + mob);
System.out.println("Basic: " + Basic);
System.out.println("HRA: " + HRA);
System.out.println("DA: " + DA);
System.out.println("Book Allowance: " + book_allowance);
System.out.println("Gross Salary: " + gross);
}
}

class Peon extends Employee {


float medical = 100; // 100 rs. fixed

public Peon() {
this.Name = "Peon";
12202040501019 Dharmik Rabadiya

this.Address = "Address";
this.mob = 1234567890;
this.Basic = 10000;
this.HRA = (10 * Basic) / 100;
this.DA = (50 * Basic) / 100;
}

void getPersonalDetails() {
// No need to implement, default values are set in constructor
}
void calculateGross() {
gross = Basic + HRA + DA + medical;
}

@Override
void displayDetails() {
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Mobile: " + mob);
System.out.println("Basic: " + Basic);
System.out.println("HRA: " + HRA);
System.out.println("DA: " + DA);
System.out.println("Medical Allowance: " + medical);
System.out.println("Gross Salary: " + gross);
}
}

class EmployeeTest {
public static void main(String[] args) {
Principal principal = new Principal();
principal.calculateGross();
System.out.println("Principal Details:");
principal.displayDetails();

System.out.println();

Lecturer lecturer = new Lecturer();


lecturer.calculateGross();
System.out.println("Lecturer Details:");
lecturer.displayDetails();

System.out.println();

Peon peon = new Peon();


peon.calculateGross();
System.out.println("Peon Details:");
peon.displayDetails();
}
}

8.1]Match Class Hierarchy

import java.util.Scanner;

class Cricket {
12202040501019 Dharmik Rabadiya

String name;
int age;

public void setData(String name, int age) {


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

public void display() {


System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

class Match extends Cricket {


int no_of_odi;
int no_of_test;

public void setData(String name, int age, int no_of_odi, int


no_of_test) {
super.setData(name, age);
this.no_of_odi = no_of_odi;
this.no_of_test = no_of_test;
}

public void display() {


super.display();
System.out.println("No. of ODIs: " + no_of_odi);
System.out.println("No. of Test Matches: " + no_of_test);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
Match[] matches = new Match[5];
for (int i = 0; i < 5; i++) {
System.out.println("Enter details for Match " + (i + 1));
matches[i] = new Match();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Age: ");
int age = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter No. of ODIs: ");
int no_of_odi = scanner.nextInt();
System.out.print("Enter No. of Test Matches: ");
int no_of_test = scanner.nextInt();
scanner.nextLine(); // Consume newline
matches[i].setData(name, age, no_of_odi, no_of_test);
System.out.println();
}

System.out.println("Match Details:");
for (int i = 0; i < 5; i++) {
System.out.println("\nMatch " + (i + 1) + ":");
matches[i].display();
}
scanner.close();
12202040501019 Dharmik Rabadiya

}
}

8.2Parking System

class Vehicle {
String make;
String model;
int year;

public Vehicle(String make, String model, int year) {


this.make = make;
this.model = model;
this.year = year;
}

public String getMake() {


return make;
}

public void setMake(String make) {


this.make = make;
}

public String getModel() {


return model;
}

public void setModel(String model) {


this.model = model;
}

public int getYear() {


return year;
}

public void setYear(int year) {


this.year = year;
}

public void display() {


System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}

class Car extends Vehicle {


int doors;
String type;

public Car(String make, String model, int year, int doors, String
type) {
super(make, model, year);
this.doors = doors;
12202040501019 Dharmik Rabadiya

this.type = type;
}

public int getDoors() {


return doors;
}

public void setDoors(int doors) {


this.doors = doors;
}

public String getType() {


return type;
}

public void setType(String type) {


this.type = type;
}

@Override
public void display() {
super.display();
System.out.println("Doors: " + doors);
System.out.println("Type: " + type);
}
}

class Motorcycle extends Vehicle {


double engineDisplacement;
String type;

public Motorcycle(String make, String model, int year, double


engineDisplacement, String type) {
super(make, model, year);
this.engineDisplacement = engineDisplacement;
this.type = type;
}

public double getEngineDisplacement() {


return engineDisplacement;
}

public void setEngineDisplacement(double engineDisplacement) {


this.engineDisplacement = engineDisplacement;
}

public String getType() {


return type;
}

public void setType(String type) {


this.type = type;
}

@Override
public void display() {
super.display();
System.out.println("Engine Displacement: " + engineDisplacement);
12202040501019 Dharmik Rabadiya

System.out.println("Type: " + type);


}
}

class ParkingSystem {
public static void main(String[] args) {
Vehicle[] parkingSlots = new Vehicle[5];
parkingSlots[0] = new Car("Toyota", "Camry", 2020, 4, "Sedan");
parkingSlots[1] = new Car("Honda", "Civic", 2018, 4, "Sedan");
parkingSlots[2] = new Motorcycle("Ducati", "Monster", 2019, 1100,
"Sports");
parkingSlots[3] = new Motorcycle("Harley Davidson", "Street",
2021, 750, "Cruiser");
parkingSlots[4] = new Car("Ford", "Fusion", 2017, 4,
"Hatchback");

System.out.println("Parking Slot Details:");


for (int i = 0; i < parkingSlots.length; i++) {
System.out.println("\nParking Slot " + (i + 1) + ":");
parkingSlots[i].display();
}
}
}

You might also like