FSR170823 :- Javascript | Inheritance | OOp

Q.s 1 :- Write a Javascript program to create a vehicle class hierarchy. The base class should be Vehicle, with subclasses Truck, Car and Motorcycle. Each subclass should have properties such as model, year, and fuel type. each class will perform inheritance to take data from main class.

Q.s 2:- Write a Javascript program that creates a class hierarchy for employees of a company. The base class should be Employee, with subclasses Manager, Developer, and Programmer. Each subclass should have properties such as name, address, salary, and job title. Implement methods for calculating bonuses,(give 30-40% salary hike).

class Vehicle{
    constructor(model,year,fuel){
        this.model=model;
        this.year=year;
        this.fuel=fuel;
    }
    p_method(){
        return "model is:-"+ this.model +"year is:-"+this.year+"fuel is ;_"+this.fuel;
    }
}
class Truck extends Vehicle{
    constructor(model,year,fuel){
    super(model,year,fuel);
    return super.p_method();
    }
}
class Car extends Vehicle{
    constructor(model,year,fuel){
        super(model,year,fuel);
        return super.p_method();
       
}}
class MotorCycle extends Vehicle{
    constructor(model,year,fuel){
        super(model,year,fuel);
        return super.p_method();
    }    
}
var x=new Vehicle("hundai","2023","petrol");
document.write(x.p_method());


1 Like

Question 1)
JavaScript

class VeHicle {
    constructor(model, year, fuel) {
        this.model = model;
        this.year = year;
        this.fuel = fuel;
    }
    display() {
        return 'Vechile model is:' + this.model + '\n' + 'Bought in:' + this.year + '\n' + 'Fuel Used is:' + this.fuel;
    }
}

class Truck extends VeHicle {
    constructor(name, model, year, fuel) {
        super(model, year, fuel);
        this.name = name;
    }
    truckDisplay() {
        return 'Vechile: ' + this.name + super.display();
    }
}
class Car extends VeHicle {
    constructor(name, model, year, fuel) {
        super(model, year, fuel);
        this.name = name;
    }
    carDisplay() {
        return 'Vechile: ' + this.name + super.display();
    }
}
class Motorcycle extends VeHicle {
    constructor(name, model, year, fuel) {
        super(model, year, fuel);
        this.name = name;
    }
    motorCycleDisplay() {
        return 'Vechile: ' + this.name + super.display();
    }
}

var v1 = new Truck('Truck', 'Volvo VNL', '2020', 'Diesel');
console.log(v1.truckDisplay());
var v2 = new Car('Car', 'Chevrolet Corvette', '2022', 'EV');
console.log(v2.carDisplay());
var v3 = new Motorcycle('Motor Cycle', 'KTM 390 Duke', '2023', 'Petrol');
console.log(v3.motorCycleDisplay());```

HTML
Community Activity
<script src="5.js" type="text/javascript"></script>
```
1 Like

1.Code:

class Vehicle{
    constructor(brand,model,year,Fuel_type){
        this.veh_brand = brand;
        this.veh_model = model;
        this.veh_year = year;
        this.veh_Fuel_type = Fuel_type;
    }
    veh_brand_model(){
        return "vehicle brand is :- "+this.veh_brand+ " and vehicle model is :- "+this.veh_model+"<br>";
    }
    veh_year_type(){
        return "Vehicle year is :- "+this.veh_year+ " and vehicle type is :- "+this.veh_Fuel_type+"<br>";
    }
}
class Truck extends Vehicle{
    constructor(brand,model,year,Fuel_type){
        super(brand,model,year,Fuel_type)
    }
    Truck_Info(){
        return super.veh_brand_model()+"<br>"+super.veh_year_type();
    }
}
var Vehicle_1 = new Truck("Mahindra","Truck","2022","Lorry");
console.log(Vehicle_1.Truck_Info());

class Car extends Vehicle{
    constructor(brand,model,year,Fuel_type){
        super(brand,model,year,Fuel_type)
    }
    Car_info(){
        return super.veh_brand_model()+"<br>"+super.veh_year_type();
    }
}
var Vehicle_2 = new Car("Audi","A8","2022","Petrol");
console.log(Vehicle_2.Car_info());

class Motorcycle extends Vehicle{
    constructor(brand,model,year,Fuel_type){
        super(brand,model,year,Fuel_type)
    }
    MoterCycle_info(){
        return super.veh_brand_model()+"<br>"+super.veh_year_type();
    }
}
var Vehicle_3 = new Motorcycle("BMW","S1000","2022","Petrol");
console.log(Vehicle_3.MoterCycle_info());
1 Like
class vehicle
{
    constructor(model,year,fuel)
    {
        this.model=model;
        this.year=year;
        this.fuel=fuel;
    }

    vehicle_dets()
    {
        return "Model is "+this.model+"<br>"+"Year of manufacture: "+this.year+"<br>"+"Fuel type :"+this.fuel+"<br>";
    }
}

class car extends vehicle
{
    constructor(model,year,fuel)
    {
        super(model,year,fuel)
    }

    car_dets()
    {
        return super.vehicle_dets();
    }
}

class truck extends vehicle
{
    constructor(model,year,fuel)
    {
        super(model,year,fuel)
    }

    truck_dets()
    {
        return super.vehicle_dets();
    }
}

car1=new car("alto","2002","petrol");
document.write(car1.car_dets());

car2=new car("swift","2012","diesel");
document.write(car2.car_dets())

truck1=new truck("loader","2020","petrol")
document.write(truck1.truck_dets());
1 Like
class vehicle{
    constructor(model,year,fuel){
        this.model = model;
        this.year = year;
        this.fuel = fuel;
    }
    vehicledetails(){
        return "vehicle:" +this.model + "the model is:" +this.year + "the year is:" +this.fuel + "fuel";
    }
}
class Truck extends vehicle{
    constructor(model,year,fuel){
        super(model,year,fuel);
        this.model = model;
    }
    Truckdetails(){
        return super.Truckdetails() +this.model + "the model is:" +this.year + "the year is:" +this.fuel + "fuel";
    }
}
class Car extends vehicle{
    constructor(model,year,fuel){
        super(model,year,fuel);
        this.model = year;
    }
    Cardetails(){
        return super.Cardetails() +this.model + "the model is:" +this.year + "the year is:" +this.fuel + "fuel";
    }
}
class Motorcycle extends vehicle{
    constructor(model,year,fuel){
        super(model,year,fuel);
        this.model = fuel;
    }
    Motorcycledetails(){
        return super.Motorcycledetailsdetails() +this.model + "the model is:" +this.year + "the year is:" +this.fuel + "fuel";
    }
}
1 Like

Question 2 )

class Employee {
    constructor(name, address, salary, jobTitle, hike) {
        this.name = name;
        this.address = address;
        this.salary = salary;
        this.JT = jobTitle;
        this.hike = hike;
    }
    values() {
        return 'Employee Name: ' + this.name + '\n' + 'Employee Address: ' + this.address + '\n' + 'Employee Salary: ' + this.salary +'\n' + 'Employee Role: ' + this.JT;
    }
    bonus() {
        var total = this.salary * this.hike + this.salary;
        return 'Salary After Hike: ' + total;
    }
}

class Manager extends Employee {
    constructor(name, address, salary, jobTitle, hike, department) {
        super(name, address, salary, jobTitle, hike);
        this.dept = department;
    }
    managerDetail() {
        return super.values() + '\n' + 'Employee Department: ' + this.dept + '\n' + super.bonus();
    }
}

class Developer extends Employee {
    constructor(name, address, salary, jobTitle, hike, programmingLanguage) {
        super(name, address, salary, jobTitle, hike);
        this.pl = programmingLanguage;
    }
    developerDetails() {
        return super.values() + '\n' + 'Employee Known Tech Skills: ' + this.pl + '\n' + super.bonus();
    }
}
class Programmer extends Employee {
    constructor(name, address, salary, jobTitle, hike, project) {
        super(name, address, salary, jobTitle, hike);
        this.project = project;
    }
    programmerDetails() {
        return super.values() + '\n' + 'Employee Current Project: ' + this.project + '\n' + super.bonus();
    }
}

var e1 = new Manager('Tom', 'US', 40000, 'Manager', 0.3, 'HR');
console.log(e1.managerDetail());
var e2 = new Developer('Jhon', 'UK', 55000, 'Back End Developer', 0.4, 'PHP');
console.log(e2.developerDetails());
var e3 = new Programmer('Krish','Canada',50000,'Programmer',0.35,'C++,JS');
console.log(e3.programmerDetails());
1 Like

class Vehicle{
constructor(model,year){
this.model=model;
this.year=year;

}
veh_model_year(){
    return "veh model is:- "+this.model+ "and veh species is:- "+this.year+"<br>";
}

}
class Truck extends Vehicle{
constructor(model,year,fuel){
super(model,year);
this.fuel=fuel;
}
Truck_details(){
return super.veh_model_year()+“
”+ “and fuel is:- “+this.fuel+”
”;
}
}
var veh1=new Truck(“Frod F”,“2021”,“Diesel”);
console.log(veh1.Truck_details());

class Car extends Vehicle{
constructor(model,year,fuel){
super(model,year);
this.fuel=fuel;
}
Car_details(){
{
return super.veh_model_year()+“
”+ “and fuel is:- “+this.fuel+”
”;
}
}
}
var veh1=new Car(“Hyundai i20”,“2008”,“petrol”);
console.log(veh1.Car_details());

class Motorcycle extends Vehicle{
constructor(model,year,fuel){
super(model,year);
this.fuel=fuel;
}
Motorcycle_details(){
return super.veh_model_year()+“
”+ “and fuel is:- “+this.fuel+”
”;
}
}
var veh1=new Motorcycle(“Cruiser”,“1960”,“marine diesel”);
console.log(veh1.Motorcycle_details());

1 Like
class Vehicle{
        constructor(Truck,Car,Motorcycle){
            this.veh_Truck= Truck;
            this.veh_Car= Car;
            this.veh_Motorcycle= Motorcycle;  
        }
    
        veh_Truck_Car_Motorcycle(){
            return "veh Truck model is :- "+this.veh_Truck+"veh Car model is :- "+this.veh_Car+"veh Motorcycle model is :- "+this.veh_Motorcycle+"<br>";
    
        }
    
        fuel_info(){
            return "veh Truck fuel is :- "+this.veh_Truck+"veh Car fuel is :- "+this.veh_Car+"veh Motorcycle fuel is :- "+this.veh_Motorcycle+"<br>";
        }

        YEAR_info(){
            return "veh Truck year is :- "+this.veh_Truck+"veh Car year is :- "+this.veh_Car+"veh Motorcycle YEAR is :- "+this.veh_Motorcycle+"<br>";
        }
    
    
    
    }
    class model extends Vehicle{
        constructor(Truck,Car,Motorcycle){
            super(Truck,Car,Motorcycle);
            //this.technology=technology;
        }
        final_reading(){
            return super.veh_Truck_Car_Motorcycle()+"<br>"
        }
    }
    
    var emp1 = new model("TATA PICKUP","TATA NANO","NINZA HR2");
    console.log(emp1.final_reading());
    

    class FUEL extends Vehicle{
        constructor(Truck,Car,Motorcycle){
            super(Truck,Car,Motorcycle);
            //this.technology=technology;
        }
        final_reading(){
            return super.fuel_info()+"<br>"
        }
    }
    
    var emp1 = new model("PETROL","EV BATTERIES","DIESEL");
    console.log(emp1.final_reading());
1 Like

2 Code:

class Employee {
    constructor(name, address, salary, jobTitle) {
      this.name = name;
      this.address = address;
      this.salary = salary;
      this.jobTitle = jobTitle;
    }
  
    calculateBonus() {
      return this.salary * 0.3; // 30% bonus
    }
  
    giveSalaryHike(percent) {
      this.salary += (this.salary * percent) / 100;
    }
  }
  
  class Manager extends Employee {
    constructor(name, address, salary) {
      super(name, address, salary, 'Manager');
    }
  
    calculateBonus() {
      return this.salary * 0.4; // Managers get a 40% bonus
    }
  }
  
  class Developer extends Employee {
    constructor(name, address, salary) {
      super(name, address, salary, 'Developer');
    }
  }
  
  class Programmer extends Employee {
    constructor(name, address, salary) {
      super(name, address, salary, 'Programmer');
    }
  }
  

  const manager = new Manager('Taran', '123 Street', 60000);
  const developer = new Developer('Karan', '456  St', 50000);
  const programmer = new Programmer('Anurag', '789 St', 45000);
  
  console.log(manager);
  console.log(developer);
  console.log(programmer);
  
  console.log('Manager Bonus: $ ' + manager.calculateBonus());
  console.log('Developer Bonus: $ ' + developer.calculateBonus());
  console.log('Programmer Bonus: $ ' + programmer.calculateBonus());
class Vehicle {
    constructor(model, year, fuel_type) {
        this.name = model;
        this.years = year;
        this.fuel = fuel_type;
    }
    vehicleDetails() {
        return 'Vehicle :' + this.name + ' launched in:' + this.years + ' fuel used in this is ' + this.fuel;
    }
}

class truck extends Vehicle {
    constructor(name, years, fuel,) {
        super(name, years, fuel);
    }
    truckDetails() {
        return super.vehicleDetails();
    }
}

class car extends Vehicle {
    constructor(name, years, fuel,) {
        super(name, years, fuel);
    }
    carDetails() {
        return super.vehicleDetails();
    }
}

class motercycle extends Vehicle {
    constructor(name, years, fuel,) {
        super(name, years, fuel);
    }
    motercycleDetails() {
        return super.vehicleDetails();
    }
}

var v1 = new truck('haiwa', '2003', 'desel');
console.log(v1.truckDetails());

var v2 = new car('swift', '2006', 'petrol');
console.log(v2.carDetails());

var v3 = new motercycle('MT15', '2022', 'petrol');
console.log(v3.motercycleDetails());
1 Like
class Vehicle{
    constructor(model, year,fuel_type){
        this.model=model;
        this.year=year;
        this.fuel_type=fuel_type;
    }
    The_model_year_fuel(){
        return "The model is :" + this.model + ", year:" + this.year + "and the fuel type:" + this.fuel_type;
    }
}

class car_fuel extends Vehicle{
    constructor(model, year,fuel_type){
        super(model, year,fuel_type);
    }
    output(){
        return super.The_model_year_fuel() + "<br>";
    }
}
var car= new car_fuel("Bugatti",2010,"petrol");
console.log(car.output());

class Truck_fuel extends Vehicle{
    constructor(model, year,fuel_type){
        super(model, year,fuel_type);
    }
    output(){
        return super.The_model_year_fuel() + "<br>";
    }
}
var Truck= new Truck_fuel("Tanker",2020,"diesel");
console.log(Truck.output());


class Motorcycle_fuel extends Vehicle{
    constructor(model, year,fuel_type){
        super(model, year,fuel_type);
    }
    output(){
        return super.The_model_year_fuel() + "<br>";
    }
}
var Motorcycle= new Motorcycle_fuel("YZF-R1",2022,"unleaded fuel");
console.log(Motorcycle.output());
1 Like

class vehicle{
constructor(model,year,fule){
this.model= model;
this.year= year;
this.fule=fule;

    }
    model_year_fule(){
        return "model is:- "+this.model+","+" year of model is: "+this.year+","+ "fule type is "+this.fule
    }
    
}

class truck extends vehicle{
    constructor(model,year,fule){
                super(model,year,fule);
    }

    final_reading(){
      
            return super. model_year_fule()+"<br>"
    }
            
    
 }
        
        var vehicle1= new truck("eicher","2016","disel");
        console.log(vehicle1. model_year_fule());

        class car extends vehicle{
            constructor(model,year,fule){
                super(model,year,fule);
            }

                final_reading(){
      
                    return super.model_year_fule()+"<br>"
            
            

        }
    }
    var vehicle2= new car("tata_punct",2023,"petrol");
    console.log(vehicle2. model_year_fule());

    class bike extends vehicle{
        constructor(model,year,fule){
            super(model,year,fule);
        }
        final_reading(){
      
            return super.model_year_fule()+"<br>"
    
    

}
}
var vehicle3= new bike("fashion_25",2022,"petrol");
console.log(vehicle3. model_year_fule());

class Vehicle{
    constructor(model,year,fuel_type){
        this.Vehicle_model = model
        this.Vehicle_year = year
        this.Vehicle_type = fuel_type
    }
    V_model(){
        return "model is "+this.Vehicle_model
    }
    V_year(){
        return " manufacturing year of vechicle is "+this.Vehicle_year
    }
    V_type(){
        return " fuel intake is "+this.Vehicle_type
    }
}

class Truck extends Vehicle{
    constructor(model,year,fuel_type,Truckname){
        super(model,year,fuel_type);
        this.Truck = Truckname 
    }
    Trucks(){
        return super.V_model()+super.V_year()+super.V_type()+" Its a HPMV category vechicle, it belongs to "+this.Truck
    }
}

class Car extends Vehicle{
    constructor(model,year,fuel_type,carname){
        super(model,year,fuel_type);
        this.Car = carname 
    }
    cars(){
        return super.V_model()+super.V_year()+super.V_type()+" Its a 4 wheeler named "+this.Car
    }
}

class Motorcycle extends Vehicle{
    constructor(model,year,fuel_type,bikename){
        super(model,year,fuel_type);
        this.Bike = bikename 
    }
    bikes(){
        return super.V_model()+super.V_year()+super.V_type()+" Its a 2 wheeler and belongs to "+this.Bike
    }
}

var vehicle1 = new Truck("407 Gold SFC","2021","Diesel","Tata Company")
console.log(vehicle1.Trucks());

var vehicle2 = new Car("Maruti Baleno","2023","Petrol & CNG","Maruthi")
console.log(vehicle2.cars());

var vehicle3 = new Motorcycle("Speed400","2023","Petrol","Triumph.Co");
console.log(vehicle3.bikes());