Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Java Inheritance - Vehicle #37

Closed
ErSKS opened this issue Aug 14, 2017 · 11 comments
Closed

Implement Java Inheritance - Vehicle #37

ErSKS opened this issue Aug 14, 2017 · 11 comments

Comments

@ErSKS
Copy link
Owner

ErSKS commented Aug 14, 2017

Vehicle

  • color
  • noOfGears
  • noOfWheels
  • speed
  • noOfDoors

Car extends Vehicle

  • price
  • controlSpeed() - 120 kmph

Truck extends Vehicle

  • controlSpeed() - 75 kmph
@milan604
Copy link

package vehicletest;

/**
*

  • @author m-lan
    */
    public class VehicleTest {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      Car Ford = new Car();
      Car BMW = new Car();
      Truck Mahindra = new Truck();

      Ford.setColor("Red");
      Ford.setNoOfDoors(4);
      Ford.setNoOfGears(6);
      Ford.setNoOfWheels(4);
      Ford.setPrice(2000000);
      Ford.setSpeed(200);

      BMW.setColor("Blue");
      BMW.setNoOfDoors(2);
      BMW.setNoOfGears(6);
      BMW.setNoOfWheels(4);
      BMW.setPrice(20000000);
      BMW.setSpeed(90);

      Mahindra.setColor("White");
      Mahindra.setNoOfDoors(2);
      Mahindra.setNoOfGears(6);
      Mahindra.setNoOfWheels(12);
      Mahindra.setSpeed(100);

      System.out.println("Car Ford");
      System.out.println("Color: " + Ford.getColor());
      System.out.println("Number of Doors: " + Ford.getNoOfDoors());
      System.out.println("Number of Gears: " + Ford.getNoOfGears());
      System.out.println("Number of Wheels: " + Ford.getNoOfWheels());
      System.out.println("Price: Rs." + Ford.getPrice());
      System.out.println("Speed: " + Ford.getSpeed() + "Kmph");
      System.out.println("");
      System.out.println("Car BMW");
      System.out.println("Color: " + BMW.getColor());
      System.out.println("Number of Doors: " + BMW.getNoOfDoors());
      System.out.println("Number of Gears: " + BMW.getNoOfGears());
      System.out.println("Number of Wheels: " + BMW.getNoOfWheels());
      System.out.println("Price: Rs." + BMW.getPrice());
      System.out.println("Speed: " + BMW.getSpeed() + "Kmph");
      System.out.println("");
      System.out.println("Truck Mahindra");
      System.out.println("Color: " + Mahindra.getColor());
      System.out.println("Number of Doors: " + Mahindra.getNoOfDoors());
      System.out.println("Number of Gears: " + Mahindra.getNoOfGears());
      System.out.println("Number of Wheels: " + Mahindra.getNoOfWheels());
      System.out.println("Speed: " + Mahindra.getSpeed() + "Kmph");

    }

}

Class Vehicle
package vehicletest;

/**
*

  • @author m-lan
    */
    public class Vehicle {

    private String color;
    private int noOfWheels;
    private int noOfGears;
    private int noOfDoors;
    private float speed;

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }

    public float getSpeed() {
    return speed;
    }

    public void setSpeed(float speed) {
    Car Cspeed = new Car();
    Truck Tspeed = new Truck();
    if (this instanceof Car) {
    this.speed = Cspeed.controlSpeed(speed);
    } else {
    this.speed = Tspeed.controlSpeed(speed);
    }

    }

}

Class Car
package vehicletest;

/**
*

  • @author m-lan
    */
    public class Car extends Vehicle {

    private double price;

    public float controlSpeed(float n) {
    if (n <= 0) {
    n = 0;
    } else if (n > 120) {
    n = 120;
    }
    return n;
    }

    public double getPrice() {
    return price;
    }

    public void setPrice(double price) {
    this.price = price;
    }

}

Class Truck
package vehicletest;

/**
*

  • @author m-lan
    */
    public class Truck extends Vehicle {

    public float controlSpeed(float n) {
    if (n <= 0) {
    n = 0;
    } else if (n > 75) {
    n = 75;
    }
    return n;
    }

}

Output
screenshot from 2017-08-14 17-57-09

@jagdish4249
Copy link

package task14august1;

/**
*

  • @author Jagdish Duwal
    */
    public class Task14August1 {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {

      Truck t = new Truck();
      t.setColor("Red");
      t.setNoOfDoors(2);
      t.setNoOfGears(5);
      t.setNoOfWheels(6);
      t.setSpeed(0);
      System.out.println("Truck Description");
      System.out.println("color: "+ t.getColor());
      System.out.println("No of Gears: "+t.getNoOfGears());

      System.out.println("No. of Wheels: "+t.getNoOfWheels());
      System.out.println("speed Now:"+ t.getSpeed());

      System.out.println("Truck Started");
      System.out.println("Truck stopped at : "+ t.controlSpeed(t.getSpeed()));

      Car c = new Car();
      c.setColor("Black");
      c.setNoOfDoors(4);
      c.setNoOfGears(10);
      c.setNoOfWheels(4);
      c.setSpeed(0);
      System.out.println("Car Description");
      System.out.println("Color: "+c.getColor());
      System.out.println("No. of Gears: "+c.getNoOfGears());

      System.out.println("No. of Wheels: "+c.getNoOfWheels());
      System.out.println("Speed Now:"+ c.getSpeed());
      System.out.println("Car Started");
      System.out.println("Car stopped at : "+ t.controlSpeed(t.getSpeed()));

    }

}

class Vehicle
package task14august1;

import javax.swing.JOptionPane;

/**
*

  • @author Jagdish Duwal
    */
    public class Vehicle {
    private String color;
    private int noOfGears=7;
    private int noOfWheels =4;
    private int speed=0;
    private int noOfDoors=4;

    int controlSpeed(int speed ){

     String a;
     a=JOptionPane.showInputDialog("Enter your choice (+ for increase or - for decrease):");
     if(a.equalsIgnoreCase("+"))
      speed++;
     else if(a.equalsIgnoreCase("-")){
         if(speed <= 0)  speed = 0;
         else  speed--;
     }
     else{
         System.out.print("\nInvalid input\n");  
     }
       
     return speed;
    

    }

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public int getSpeed() {
    return speed;
    }

    public void setSpeed(int speed) {
    this.speed = speed;
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }
    }

class Truck
package task14august1;

import javax.swing.JOptionPane;

/**
*

  • @author Jagdish Duwal
    */
    public class Truck extends Vehicle {
    @OverRide
    int controlSpeed( int speed){

     String again;
         do{
             speed =  super.controlSpeed(speed);
              
             if(speed >= 75) {
                 JOptionPane.showMessageDialog(null,"MAXIMUM speed reached");
                 speed = 75;
             
             }
             System.out.printf("speed Now: %d" , speed);
             again = JOptionPane.showInputDialog("Press y to continue");
         
         }while(again.equalsIgnoreCase("y"));
    return speed;
    

    }
    }

class Car
package task14august1;

import javax.swing.JOptionPane;

/**
*

  • @author Jagdish Duwal
    */
    public class Car extends Vehicle {
    private float price;

    public float getPrice() {
    return price;
    }

    public void setPrice(float price) {
    this.price = price;
    }

    @OverRide
    int controlSpeed(int speed){

     String again;
     
         do{
             speed =  super.controlSpeed(speed);
              
             if(speed > 120){
                                 JOptionPane.showMessageDialog(null,"MAXIMUM speed reached");
    
             speed = 120;
             } 
             System.out.printf("speed Now: %d" , speed);
             again = JOptionPane.showInputDialog("Press y to continue");
         
         }while(again.equalsIgnoreCase("y"));
    
     return speed;
    

    }
    }

capture

capture1

@RakenShahi
Copy link

RakenShahi commented Aug 14, 2017

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledetail;

/**
*

  • @author DELL
    */
    public class VechileDetail {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
      Car A = new Car();
      A.setColor("Red");
      A.setNoOfDoors(2);
      A.setNoOfGears(6);
      A.setNoOfWheels(4);
      A.setPrice(50000000);
      A.setSpeed(343);

      System.out.println("CAR A");
      System.out.println("Colour: " + A.getColor());
      System.out.println("No.of Doors : " + A.getNoOfDoors());
      System.out.println("No.of Gears : " + A.getNoOfGears());
      System.out.println("No.of wheels : " + A.getNoOfWheels());
      System.out.println("Price : Rs. " + A.getPrice());
      System.out.println("Speed : " + A.getSpeed() + "Kmph");

      Truck T = new Truck();
      T.setColor("Blue");
      T.setNoOfDoors(2);
      T.setNoOfGears(6);
      T.setNoOfWheels(6);
      T.setControlSpeed(95);

      System.out.println("\nTruck T");
      System.out.println("Colour : " + T.getColor());
      System.out.println("No.of Doors : " + T.getNoOfDoors());
      System.out.println("No.of Gears : " + T.getNoOfGears());
      System.out.println("No.of wheels : " + T.getNoOfWheels());
      System.out.println("Speed : " + T.getControlSpeed() + "Kmph");
      }

}

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledetail;

/**
*

  • @author DELL
    */
    public class Vehicle {

    private String color;
    private int noOfGears;
    private int noOfWheels;
    private float speed;
    private int noOfDoors;

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public float getSpeed() {
    return speed;
    }

    public void setSpeed(float speed) {
    Car As = new Car();
    Truck Ts = new Truck();
    if (this instanceof Car) {
    this.speed = As.setControlSpeed(speed);
    } else if (this instanceof Truck) {
    this.speed = Ts.setControlSpeed(speed);
    }
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }
    }

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledetail;

/**
*

  • @author DELL
    */
    public class Truck extends Vehicle {

    private float controlSpeed;

    public float getControlSpeed() {
    return controlSpeed;
    }

    public float setControlSpeed(float controlSpeed) {

     if (controlSpeed >= 75) {
         controlSpeed = 75;
     } else if (controlSpeed <= 0) {
         controlSpeed = 0;
     }
    
     this.controlSpeed = controlSpeed;
     return controlSpeed;
    

    }

}

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledetail;

/**
*

  • @author DELL
    */
    public class Car extends Vehicle {

    private int price;
    private float controlSpeed;

    public int getPrice() {
    return price;
    }

    public void setPrice(int price) {
    this.price = price;
    }

    public float getControlSpeed() {
    return controlSpeed;
    }

    public float setControlSpeed(float controlSpeed) {
    if (controlSpeed > 120) {
    controlSpeed = 120;
    } else if (controlSpeed <= 0) {
    controlSpeed = 0;
    }
    this.controlSpeed = controlSpeed;
    return controlSpeed;
    //this.controlSpeed = controlSpeed;
    }
    }

OUTPUT
vech

@maheshyakami
Copy link

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package implementvehicle;

/**
*

  • @author mahesh
    */
    public class ImplementVehicle {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
      Car BMW = new Car();
      Car Ford = new Car();
      Car Tesla = new Car();
      Truck Volvo = new Truck();
      Truck Mercedes = new Truck();
      BMW.setColor("Black");
      BMW.setNoOfDoors(2);
      BMW.setNoOfGears(6);
      BMW.setNoOfWheels(4);
      BMW.setPrice(200000);
      BMW.setSpeed(115);
      Ford.setColor("Blue");
      Ford.setNoOfDoors(2);
      Ford.setNoOfGears(6);
      Ford.setNoOfWheels(4);
      Ford.setPrice(150000);
      Ford.setSpeed(100);
      Tesla.setColor("White");
      Tesla.setNoOfDoors(2);
      Tesla.setNoOfGears(6);
      Tesla.setNoOfWheels(4);
      Tesla.setPrice(300000);
      Tesla.setSpeed(200);
      Volvo.setColor("Black");
      Volvo.setNoOfDoors(2);
      Volvo.setNoOfGears(6);
      Volvo.setNoOfWheels(4);
      Volvo.setSpeed(60);
      Mercedes.setColor("Red");
      Mercedes.setNoOfDoors(2);
      Mercedes.setNoOfGears(6);
      Mercedes.setNoOfWheels(4);
      Mercedes.setSpeed(85);
      System.out.println("BMW info: ");
      System.out.println("Color:" + BMW.getColor() + "\tDoors: " + BMW.getNoOfDoors() + "\tGears:" + BMW.getNoOfGears() + "\tWheels: " + BMW.getNoOfWheels() + "\nPrice : €" + BMW.getPrice() + "\tSpeed: " + BMW.getSpeed() + "Kmph\nControl Speed: " + BMW.controlspeed() + "Kmph");
      System.out.println();
      System.out.println("Ford info: ");
      System.out.println("Color:" + Ford.getColor() + "\tDoors: " + Ford.getNoOfDoors() + "\tGears:" + Ford.getNoOfGears() + "\tWheels: " + Ford.getNoOfWheels() + "\nPrice : €" + Ford.getPrice() + "\tSpeed: " + Ford.getSpeed() + "Kmph\nControl Speed: " + Ford.controlspeed() + "Kmph");
      System.out.println();
      System.out.println("Tesla info: ");
      System.out.println("Color:" + Tesla.getColor() + "\tDoors: " + Tesla.getNoOfDoors() + "\tGears:" + Tesla.getNoOfGears() + "\tWheels: " + Tesla.getNoOfWheels() + "\nPrice : €" + Tesla.getPrice() + "\tSpeed: " + Tesla.getSpeed() + "Kmph\nControl Speed: " + Tesla.controlspeed() + "Kmph");
      System.out.println();
      System.out.println("Volvo Truck info: ");
      System.out.println("Color:" + Volvo.getColor() + "\tDoors: " + Volvo.getNoOfDoors() + "\tGears:" + Volvo.getNoOfGears() + "\tWheels: " + Volvo.getNoOfWheels() + "\nSpeed: " + Volvo.getSpeed() + "Kmph\nControl Speed: " + Volvo.controlspeed() + "Kmph");
      System.out.println();
      System.out.println("Mercedes Truck info: ");
      System.out.println("Color:" + Mercedes.getColor() + "\tDoors: " + Mercedes.getNoOfDoors() + "\tGears:" + Mercedes.getNoOfGears() + "\tWheels: " + Mercedes.getNoOfWheels() + "\nSpeed: " + Mercedes.getSpeed() + "Kmph\nControl Speed: " + Mercedes.controlspeed() + "Kmph");
      }

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package implementvehicle;

/**
*

  • @author mahesh
    */
    public class Vehicle {

    String color;
    int noOfWheels, noOfGears, speed, noOfDoors;

    public void setColor(String color) {
    this.color = color;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public void setSpeed(int speed) {
    this.speed = speed;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }

    public String getColor() {
    return color;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public int getSpeed() {
    return speed;
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package implementvehicle;

/**
*

  • @author mahesh
    */
    public class Car extends Vehicle {

    int price;

    public void setPrice(int price) {
    this.price = price;
    }

    public int getPrice() {
    return price;
    }

    int controlspeed() {
    if (speed >= 120) {
    speed = 120;

     } else if (speed < 0) {
         speed = 0;
     }
     return speed;
    

    }
    }
    /*

  • To change this license header, choose License Headers in Project Properties.

  • To change this template file, choose Tools | Templates

  • and open the template in the editor.
    */
    package implementvehicle;

/**
*

  • @author mahesh
    */
    public class Truck extends Vehicle {

    int controlspeed() {
    if (speed >= 75) {
    speed = 75;

     } else if (speed < 0) {
         speed = 0;
     }
     return speed;
    

    }

}
screen shot 2017-08-14 at 12 06 38 pm

@Sudan15423
Copy link

Sudan15423 commented Aug 14, 2017

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package drive;

import static java.lang.Integer.parseInt;
import javax.swing.JOptionPane;

/**
*

  • @author dragon15423
    */
    public class Drive {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
      int a = 20;
      int choice = parseInt(JOptionPane.showInputDialog("Enter your choice: \n1)Drive a car\n2)Drive a truck\n3)Exit"));
      while (choice != 3) {

       switch (choice) {
           case 1:
               Car car = new Car();
               car.setName("Buggati Chiron");
               car.setColor("Black");
               car.setNoOfDoors(2);
               car.setNoOfGears(7);
               car.setNoOfWheels(4);
               car.setPrice(2998000);
               car.setSpeed(0);
               System.out.println("\n::Car information::");
               System.out.println("Name: " + car.getName());
               System.out.println("Color: " + car.getColor());
               System.out.println("Number of doors: " + car.getNoOfDoors());
               System.out.println("Gearbox: " + car.getNoOfGears());
               System.out.println("Number of wheels: " + car.getNoOfWheels());
               System.out.println("Price: $" + car.getPrice());
               System.out.println("Top speed: " + car.getSpeed() + "kmph");
               JOptionPane.showMessageDialog(null, "\nWelcome user");
               outer:
               while (car.getSpeed() >= 0) {
      
                   if (car.getSpeed() >= 120) {
                       JOptionPane.showMessageDialog(null, "Maximum control speed reached!", "Warning", 2);
                       String cha = JOptionPane.showInputDialog("Press D to decelerate::");
                       if (cha.equals("D")) {
                           car.controlSpeed(a, cha);
                           System.out.println(car.getSpeed());
                       }
                   } else if (car.getSpeed() == 0) {
                       JOptionPane.showMessageDialog(null, "Car has stopped!", "Warning", 2);
                       String cha = JOptionPane.showInputDialog("Press A to accelerate::");
                       if (cha.equals("A")) {
                           car.controlSpeed(a, cha);
                           System.out.println(car.getSpeed());
                       }
                   } else {
                       String ch = JOptionPane.showInputDialog("Press A to accelerate, D to decelerate::");
                       if (ch.equals("A")) {
                           car.controlSpeed(a, ch);
                           System.out.println(car.getSpeed());
                       } else if (ch.equals("D")) {
                           car.controlSpeed(a, ch);
                           System.out.println(car.getSpeed());
                       }
                   }
               }
      
               break;
      
           case 2:
               System.out.println("\nTruck");
      
               Truck truck = new Truck();
               truck.setName("BLAZO 31 8X4 Tipper");
               truck.setColor("Indigo");
               truck.setNoOfDoors(2);
               truck.setNoOfGears(9);
               truck.setNoOfWheels(10);
               truck.setSpeed(0);
               System.out.println("Name: " + truck.getName());
               System.out.println("Color: " + truck.getColor());
               System.out.println("Number of doors: " + truck.getNoOfDoors());
               System.out.println("Gearbox: " + truck.getNoOfGears());
               System.out.println("Number of wheels: " + truck.getNoOfWheels());
               System.out.println("Top speed: " + truck.getSpeed() + "kmph");
               JOptionPane.showMessageDialog(null, "\nWelcome user");
               while (truck.getSpeed() >= 0) {
      
                   if (truck.getSpeed() == 80) {
                       JOptionPane.showMessageDialog(null, "Maximum control speed reached!", "Warning", 2);
                       String cha = JOptionPane.showInputDialog("Press D to decelerate::");
                       if (cha.equals("D")) {
                           truck.controlSpeed(a, cha);
                           System.out.println(truck.getSpeed());
                       }
                   } else if (truck.getSpeed() == 0) {
                       JOptionPane.showMessageDialog(null, "Truck has stopped!", "Warning", 2);
                       String cha = JOptionPane.showInputDialog("Press A to accelerate::");
                       if (cha.equals("A")) {
                           truck.controlSpeed(a, cha);
                           System.out.println(truck.getSpeed());
                       }
                   } else {
                       String ch = JOptionPane.showInputDialog("Press A to accelerate, D to decelerate::");
                       if (ch.equals("A")) {
                           truck.controlSpeed(a, ch);
                           System.out.println(truck.getSpeed());
                       } else if (ch.equals("D")) {
                           truck.controlSpeed(a, ch);
                           System.out.println(truck.getSpeed());
                       }
      
                   }
      
               }
               break;
      
           case 3:
               System.exit(0);
               break;
      
           default:
               System.out.println("Wrong choice");
               break;
       }
      

      }

    }

}

Class Vehicle

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package drive;

/**
*

  • @author dragon15423
    */
    public class Vehicle {

    private String color;
    private String name;
    private int noOfGears;

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }
    private int noOfWheels;

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }
    private int speed;

    public int getSpeed() {
    return speed;
    }

    public void setSpeed(int speed) {
    this.speed = speed;
    }
    private int noOfDoors;

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

}

Class Car

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package drive;

/**
*

  • @author dragon15423
    */
    public class Car extends Vehicle {

    private float price;

    public void controlSpeed(int a, String c) {

     if (c.equals("A")) {
         this.setSpeed(this.getSpeed() + a);
     } else {
         this.setSpeed(this.getSpeed() - a);
    
     }
    

    }

    public float getPrice() {
    return price;
    }

    public void setPrice(float price) {
    this.price = price;
    }

}

Class Truck

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package drive;

/**
*

  • @author dragon15423
    */
    public class Truck extends Vehicle {

    public void controlSpeed(int a, String c) {

     if (c.equals("A")) {
         this.setSpeed(this.getSpeed() + a);
     } else {
         this.setSpeed(this.getSpeed() - a);
     }
    

    }

}
choice
car
accelerate
maxspeed
decelerate
stopped

@karmi214
Copy link

karmi214 commented Aug 15, 2017

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleinheritance;

import java.util.Scanner;
import javax.swing.JOptionPane;

/**
*

  • @author Anish
    */
    public class VehicleInheritance {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
      String check = "y";
      while (check.equalsIgnoreCase("y")) {
      String ch = JOptionPane.showInputDialog("1:Drive Car\n2:Drive Truck\nEnter your choice:");
      switch (ch) {
      case "1":
      Car c = new Car();
      c.setColor("Red");
      c.setNoOfDoors(2);
      c.setNoOfGears(6);
      c.setSpeed(0);
      c.setPrice(500000);
      c.setNoOfWheels(4);
      c.setMaxSpeed(120);
      System.out.println("The details of the car are:");
      System.out.println("Color:" + c.getColor());
      System.out.println("No. of doors:" + c.getNoOfDoors());
      System.out.println("No. of Gears:" + c.getNoOfGears());
      System.out.println("No. of wheels:" + c.getNoOfWheels());
      System.out.println("Max speed:" + c.getMaxSpeed() + "Kmph");
      System.out.println("Price:" + c.getPrice());
      c.controlSpeeed();
      break;
      case "2":
      Truck t = new Truck();
      t.setColor("Whilte");
      t.setNoOfDoors(4);
      t.setNoOfGears(8);
      t.setSpeed(0);
      t.setNoOfWheels(6);
      t.setMaxSpeed(75);
      System.out.println("The details of the truck are:");
      System.out.println("Color:" + t.getColor());
      System.out.println("No. of doors:" + t.getNoOfDoors());
      System.out.println("No. of Gears:" + t.getNoOfGears());
      System.out.println("No. of wheels:" + t.getNoOfWheels());
      System.out.println("Max speed:" + t.getMaxSpeed() + "Kmph");
      t.controlSpeeed();
      break;
      default:
      JOptionPane.showMessageDialog(null, "Incorrect input", "", 0);
      break;
      }
      check = JOptionPane.showInputDialog("Do you want to continue??(Y/N)");
      }
      }

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleinheritance;

/**
*

  • @author Anish
    */
    public class Car extends Vehicle {

    private int price;

    public int getPrice() {
    return price;
    }

    public void setPrice(int price) {
    this.price = price;
    }

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleinheritance;

/**
*

  • @author Anish
    */
    public class Truck extends Vehicle {

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleinheritance;

import javax.swing.JOptionPane;

/**
*

  • @author Anish
    */
    public class Vehicle {

    private String color;
    private int noOfWheels;
    private int noOfGears;
    private int speed;
    private int noOfDoors;
    private int maxSpeed;

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public int getSpeed() {
    return speed;
    }

    public void setSpeed(int speed) {
    this.speed = speed;
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }

    public static boolean isNumeric(String check) {
    if (check.charAt(0) == '-' || check.charAt(0) == '+') {
    return isNumeric(check.substring(1));
    }

     for (int i = 0; i < check.length(); i++) {
         char c = check.charAt(i);
         if (!Character.isDigit(c)) {
             return false;
         }
     }
     return true;
    

    }

    void controlSpeeed() {
    String check = JOptionPane.showInputDialog("Enter your operation\n"Exit"-To exit to previous menu\nPositive Number-To increase speed\nNegative number to decrease speed");

     while (!check.equalsIgnoreCase("exit")) {
         if (isNumeric(check)) {
             int s = Integer.parseInt(check);
             if (s > this.getMaxSpeed()) {
                 JOptionPane.showMessageDialog(null, "You have entered speed greater than maximum speed!!!", "Warning", 0);
             } else if ((this.getSpeed() + s) > this.getMaxSpeed()) {
                 JOptionPane.showMessageDialog(null, "Maximum speed reached!!!", "Warning", 0);
                 this.setSpeed(this.getMaxSpeed());
             } else if (s < 0 && this.getSpeed() < (s * -1)) {
                 JOptionPane.showMessageDialog(null, "You have entered speed less than current speed!!!", "Warning", 0);
                 this.setSpeed(0);
             } else {
                 this.setSpeed(this.getSpeed() + s);
             }
         } else {
             JOptionPane.showMessageDialog(null, "You have entered invalid operation", "Error!!!", 0);
         }
         System.out.println("The speed of the vehicle is:" + this.getSpeed() + "Kmph");
    
         check = JOptionPane.showInputDialog("Enter your operation\n\"Exit\"-To exit to previous menu\nPositive Number-To increase speed\nNegative number to decrease speed");
     }
    

    }

    public int getMaxSpeed() {
    return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
    this.maxSpeed = maxSpeed;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8

@sajanbasnet75
Copy link

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledrive;

import javax.swing.JOptionPane;

/**
*

  • @author LORDsajan
    */
    public class VechileDrive {

    static int ControlSpeed(int n, String type) {

     String inp_spd = JOptionPane.showInputDialog("Enter the speed of vechile");
     int inp_sp = Integer.parseInt(inp_spd);
     int tot_speed = inp_sp;
     int max_speed = 0;
     if (type.equalsIgnoreCase("car")) {
         max_speed = 120;
     }
     if (type.equalsIgnoreCase("truck")) {
         max_speed = 75;
     }
     if (inp_sp > max_speed) {
         JOptionPane.showMessageDialog(null, "Speed can be (0-" + max_speed + ")", "Oops", 0);
     }
     while (inp_sp < max_speed) {
         String spd;
         spd = JOptionPane.showInputDialog("Control the speed of vechile\n + to increase\n - to decrease\n press E key to exit");
         boolean matches = spd.matches("[-,+,e,E]");
         if (matches == false) {
             JOptionPane.showMessageDialog(null, "Use only + and -", "Invalid", 0);
             break;
         }
         if ("+".equals(spd)) {
             tot_speed = 10 + inp_sp;
             System.out.println(tot_speed);
         }
         if ("-".equals(spd)) {
             tot_speed = inp_sp - 10;
             System.out.println(tot_speed);
             if (tot_speed <= 0) {
                 JOptionPane.showMessageDialog(null, "Vechile has stopped", null, 1);
                 break;
             }
         }
         if (spd.equalsIgnoreCase("e")) {
             break;
         }
         inp_sp = tot_speed;
         if (tot_speed >= max_speed) {
             JOptionPane.showMessageDialog(null, "Max speed reached", null, 1);
             break;
         }
     }
     return 0;
    

    }

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      String choice;
      choice = JOptionPane.showInputDialog("Enter your choice of vechile\n 1. Car\n 2. Truck");
      int c = Integer.parseInt(choice);
      switch (c) {
      case 1:
      System.out.println(".......CAR.......");
      Car car = new Car();

           car.setColor("Black");
           System.out.println("Color: " + car.getColor());
           car.setName("Merceedez");
           System.out.println("Name: " + car.getName());
           car.setWheels(4);
           System.out.println("Wheels: " + car.getWheels());
           car.setWindows(4);
           System.out.println("Windows: " + car.getWindows());
           car.setPirice(2000000);
           System.out.println("Price: " + car.getPirice());
           car.setSpeed_car(120);
           System.out.println("Max speed of car: " + car.getSpeed_car());
           int sped = 0;
           String type = "car";
           ControlSpeed(sped, type);
           break;
       case 2:
           System.out.println(".......TRUCK.......");
           Truck truck = new Truck();
           truck.setColor("Blue");
           System.out.println("Color: " + truck.getColor());
           truck.setName("Maruti");
           System.out.println("Name: " + truck.getName());
           truck.setWheels(6);
           System.out.println("Wheels: " + truck.getWheels());
           truck.setWindows(2);
           System.out.println("Windows: " + truck.getWindows());
           truck.setSpeed_truck(75);
           System.out.println("Max speed of truck: " + truck.getSpeed_truck());
           int tru_sped = 0;
           String tru_type = "truck";
           ControlSpeed(tru_sped, tru_type);
           break;
       default:
           System.out.println("invalid");
           break;
      

      }
      }
      }

Class Vechile
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledrive;

/**
*

  • @author LORDsajan
    */
    public class Vechile {

    private String name;
    private String color;
    private int wheels;
    private int windows;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getWheels() {
    return wheels;
    }

    public void setWheels(int wheels) {
    this.wheels = wheels;
    }

    public int getWindows() {
    return windows;
    }

    public void setWindows(int windows) {
    this.windows = windows;
    }

}

Class Car
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledrive;

/**
*

  • @author LORDsajan
    */
    public class Car extends Vechile {

    private float pirice;
    private int speed_car;
    private String type;
    public float getPirice() {
    return pirice;
    }

    public void setPirice(float pirice) {
    this.pirice = pirice;
    }

    public int getSpeed_car() {
    return speed_car;
    }

    public void setSpeed_car(int speed_car) {
    this.speed_car = speed_car;
    }

}

Class Truck
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vechiledrive;

/**
*

  • @author LORDsajan
    */
    public class Truck extends Vechile {

    private int speed_truck;
    private String type;

    public int getSpeed_truck() {
    return speed_truck;
    }

    public void setSpeed_truck(int speed_truck) {
    this.speed_truck = speed_truck;
    }

}

1

2
n4w
4
5
6

@kajalmaharjan
Copy link

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package inheritance5;

import static java.lang.Float.parseFloat;
import static java.lang.Integer.parseInt;
import javax.swing.JOptionPane;

/**
*

  • @author som
    */
    public class Inheritance5 {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
      int choice;
      String check = "y";
      while (check.equalsIgnoreCase("y")) {
      choice = parseInt(JOptionPane.showInputDialog("Enter your choice:\n1.Car\n2.Truck"));
      switch (choice) {
      case 1:
      Car a = new Car();
      a.setColor(JOptionPane.showInputDialog("Enter the color of the car:"));
      a.setNoOfDoors(parseInt(JOptionPane.showInputDialog("Enter the number of doors:")));
      a.setNoOfGears(parseInt(JOptionPane.showInputDialog("Enter the number of gears:")));
      a.setNoOfWheels(parseInt(JOptionPane.showInputDialog("Enter the number of wheels:")));
      a.setPrice(parseInt(JOptionPane.showInputDialog("Enter the number of Price:")));

               JOptionPane.showMessageDialog(null, "Car Info\n" + "color: " + a.getColor() + "\nNo. of doors: " + a.getNoOfDoors() + "\nNo. of gears: " + a.getNoOfGears() + "\nNo. of wheels: " + a.getNoOfWheels() + "\nPrice: " + a.getPrice());
               String c = "y";
               do {
      
                   a.setSpeed(parseFloat(JOptionPane.showInputDialog("Enter the '+' for acceleration and '-' for brake :")));
                   a.controlSpeed(a.getSpeed());
                   c = JOptionPane.showInputDialog("Do you want to increase or decrease the speed?(y/n)");
               } while (c.equalsIgnoreCase("y"));
               break;
      
           case 2:
               Truck b = new Truck();
               b.setColor(JOptionPane.showInputDialog("Enter the color of the car:"));
               b.setNoOfDoors(parseInt(JOptionPane.showInputDialog("Enter the number of doors:")));
               b.setNoOfGears(parseInt(JOptionPane.showInputDialog("Enter the number of gears:")));
               b.setNoOfWheels(parseInt(JOptionPane.showInputDialog("Enter the number of wheels:")));
               b.setPrice(parseInt(JOptionPane.showInputDialog("Enter the number of Price:")));
      
               JOptionPane.showMessageDialog(null, "Truck Info\n" + "color:" + b.getColor() + "\nNo. of doors: " + b.getNoOfDoors() + "\nNo. of gears: " + b.getNoOfGears() + "\nNo. of wheels: " + b.getNoOfWheels() + "\nPrice: " + b.getPrice());
      
               String d = "y";
               do {
      
                   b.setSpeed(parseFloat(JOptionPane.showInputDialog("Enter the '+' for acceleration and '-' for brake :")));
                   b.controlSpeed(b.getSpeed());
                   d = JOptionPane.showInputDialog("do you want to increase ar decrease the speed?(y/n)");
               } while (d.equalsIgnoreCase("y"));
               break;
      
           default:
               JOptionPane.showMessageDialog(null, "Incorrect input", "", 0);
               break;
       }
       check = JOptionPane.showInputDialog("do you want make another choice(y/n)");
      

      }

    }
    }
    /*

  • To change this license header, choose License Headers in Project Properties.

  • To change this template file, choose Tools | Templates

  • and open the template in the editor.
    */
    package inheritance5;

import javax.swing.JOptionPane;

/**
*

  • @author som
    */
    public class Vehicle {

    private String color;
    private int noOfGears;
    private int noOfWheels;
    private int noOfDoors;
    private float speed;

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }

    public float getSpeed() {
    return speed;
    }

    public void setSpeed(float speed) {
    this.speed = speed;
    }

    public static boolean isNumeric(String check) {
    if (check.charAt(0) == '-' || check.charAt(0) == '+') {
    return isNumeric(check.substring(1));
    }

     for (int i = 0; i < check.length(); i++) {
         char c = check.charAt(i);
         if (!Character.isDigit(c)) {
             return false;
         }
     }
     return true;
    

    }
    }
    /*

  • To change this license header, choose License Headers in Project Properties.

  • To change this template file, choose Tools | Templates

  • and open the template in the editor.
    */
    package inheritance5;

import javax.swing.JOptionPane;

/**
*

  • @author som
    */
    public class Car extends Vehicle {

    private float price;
    float a = 0;

    void controlSpeed(float n) {

     if (n > 120) {
         JOptionPane.showMessageDialog(null, "The speed is maximum");
     } else {
    
         this.setSpeed(n + a);
         a = n + a;
         if (a > 120) {
             JOptionPane.showMessageDialog(null, "Speed limit exceeds", "Warning", 0);
         }
         if (a <= 0) {
             JOptionPane.showMessageDialog(null, "The car has stopped", "Warning", 0);
         } else {
             JOptionPane.showMessageDialog(null, "Current speed: " + this.getSpeed() + " kmph");
         }
     }
    

    }

    public float getPrice() {
    return price;
    }

    public void setPrice(float price) {
    this.price = price;
    }
    }
    /*

  • To change this license header, choose License Headers in Project Properties.

  • To change this template file, choose Tools | Templates

  • and open the template in the editor.
    */
    package inheritance5;

import javax.swing.JOptionPane;

/**
*

  • @author som
    */
    public class Truck extends Vehicle {

    private float price;
    float a = 0;

    void controlSpeed(float n) {

     if (n > 120) {
         JOptionPane.showMessageDialog(null, "The speed is maximum");
     } else {
    
         this.setSpeed(n + a);
         a = n + a;
         if (a > 120) {
             JOptionPane.showMessageDialog(null, "Speed limit exceeds", "Warning", 0);
         }
         if (a <= 0) {
             JOptionPane.showMessageDialog(null, "The truck has stopped", "Warning", 0);
         } else {
             JOptionPane.showMessageDialog(null, "Current speed: " + this.getSpeed() + " kmph");
         }
     }
    

    }

    public float getPrice() {
    return price;
    }

    public void setPrice(float price) {
    this.price = price;
    }
    }
    1
    2
    3
    4
    5
    7

@ajay987
Copy link

ajay987 commented Aug 15, 2017

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleimplementation;

import static java.lang.Integer.parseInt;
import javax.swing.JOptionPane;
import static jdk.nashorn.internal.objects.NativeJava.type;

/**
*

  • @author Dell
    */
    public class VehicleImplementation {

    /**

    • @param args the command line arguments
      */
      static int ControlSpeed(int n, String s) {
      int max = 0;
      if (s.equalsIgnoreCase("truck")) {
      max = 75;
      }

      if (s.equalsIgnoreCase("car")) {
      max = 120;
      }
      int ta = 0;
      String sp = JOptionPane.showInputDialog("Enter speed 0-120 or q to quit");
      if (sp.equalsIgnoreCase("q")) {
      JOptionPane.showMessageDialog(null, "quit!", "WARNING", 2);
      } else {

       int spp = parseInt(sp);
       if (spp == max) {
           JOptionPane.showMessageDialog(null, "  u r already inMax speed", null, 1);
       }
       if (spp > max || spp >= 121 && spp >= 76) {
           JOptionPane.showMessageDialog(null, "warning!not more than 120 for car and 75 for truck", "error", 0);
       }
       while (spp < max) {
           String spd;
           spd = JOptionPane.showInputDialog("speed meter\n a to increase\n d to decrease\n press E key to exit");
           boolean matches = spd.matches("[D,d,a,A,e,E]");
           if (matches == false) {
               JOptionPane.showMessageDialog(null, "Use only a and d", "Invalid", 0);
               break;
           }
           if (spd.equalsIgnoreCase("a")) {
               ta = 10 + spp;
               System.out.println(ta);
           }
           if (spd.equalsIgnoreCase("d")) {
               ta = spp - 10;
               System.out.println(ta);
               if (ta <= 0) {
                   JOptionPane.showMessageDialog(null, "Vechile stopped", null, 1);
                   break;
               }
           }
           if (spd.equalsIgnoreCase("e")) {
               break;
           }
           spp = ta;
           if (ta >= max) {
               JOptionPane.showMessageDialog(null, "Max speed", null, 1);
               break;
           }
      
       }
      

      }

      return 0;
      }

    public static void main(String[] args) {
    // TODO code application logic here
    String st = JOptionPane.showInputDialog("enter choice\n"
    + "1:Car\n"
    + "2:Truck");
    int x = parseInt(st);
    String check = "y";
    //while (check.equalsIgnoreCase("y")) {
    switch (x) {
    case 1:
    String s = "car";
    Car c = new Car();
    c.setColor("blue");
    c.setNoOfDoors(4);
    c.setNoOfGears(5);
    c.setNoOfWheels(4);
    c.setPrice(120000);
    c.setSpeed(120);
    System.out.println("Color:" + c.getColor());
    System.out.println("noOfDoors:" + c.getNoOfDoors());
    System.out.println("noOfGears:" + c.getNoOfGears());
    System.out.println("noOfWheels:" + c.getNoOfWheels());
    System.out.println("Price:" + c.getPrice());
    System.out.println("Max speed:" + c.getSpeed());
    ControlSpeed(0, s);
    break;
    case 2:
    Truck t = new Truck();
    String p = "truck";
    t.setColor("green");
    t.setNoOfDoors(2);
    t.setNoOfGears(4);
    t.setNoOfWheels(8);
    t.setSpeed(75);
    System.out.println("Color:" + t.getColor());
    System.out.println("noOfDoors:" + t.getNoOfDoors());
    System.out.println("noOfGears:" + t.getNoOfGears());
    System.out.println("noOfWheels:" + t.getNoOfWheels());
    System.out.println("Max speed:" + t.getSpeed());
    ControlSpeed(0, p);
    break;
    }
    // check = JOptionPane.showInputDialog("continue?" + "y\t" + "n");

     // }
    

    }

}

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleimplementation;

/**
*

  • @author Dell
    */
    public class vehicle {
    private String color;
    private int noOfWheels;
    private int noOfGears;
    private int noOfDoors;
    private int speed;

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoOfWheels() {
    return noOfWheels;
    }

    public void setNoOfWheels(int noOfWheels) {
    this.noOfWheels = noOfWheels;
    }

    public int getNoOfGears() {
    return noOfGears;
    }

    public void setNoOfGears(int noOfGears) {
    this.noOfGears = noOfGears;
    }

    public int getNoOfDoors() {
    return noOfDoors;
    }

    public void setNoOfDoors(int noOfDoors) {
    this.noOfDoors = noOfDoors;
    }

    public int getSpeed() {
    return speed;
    }

    public void setSpeed(int speed) {
    this.speed = speed;
    }

}

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package vehicleimplementation;

/**
*

  • @author Dell
    */
    public class Car extends vehicle {

    private int price;

    public int getPrice() {
    return price;
    }

    public void setPrice(int price) {
    this.price = price;
    }

}
op1
op2
op3
op4
op5
op6
op7
op8

@duwalshraddha
Copy link

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package day_11;

import java.util.Scanner;

/**
*

  • @author Lenovo
    */
    public class Day_11 {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
      int speed;
      String color, ve;
      int noWheel;
      int noGear;
      int noDoors;
      System.out.println("Which one do you want to choose?");
      Scanner in = new Scanner(System.in);
      ve = in.next();
      if (ve.equals("car")) {

       Car ford = new Car();
       ford.start();
       Car bmw = new Car();
       bmw.start1();
      

      } else {
      Truck t = new Truck();
      t.start2();
      Truck t1 = new Truck();
      t1.start3();
      }

    }

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package day_11;

//import java.util.Scanner;
/**
*

  • @author Lenovo
    */
    public class Vehicle {

    private String color;
    public int noWheel;
    private int noGear;
    int speed;
    private int noDoors;

    void start() {
    System.out.println("Ford");
    setColor("Red");
    setNoWheel(4);
    setNoGear(3);
    setSpeed(150);
    setNoDoors(4);
    //if (getSpeed() == 120) {
    // change_speed();
    //}
    System.out.println("color is" + " " + getColor());
    System.out.println("no of wheels " + " " + getNoWheel());
    System.out.println("no of gears" + " " + getNoGear());
    System.out.println("Speed" + " " + getSpeed());
    System.out.println("no of doors" + " " + getNoDoors());
    System.out.println();

    }

    void start1() {
    System.out.println("BMW");
    setColor("Black");
    setNoWheel(4);
    setNoGear(6);
    setSpeed(170);
    setNoDoors(4);

     System.out.println("color is" + " " + getColor());
     System.out.println("no of wheels " + " " + getNoWheel());
     System.out.println("no of gears" + " " + getNoGear());
     System.out.println("Speed" + " " + getSpeed());
     System.out.println("no of doors" + " " + getNoDoors());
     System.out.println();
    

    }

    void start2() {
    setColor("Blue");
    setNoWheel(4);
    setNoGear(6);
    setSpeed(120);
    setNoDoors(4);

     System.out.println("color is" + " " + getColor());
     System.out.println("no of wheels " + " " + getNoWheel());
     System.out.println("no of gears" + " " + getNoGear());
     System.out.println("Speed" + " " + getSpeed());
     System.out.println("no of doors" + " " + getNoDoors());
     System.out.println();
    

    }

    void start3() {
    setColor("Brown");
    setNoWheel(4);
    setNoGear(5);
    setSpeed(180);
    setNoDoors(4);

     System.out.println("color is" + " " + getColor());
     System.out.println("no of wheels " + " " + getNoWheel());
     System.out.println("no of gears" + " " + getNoGear());
     System.out.println("Speed" + " " + getSpeed());
     System.out.println("no of doors" + " " + getNoDoors());
     System.out.println();
    

    }

    /public int change_speed() {
    System.out.println("speed=" + " " + getSpeed());
    Scanner in = new Scanner(System.in);
    String choice;
    System.out.println("Do you want to increase or decrease?");
    choice = in.next();
    if (choice.equals("increase")) {
    speed = speed + 1;
    System.out.println();
    } else {
    speed = speed - 1;
    System.out.println("speed is " + speed);
    }
    return speed;
    }
    /
    public int getSpeed() {
    if (speed < 0) {
    return 0;
    } else if (speed > 120) {
    return 120;
    } else {
    return speed;
    }
    /System.out.println("speed=" + " " + getSpeed());
    Scanner in = new Scanner(System.in);
    String choice;
    System.out.println("Do you want to increase or decrease?");
    choice = in.next();
    if (choice.equals("increase")) {
    speed = speed + 1;
    System.out.println();
    } else {
    speed = speed - 1;
    }
    /

    }

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getNoWheel() {
    return noWheel;
    }

    public void setNoWheel(int noWheel) {
    this.noWheel = noWheel;
    }

    public int getNoGear() {
    return noGear;
    }

    public void setNoGear(int noGear) {
    this.noGear = noGear;
    }

    public void setSpeed(int speed) {
    //if (speed) {
    this.speed = speed;
    }

    public int getNoDoors() {
    return noDoors;
    }

    public void setNoDoors(int noDoors) {
    this.noDoors = noDoors;
    }

}
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package day_11;

import java.util.Scanner;

/**
*

  • @author Lenovo
    */
    public class Car extends Vehicle {

    private int price;
    private int speed;

    public int getPrice() {
    return price;
    }

    public void setPrice(int price) {
    this.price = price;
    }

    public void setControl_speed(int control_speed) {
    this.speed = control_speed;
    }
    }
    /*

  • To change this license header, choose License Headers in Project Properties.

  • To change this template file, choose Tools | Templates

  • and open the template in the editor.
    */
    package day_11;

/**
*

  • @author Lenovo
    */
    public class Truck extends Vehicle {

    private int control_speed;

    public int getSpeed() {
    if (speed < 0) {
    return 0;
    } else if (speed > 75) {
    return 75;
    } else {
    return speed;
    }
    }

    public void setControl_speed(int speed) {
    this.speed = control_speed;
    }
    }
    1
    2

@ghost
Copy link

ghost commented Aug 16, 2017

package day11task;

import static java.lang.Integer.parseInt;
import javax.swing.JOptionPane;

/**
*

  • @author Samikshya
    */
    public class Day11Task {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) {
      int s, s1, n1, m1, m2, n2, m, n;

      s = parseInt(JOptionPane.showInputDialog("Enter the name of vehicle \n 1:Car \n 2:Truck"));
      CarClass c = new CarClass();
      TruckClass t = new TruckClass();
      switch (s) {
      case 1: {
      // System.out.println("Details"+,c.getColor(),+c.getPrice(),c.getNoOfDoors());
      System.out.println("\n::Car information::");
      System.out.println("Color is: " + c.getColor());
      System.out.println("No of Doors are: " + c.getNoOfDoors());
      System.out.println("No of gears are: " + c.getNoOfGears());
      System.out.println("No of wheels are: " + c.getNoOfWheels());
      System.out.println("Max speed is: " + c.getControlspeed());
      n1 = parseInt(JOptionPane.showInputDialog("Enter the speed of the car in kmph "));

           if (n1 > c.getControlspeed()) {
               JOptionPane.showMessageDialog(null, n1, "Its more than Maximum speed", 2);
           } else if (n1 <= 0) {
               JOptionPane.showMessageDialog(null, n1, "Your Car has Stopped", 0);
           } else {
               JOptionPane.showMessageDialog(null, n1, "Your speed is:", 1);
           }
      
           m1 = parseInt(JOptionPane.showInputDialog("Enter to increse the speed: \n 1:To increase \n 2:To decrease"));
           switch (m1) {
               case 1:
                   m = parseInt(JOptionPane.showInputDialog("Enter the value to increase"));
                   n1 = n1 + m;
                   if (n1 > c.getControlspeed()) {
                       System.out.println("The maximum speed  should be 120");
                   } else {
                       System.out.println("Your speed is" + n1);
                   }
                   break;
      
               case 2:
                   m = parseInt(JOptionPane.showInputDialog("Enter the  negative value to decrease"));
                   n1 = n1 + m;
                   if (m1 < 0) {
                       System.out.println("your car has stopped");
                   } else {
                       System.out.println(" Now Your speed is" + n1);
                   }
           }
      
           break;
      
       }
       case 2: {
           System.out.println("\n::Car information::");
           System.out.println("Color is: " + t.getColor());
           System.out.println("No of Doors are: " + t.getNoOfDoors());
           System.out.println("No of gears are: " + t.getNoOfGears());
           System.out.println("No of wheels are: " + t.getNoOfWheels());
           System.out.println("Max speed is: " + t.getControlspeed());
           n2 = parseInt(JOptionPane.showInputDialog("Enter the speed of the Truck  in kmph"));
      
           if (n2 > 75) {
               JOptionPane.showMessageDialog(null, n2, "Its more than Maximum speed", 2);
           } else if (n2 < 0) {
               JOptionPane.showMessageDialog(null, n2, "Your car has stopped:", 0);
           } else {
               JOptionPane.showMessageDialog(null, n2, "Your speed is:", 1);
           }
      
           m2 = parseInt(JOptionPane.showInputDialog("Enter to increse the speed: \n 1:To increase \n 2:To decrease"));
           switch (m2) {
               case 1:
                   n = parseInt(JOptionPane.showInputDialog("Enter the value to increase"));
                   n2 = n2 + n;
                   if (n2 > c.getControlspeed()) {
                       System.out.println("The maximum speed  should be 120");
                   } else {
                       System.out.println("Your speed is" + n2);
                   }
                   break;
      
               case 2:
                   n = parseInt(JOptionPane.showInputDialog("Enter the  negative value to decrease"));
                   n2 = n2 + n;
                   if (m2 < 0) {
                       System.out.println("your car has stopped");
                   } else {
                       System.out.println(" Now your speed is" + n2);
                   }
           }
      
           break;
      
       }
      

      }
      }

}

public class VechicleClass {

private String color = "White";
private String noOfWheels = "4";
private String noOfGears = "5";
private String speed = "75kmph";
private String noOfDoors = "4";

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public String getNoOfWheels() {
    return noOfWheels;
}

public void setNoOfWheels(String noOfWheels) {
    this.noOfWheels = noOfWheels;
}

public String getNoOfGears() {
    return noOfGears;
}

public void setNoOfGears(String noOfGears) {
    this.noOfGears = noOfGears;
}

public String getSpeed() {
    return speed;
}

public void setSpeed(String speed) {
    this.speed = speed;
}

public String getNoOfDoors() {
    return noOfDoors;
}

public void setNoOfDoors(String noOfDoors) {
    this.noOfDoors = noOfDoors;
}

}
public class CarClass extends VechicleClass {

private double price = 200000;
private double controlspeed = 120;

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public double getControlspeed() {
    return controlspeed;
}

public void setControlspeed(double controlspeed) {
    this.controlspeed = controlspeed;
}

}
public class TruckClass extends VechicleClass {

private double Controlspeed = 75;

public double getControlspeed() {
    return Controlspeed;
}

public void setControlspeed(double Controlspeed) {
    this.Controlspeed = Controlspeed;
}

}
capture
capture2
capture3
capture4
capture5
capture6
capture 7jpg

@ErSKS ErSKS closed this as completed Aug 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests