Question

In: Computer Science

JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...

JAVA:

*USING INHERITANCE

*USING SUPER IN TH CONSTRUCTOR

*USING SUPER IN THE METHOD

*METHOD OVERRIDING

Create an application with 5 classes:

1.) Vehicle

2.) Car

3.) Bus

4.) Truck

5.) Tester

Following characteristics should be used:

make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats,

maxWeightLoad, numberAxels

**Class Vehicle: should have a constructor that initializes all of it's data

**Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data

**Class Vehicle: Should have toString method that returns string representatipn of all vehicle data. Classes Car, Bus, Truck will override the inheritted toString method from class vehicle in order to provide appropriate string representation of all data for their classes which includes inheritted data from Vehicle class and their own data.

** Class Tester will instantiate 1-2 objects from those 4 classes (at least 5 total) and it will display the information about those objects by invoking their toString methods

Solutions

Expert Solution

The java code is as follows:-

class Vehicle //parent class
{
String make;
double weight, height, length, maxSpeed, maxWeightLoad;
int numberDoors, maxPassengers, numberSeats, numberAxels;
boolean isCpnvertable;
Vehicle(String m,double w,double h, double l, double ms, double mwl, int nd, int mp, int ns, int na, boolean ic) //parent constructor
{
make= m;
weight=w;
height=h;
length=l;
maxSpeed=ms;
numberDoors=nd;
maxPassengers=mp;
isCpnvertable=ic;
numberSeats=ns;
maxWeightLoad=mwl;
numberAxels=na;
}
@Override
public String toString() //override toString method
{
return "made by: "+this.make+", weight: "+this.weight+" kg, height: "+this.height+" metre, length: "+this.length+" metre, maxSpeed: "
+this.maxSpeed+" km/h, numberDoors: "+this.numberDoors+", maxPassengers: "+this.maxPassengers+", isCpnvertable: "+this.isCpnvertable+
", numberSeats: "+this.isCpnvertable+", numberSeats: "+this.numberSeats+", maxWeightLoad: "+this.maxWeightLoad+" kg, numberAxels: "+this.numberAxels;
}
}
class Car extends Vehicle //child class
{
String color,type; //additional attributes of child class
Car(String m,double w,double h, double l, double ms, double mwl, int nd, int mp, int ns, int na, boolean ic,String c,String t)
{
super(m, w, h, l, ms, mwl, nd, mp, ns, na, ic);//call constructor of parent class
color=c;
type=t;
}
@Override
public String toString() //override toString method
{
return "Car details:\n"+super.toString()+" color: "+this.color+" type: "+this.type;
}
}
class Bus extends Vehicle //child class
{
int deckers; //additional attributes of child class
Bus(String m,double w,double h, double l, double ms, double mwl, int nd, int mp, int ns, int na, boolean ic,int d)
{
super(m, w, h, l, ms, mwl, nd, mp, ns, na, ic);//call constructor of parent class
deckers=d;
}
@Override
public String toString() //override toString method
{
return "Bus details:\n"+super.toString()+" number of deckers: "+this.deckers;
}
}
class Truck extends Vehicle //child class
{
int tyres; //additional attributes of child class
Truck(String m,double w,double h, double l, double ms, double mwl, int nd, int mp, int ns, int na, boolean ic,int t)
{
super(m, w, h, l, ms, mwl, nd, mp, ns, na, ic);//call constructor of parent class
tyres=t;
}
@Override
public String toString() //override toString method
{
return "Truck details:\n"+super.toString()+" number of tyres used: "+this.tyres;
}
}
public class Main //tester class
{
   public static void main(String[] args)
   {
   //5 objects of parent or child class
   Vehicle v=new Vehicle("TATA",125,1.5,5,100,200,4,5,5,4,true);
   System.out.println("Vehicle details:\n"+v.toString());
       Car c1=new Car("Toyoto",125,1.5,3,120,300,4,7,7,4,true,"blue","small");
       System.out.println(c1.toString());
       Car c2=new Car("Nano",100,1,2,100,300,4,4,4,4,true,"red","small");
       System.out.println(c2.toString());
       Bus b=new Bus("VRL",335,3,5,120,2500,2,50,52,4,true,2);
       System.out.println(b.toString());
       Truck t=new Truck("TATA",535,3.3,7,120,2500,2,5,5,4,true,10);
       System.out.println(t.toString());
   }
}

The output of the above code is as follows:-

Vehicle details:
made by: TATA, weight: 125.0 kg, height: 1.5 metre, length: 5.0 metre, maxSpeed: 100.0 km/h, numberDoors: 4, maxPassengers: 5, isCpnvertable: true, numberSeats: true, numberSeats: 5, maxWeightLoad: 200.0 kg, numberAxels: 4
Car details:
made by: Toyoto, weight: 125.0 kg, height: 1.5 metre, length: 3.0 metre, maxSpeed: 120.0 km/h, numberDoors: 4, maxPassengers: 7, isCpnvertable: true, numberSeats: true, numberSeats: 7, maxWeightLoad: 300.0 kg, numberAxels: 4 color: blue type: small
Car details:
made by: Nano, weight: 100.0 kg, height: 1.0 metre, length: 2.0 metre, maxSpeed: 100.0 km/h, numberDoors: 4, maxPassengers: 4, isCpnvertable: true, numberSeats: true, numberSeats: 4, maxWeightLoad: 300.0 kg, numberAxels: 4 color: red type: small
Bus details:
made by: VRL, weight: 335.0 kg, height: 3.0 metre, length: 5.0 metre, maxSpeed: 120.0 km/h, numberDoors: 2, maxPassengers: 50, isCpnvertable: true, numberSeats: true, numberSeats: 52, maxWeightLoad: 2500.0 kg, numberAxels: 4 number of deckers: 2
Truck details:
made by: TATA, weight: 535.0 kg, height: 3.3 metre, length: 7.0 metre, maxSpeed: 120.0 km/h, numberDoors: 2, maxPassengers: 5, isCpnvertable: true, numberSeats: true, numberSeats: 5, maxWeightLoad: 2500.0 kg, numberAxels: 4 number of tyres used: 10

The screenshot of the above code is as follows:-


Related Solutions

Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should...
Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should consist of 3 classes and will calculate an hourly paycheck.    - The superclass will contain fields for common user data for an employee (first name and last name) and a toString method that returns the formatted output of the names to the calling method.    - The subclass will calculate a paycheck for an hourly employee and will inherit from the superclass.   ...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
How to identify when to use super() in constructor for java language in a easy way?
How to identify when to use super() in constructor for java language in a easy way?
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of using "super" and "this" both as constructors and as variables.
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of using "super" and "this" both as constructors and as variables.**
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT