Write a VehicleDemo.java class that does the following: 1- Creates an instance o
ID: 3784623 • Letter: W
Question
Write a VehicleDemo.java class that does the following: 1- Creates an instance of a Car and an instance of an Airplane class. 2- Assigns values to the name, max_speed and number_of_cylinders instance variables of the Car object and the name, max_speed and number_of_engines instance variables of the Airplane object. 3- Calls all the methods of the Car and Airplane objects that return the values of their instance variables and prints the results. 4- Calls the runningCost(5) of the Car and Airplane objects and prints the result. 5- Calls the maintenanceCost (30.0) of the Car and maintenanceCost (250.0) of the Airplane objects and prints the result. 6- Defines a reference variable of type Vehicle and assigns it to an instance of a Car class. Example: Vehicle v1 = new Car(…); 7- Prints v1. Notice whether the toString() method of the Vehicle class is called or the toString() method of the Car class. Explain why it has been the case. In Object Oriented paradigm, what is this called? need coding in java for all the parts
Explanation / Answer
class VehicleDemo
{
public static void main(String[] args) {
Vechile v1=new Car();
Vechile v2=new Airplane();
v1.setName("Car A");
v2.setName("Airplane A");
v1.setSpeed(100);
v2.setSpeed(1000);
v1.Setcylinders(2);
v2.SetEngines(1);
System.out.println("Name of car is "+v1.getName());
System.out.println("Name of Airplane is "+v2.getName());
System.out.println("Speed of car is "+v1.getSpeed());
System.out.println("Speed of Airplane is "+v2.getSpeed());
System.out.println("Number of cylinders of car is "+v1.getcylinders());
System.out.println("Number of engine of Airplane is "+v1.getEngines());
}
}
class Vechile
{
String name;
float max_speed;
Vechile()
{
}
public void setName(String n)
{
name=n;
}
public void setSpeed(float s)
{
max_speed=s;
}
String getName()
{
return name;
}
float getSpeed()
{
return max_speed;
}
void runningCost(float s)
{
System.out.println("Runing cost of car is "+s);
}
void maintenanceCost(float s)
{
System.out.println("maintenance cost of car is "+s);
}
void Setcylinders (int n)
{
}
int getcylinders ()
{
return 1;
}
void SetEngines (int n)
{
}
int getEngines ()
{
return 1;
}
}
class Car extends Vechile
{
int number_of_cylinders ;
Car()
{
}
public void setName(String n)
{
name=n;
}
String getName()
{
return name;
}
public void setSpeed(float s)
{
max_speed=s;
}
float getSpeed()
{
return max_speed;
}
void Setcylinders (int n)
{
number_of_cylinders =n;
}
int getcylinders ()
{
return number_of_cylinders ;
}
void runningCost(float s)
{
System.out.println("Runing cost of car is "+s);
}
void maintenanceCost(float s)
{
System.out.println("maintenance cost of car is "+s);
}
}
class Airplane extends Vechile
{
int number_of_engines ;
Airplane()
{
}
public void setName(String n)
{
name=n;
}
String getName()
{
return name;
}
void runningCost(float s)
{
System.out.println("Runing cost of Airplane is "+s);
}
void maintenanceCost(float s)
{
System.out.println("maintenance cost of Airplane is "+s);
}
float getSpeed()
{
return max_speed;
}
public void setSpeed(float s)
{
max_speed=s;
}
void SetEngines (int n)
{
number_of_engines =n;
}
int getEngines ()
{
return number_of_engines ;
}
}
===============================================
Output:
akshay@akshay-Inspiron-3537:~$ javac VehicleDemo.java
akshay@akshay-Inspiron-3537:~$ java VehicleDemo
Name of car is Car A
Name of Airplane is Airplane A
Speed of car is 100.0
Speed of Airplane is 1000.0
Number of cylinders of car is 2
Number of engine of Airplane is 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.