Java Program to Implement Vehicle Class using Polymorphism | IT2305 - Java Programming Lab

AIM:
To write a Java program for the Implementation of Vehicle Class using Polymorphism in IT2305 - Java Programming Laboratory.

SOURCE CODE:
/**
@author sourcecodesonline.blogspot.com
@version  1.6.0
*/
import java.util.*;
import java.lang.*;
abstract class Vehi
{
 /* id is my private which indicates the id of vehicle*/
 private int id;
 /* permit and fuel are my private which indicates the fueltype and permit of the vehicle*/
 private String permit,fuel;

 /**
 objective        : tis is an abstract method used to compute the charge of individual person and the total charge of the person
  @param nand distance are the parameters used where n indicates the no of person and d indicates the distance covered
  @return no  value
*/
 public abstract void compute(int n,int distance);

/**
 objective        : tis is read method used to read the information
  @param no parameter for this method
    @param  x1 -----String (name of pgm)
  @return no value
*/
 public void read()
 {

  System.out.println("Enter vehicle id,permission and fuel type:");
  Scanner s=new Scanner(System.in);
  id=s.nextInt();
  permit=s.next();
  fuel=s.next();
 }

 /**
 objective        : tis is display method used to display d info
  @param no parameter
  @return no  value
*/
 public void disp()
  {
System.out.println("Vehicle id:"+id+"\nPermission:"+permit+"\nFuel type:"+fuel);
 }
 /**
 objective        : tis is tostring  method used to display d info
  @param no parameter
   @return String
*/
 public String toString()
 {
   return getClass().getName()+"["+id+" "+permit+" "+fuel+"]";
 }
}
/* this is the car class inherited from vehicle class*/
class Car extends Vehi
{
  /* mile is my private which indicates the mileage*/
  private int mile;
  /* fcap is my private which indicates the fuel capacity*/
  private int fcap;
  /* mcap is my private which indicates the maximum capacity*/
  private int mcap;
  /* model is my private  which indicates the model of the car*/
  private String model;
 
  /**
  objective        : tis is compute method used to compute the charge of individual person and total charge
  @param n,distance are the parameters for this method where n indicates the total no of persons and distance indicate the distance covered
  @return doesnt return anything 
  */ 

  public void compute(int n,int distance)
  {
    int charge=(distance*n)/mile;
    int icharge=charge/n;
     System.out.println("Total charge:"+charge+"\nIndividaul charge:"+icharge);
  }

/**
  objective        : tis is read method used to read the information
  @param no para for tis method
  @return doesnt return anything 
  */ 

 public void read()
 {
   super.read();
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Fuel capacity,milage,maximum capacity and model");
   fcap=s.nextInt();
   mile=s.nextInt();
   mcap=s.nextInt();
   model=s.next();
 }

/**
  objective        : tis is disp method used to display d info
  @param no para for tis method
  @return doesnt return anything 
  */ 

 public void disp()
 {
  super.disp();
  System.out.println("Fuel capacity:"+fcap);
  System.out.println("Mileage:"+mile);
  System.out.println("Maximum capacity:"+mcap);
  System.out.println ("Model:"+model);
  }

 /**
  objective        : tis is tostring method used to display d info
  @param no para for tis method
  @return String 
  */ 
 public String toString()
 {
   return super.toString()+"["+fcap+" "+mile+" "+mcap+" "+model+"]";
 }
}

/* this is a bike class inherited from class vehicle*/
class Bike extends Vehi
{
  /* fuelcap is my private which indicates the fuelcapacity*/
  private int fuelcap;
  /* mil is my private which indicates the mileage*/
  private int mil;
  /* cc is my private which indicates the cubic centimeter of the engine*/
  private int cc;
  /* mod is my private which indicates the model of the bike*/
  private String mod;

 /**
  objective        : tis is compute  method used to compute the individual charge and totalcharge
  @param n and distance are the parameters where n indicates the no of persons and distance indicates the distance covered
  @return doesnt return anything 
  */ 

  public void compute(int n,int distance)
  {
   int charge=(distance*n)/mil;
   int icharge=charge/n;
   System.out.println("Total charge:"+charge+"\nIndividual charge:"+icharge);
  }

/**
  objective        : tis is read method used to read d info
  @param no para for tis method
  @return doesnt return anything 
  */ 

 public void read()
  {
    super.read();
    Scanner s=new Scanner(System.in);
    System.out.println("Enter fuel capacity,mileage,cc and model:");
    fuelcap=s.nextInt();
    mil=s.nextInt();
   cc=s.nextInt();
    mod=s.next();
  }

/**
  objective        : tis is display method used to display d info
  @param no para for tis method
  @return doesnt return anything 
  */ 

 public void disp()
 {
   super.disp();
   System.out.println("Fuel capacity:"+fuelcap);
   System.out.println("Mileage:"+mil);
   System.out.println("cc:"+cc);
   System.out.println("Model:"+mod);
 }

/**
  objective        : tis is tostring  method used to display d info
  @param no para for tis method
  @return String
  */ 

 public String toString()
 {
   return super.toString()+"["+fuelcap+" "+mil+" "+cc+" "+mod+"]";
 }
}
public class Vehicle
{
  public static void main(String srgs[])
  {
    Scanner s=new Scanner(System.in);
    int n,distance,c,ch;
    Vehi v;
      do
      {
        System.out.println("Enter no. of people and distance");
        n=s.nextInt();
        distance=s.nextInt();
        if(n>=2)
          ch=1;
          else
          ch=2;
          switch(ch)
         {
           case 1:
            v=new Car();
            break;
           case 2:
            v=new Bike();
            break;
           default:
            v=new Car();
            break;
         }
       v.read();
       v.compute(n,distance);
       v.disp();
       System.out.println(v.toString());
   System.out.println("Do u want to continue:yes-1 no-0");
    c=s.nextInt();
  }while(c==1);
}
}
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: