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 assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
(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...
c++ programing. Objectives Further practice with classes Use of inheritance Use compositions Define a class called...
c++ programing. Objectives Further practice with classes Use of inheritance Use compositions Define a class called Person with private members name of type string, and money of integer type. The class has the public member functions set(), getMoney(), print(), and a parameterized constructor with default values. Define a class called Patient with private data members name of type string, durationOfTreatment, and TreatmentCost of type integer. The class has the public member functions set(), getCost(), getDuration(), print(), and a parameterized constructor...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep ( i.e., as many levels ) as possible. Specify the instance variables and methods for each class. The private instances variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects...
(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...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT