Advertisement
rafid_shad

Car

Feb 13th, 2019 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package ClassTheory;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Car {
  6.  
  7.     private String model, colour;
  8.  
  9.     private Wheel wheel;
  10.     private Engine e;
  11.     private ArrayList<Passenger> pass = new ArrayList();
  12.  
  13.     public Car(String m, String c, int ch, double ca, int w) {
  14.         e = new Engine(ch, ca);
  15.         wheel = new Wheel(w);
  16.         this.model = m;
  17.         this.colour = c;
  18.     }
  19.  
  20.     public void addPassenger(Passenger p) {
  21.         if (pass.size() < 5) {
  22.             pass.add(p);
  23.         } else {
  24.             System.out.println("Car is full");
  25.         }
  26.     }
  27.  
  28.     public void display() {
  29.         System.out.println("Model name of car is " + model);
  30.         System.out.println("Colour of car is " + colour);
  31.         wheel.diplay();
  32.         e.display();;
  33.         for (Passenger p : pass) {
  34.             p.display();
  35.         }
  36.     }
  37. }
  38.  
  39. package ClassTheory;
  40.  
  41. public class Engine {
  42.     private int chasisNo;
  43.     private double capacity;
  44.     public Engine (int a,double d){
  45.         this.chasisNo=a;
  46.         this.capacity=d;
  47.     }
  48.     public void display(){
  49.         System.out.println("Chasis No = "+chasisNo);
  50.         System.out.println("Capcity = "+capacity);
  51.     }
  52. }
  53. package ClassTheory;
  54.  
  55. public class Wheel {
  56.  
  57.     private int wheelNo;
  58.     public Wheel(int w)
  59.     {
  60.         this.wheelNo=w;
  61.     }
  62.     public void diplay(){
  63.         System.out.println("Wheel No is "+wheelNo);
  64.     }
  65. }
  66.  
  67. package ClassTheory;
  68.  
  69. public class Passenger {
  70.     private String name;
  71.     private int age;
  72.     public Passenger(String s,int a){
  73.         this.name=s;
  74.         this.age=a;
  75.     }
  76.     public void display(){
  77.         System.out.println("Name of passenger is "+name);
  78.         System.out.println("Age of passenger is "+age);
  79.     }
  80. }
  81.  
  82. package ClassTheory;
  83.  
  84. public class Test1 {
  85.     public static void main(String[] args) {
  86.         Car c=new Car("Ford","Black",12345,5,4);
  87.         Passenger p=new Passenger("Rfid",20);
  88.        
  89.         c.addPassenger(p);
  90.        
  91.         c.display();
  92.         System.out.println("");
  93.         Car c1= new Car("BMW","RED",12345,5,4);
  94.         Passenger p2=new Passenger("sagor",18);
  95.         c1.addPassenger(p2);
  96.         c1.display();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement