Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. public static void main(String[] args) {
  2.         Product tab[]=new Product[6];
  3.         Vehicle first=new Vehicle(100000,"suzuki","vitara");
  4.         Vehicle second=new Vehicle(25000,"mercedes","w123");
  5.         Car third=new Car(300,"fiat","ducato","dostawczy");
  6.         Car fourth=new Car(300,"daewoo","nubira","sedan");
  7.         Motorcycle fifth=new Motorcycle(203,"kawasaki","ninja","m");
  8.         Motorcycle sixth=new Motorcycle(200, "suzuki", "burgman", "m");
  9.         tab[0]=first; tab[1]=second; tab[2]=third; tab[3]=fourth; tab[4]=fifth; tab[5]=sixth;
  10.         Vehicle tmp;
  11.         for(int i=2;i<6;i++){
  12.             tmp=(Vehicle)tab[i];
  13.             System.out.println(tmp.getBrand()+" "+tmp.getModel()+" "+tmp.getPrice()+" "+tmp.calculateBargainPrice());
  14.         }
  15.        
  16.     }
  17.    
  18. }
  19.    
  20.        
  21.    
  22. //1.
  23. class  Product {
  24.     double price;
  25.     public Product(double price){
  26.         this.price=price;
  27.     }
  28.     public double calculateBargainPrice(){
  29.         return price;
  30.     };
  31.        
  32.     public double getPrice(){
  33.         return price;
  34.     }
  35. }
  36. //2.
  37. class Vehicle extends Product{
  38.     String brand;
  39.     String model;
  40.     public Vehicle(double price,String brand,String model){
  41.         super(price);
  42.         this.brand=brand;
  43.         this.model=model;
  44.     }
  45.    
  46.     public double calculateBargainPrice(){
  47.         return this.getPrice()*0.95;
  48.     }
  49.     public String getBrand(){
  50.         return brand;
  51.     }
  52.    
  53.     public String getModel(){
  54.         return model;
  55.     }
  56.    
  57.     public void setModel(String model){
  58.         this.model=model;
  59.     }
  60.    
  61.     public void setBrand(String brand){
  62.         this.brand=brand;
  63.     }
  64. }
  65.  
  66. //3.
  67. class Car extends Vehicle{
  68.     String carBody;
  69.     public Car(double price, String brand, String model,String carBody){
  70.         super(price, brand, model);
  71.         this.carBody=carBody;
  72.     }
  73.    
  74.     public double calculateBargainPrice(){
  75.         return this.getPrice()*0.85;
  76.     }
  77.    
  78.     public String getCarBody(){
  79.         return carBody;
  80.     }
  81.    
  82.     public void setCarBody(String carBody){
  83.         this.carBody=carBody;
  84.     }
  85.            
  86.            
  87. }
  88. //4.
  89. class Motorcycle extends Vehicle{
  90.     private String typeOfDrive;
  91.     public Motorcycle(double price, String brand, String model, String typeOfDrive){
  92.         super(price, brand, model);
  93.         this.typeOfDrive=typeOfDrive;
  94.     }
  95.    
  96.     public double calculateBargainPrice(){
  97.         return this.getPrice()*0.9;
  98.     }
  99.  
  100.  
  101.     public String getTypeOfDrive(){
  102.         return typeOfDrive;
  103.     }
  104.  
  105.     public void settypeofDrive(String typeOfDrive){
  106.         this.typeOfDrive=typeOfDrive;
  107.     }
  108.  
  109. //5.
  110.    
  111.     class  Product {
  112.     double price;
  113.     public Product(double price){
  114.         this.price=price;
  115.     }
  116.     public double calculateBargainPrice(){
  117.         return price;
  118.     }
  119.        
  120.    
  121.     public double getPrice(){
  122.         return price;
  123.     }
  124.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement