Question

In: Computer Science

Use inheritance to implement the following classes: A: A Car that is a Vehicle and has...

Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called the number_of_engines it has. Add public methods to set and get the values of these variables. When an airplane is printed (using the toString method), its name, max_speed and number_of_engines are shown. C: Write a VehicleDemo.java class that does the following: 1- Creates an instance of a Car and an Airplane class. 2- Assign values to the name, speed, number_of_cylinders (for the Car object) and number_of_engines (for the Airplane object) variables. 3- Compares which vehicle goes faster and prints the result. 4- Prints the instances of the car and airplane classes.needed coding in java

Solutions

Expert Solution

Vehicle.java

public class Vehicle {
   //Declaring variables
   private String name;
   private int max_speed;
  
   //Parameterized constructor
   public Vehicle(String name, int max_speed) {
       super();
       this.name = name;
       this.max_speed = max_speed;
   }
  
   //Setters and getters
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getMax_speed() {
       return max_speed;
   }
   public void setMax_speed(int max_speed) {
       this.max_speed = max_speed;
   }
   @Override
   public String toString() {
       return "Name=" + name + "\nMax Speed=" + max_speed;
   }
  
   }

______________________

Car.java

public class Car extends Vehicle {

   //Declaring instance variables
   int no_of_cylinders;

  
   //parameterized constructor
   public Car(String name, int max_speed, int no_of_cylinders) {
       super(name, max_speed);
       this.no_of_cylinders = no_of_cylinders;
   }

   //getters and setters
   public int getNo_of_cylinders() {
       return no_of_cylinders;
   }

   public void setNo_of_cylinders(int no_of_cylinders) {
       this.no_of_cylinders = no_of_cylinders;
   }

   //toString() method is used to display the contents of an object inside it
   @Override
   public String toString() {
       System.out.println("\n** CAR **");
       System.out.println(super.toString());
       return "No of cylinders=" + no_of_cylinders;
   }


}

_________________________

Airplane.java

public class Airplane extends Vehicle {
   //Declaring instance variables
   int no_of_engines;

   //parameterized constructor
   public Airplane(String name, int max_speed, int no_of_engines) {
       super(name, max_speed);
       this.no_of_engines = no_of_engines;
   }

   //getters and setters
   public int getNo_of_engines() {
       return no_of_engines;
   }

   public void setNo_of_engines(int no_of_engines) {
       this.no_of_engines = no_of_engines;
   }

   //toString() method is used to display the contents of an object inside it
   @Override
   public String toString() {
       System.out.println("\n** AIRPLANE **");
       System.out.println(super.toString());
       return "No of engines=" + no_of_engines;
   }
  

}

______________________

VehicleDemo.java

public class VehicleDemo {

   public static void main(String[] args) {
       //Creating the references of Vehicle class
       Vehicle car,airplane;
      
       //creating the Car class object
       car=new Car("Ferrari",300,12);
      
       //creating the Airplane class object
       airplane=new Airplane("Boeing 747-8",700,4);
      
      
       //Calling the toString() method on the Car class object
       System.out.println(car.toString());
      
       //Calling the toString() method on the Airplane class object
       System.out.println(airplane.toString());

   }

}

____________________________

output:


** CAR **
Name=Ferrari
Max Speed=300
No of cylinders=12

** AIRPLANE **
Name=Boeing 747-8
Max Speed=700
No of engines=4

________________Thank You


Related Solutions

Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance betweenclasses. You have to write four classes in C++ i.e. one superclass, two sub classes and one driver class. Vehicle is the super class whereas Bus and Truck are sub classesof Vehicle class. Transport is a driver class which contains mainmethod. Detailed description of Vehicle (Super class): The class Vehicle must have following attributes: 1. Vehicle model 2. Registration number 3. Vehicle speed (km/hour)...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and  numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which will...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and two methods (see the classes below for details about what these methods should do): i. getSummary(), ii. getCompetitors() b. Class Game: A game object is associated with a home team, away team, home score, away score, and a date of competition. It should only be created using the following constructor: Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date). A game is also...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an integer à The age of the vehicle       Price, a float à The price of the vehicle       Behaviors: Vehicle() à default constructor sets age=0, and price=0.0 setAge()   à Takes an integer parameter, returns nothing setPrice() à Takes a float parameter, returns nothing getAge()   à Takes no parameters, returns the vehicle’s age getPrice() à Takes no parameters, returns the vehicle’s price End Class Vehicle...
The following classes along with the driver has been created and compiled. public class Car {...
The following classes along with the driver has been created and compiled. public class Car { public void method1() { System.out.println("I am a car object"); } } class Point { public void method2() { System.out.println("I am a Point object"); } } class Animal { public void method3() { System.out.println("I am an animal object"); } } The following driver class has been created and all the lines of code inside the for loop is not compiling. Modify the code and write...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT