Question

In: Computer Science

IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...

IN JAVA

Inheritance

Using super in the constructor

Using super in the method

Method overriding

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 reuse their parents constructor and provide additional code for initializing their specific data.

Class Vehicle should have toString method that returns string representtion of all vehicle data. Classes Car, Bus, and Truck will override inherited toString method from class vehicle in order to provide appropriate string representation of all data for their classes which includes inherited data from Vehicle class and their own data.

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

Submit one word document with code for all five classes, picture of program run from blueJ, and picture of UML diagram.

Solutions

Expert Solution

public class Vehicle
{
    /*Start of Vehicle class instance variable declarations*/

    String make = null;
    double weight = 0.0d;
    double height = 0.0d;
    double length = 0.0d;
    double maxSpeed = 0.0d;
    int numberDoors = 0;
    boolean isConvertable = false;
    int numberSeats = 0;
    int numberAxels = 0;

    /*End of Vehicle class instance variable declarations*/

    //Vehical Class No-Argument Constructor
    public Vehicle()
    {
        System.out.println("Enter Default Constructor for Vehicle Class");
    }

    //Vehical Class Parameterized Constructor
    public Vehicle(String make, double weight, double height, double length, double maxSpeed, int numberDoors, boolean isConvertable, int numberSeats, int numberAxels)
    {
        this.make = make;
        this.weight = weight;
        this.height = height;
        this.length = length;
        this.maxSpeed = maxSpeed;
        this.numberDoors = numberDoors;
        this.isConvertable = isConvertable;
        this.numberSeats = numberSeats;
        this.numberAxels = numberAxels;
    }

    //Overriding toString() of Object Class
    public String toString()
    {
        return "Make-"+make+" Corp. : Weight-"+weight+" Kgs : Height-"+height+" meters : Length-"+length+" meters : MaxSpeed-"+maxSpeed+" KMPH : NumberOfDoors-"+numberDoors+" : IsConvertable-"+isConvertable+" : NumberOfSeats-"+numberSeats+" : NumberOfAxels-"+numberAxels;
    }

}

//bus class extending its parent class vehicle

public class Bus extends Vehicle
{
/*Start of Bus class instance variable declarations*/

int maxPassengers = 0;

/*End of Bus class instance variable declarations*/

//Bus Class No-Argument Constructor
public Bus()
{
System.out.println("Enter Default Constructor for Bus Class");
}

//Bus Class Parameterized Constructor
public Bus(String make, double weight, double height, double length, double maxSpeed, int numberDoors, boolean isConvertable, int numberSeats, int numberAxels, int maxPassengers)
{
super(make,weight,height,length,maxSpeed,numberDoors,isConvertable,numberSeats,numberAxels);
this.maxPassengers = maxPassengers;
System.out.println("This is a Bus");
}

//Overriding toString() of Vehicle Class
public String toString()
{
String retString = super.toString();
return retString+" : MaxPassengers-"+maxPassengers;
}

}

//car class exending vehicle

public class Car extends Vehicle
{
/*Start of Car class instance variable declarations*/

int maxPassengers = 0;

/*End of Car class instance variable declarations*/

//Car Class No-Argument Constructor
public Car()
{
System.out.println("Enter Default Constructor for Car Class");
}

//Car Class Parameterized Constructor
public Car(String make, double weight, double height, double length, double maxSpeed, int numberDoors, boolean isConvertable, int numberSeats, int numberAxels, int maxPassengers)
{
super(make,weight,height,length,maxSpeed,numberDoors,isConvertable,numberSeats,numberAxels);
this.maxPassengers = maxPassengers;
System.out.println("This is a Car");
}

//Overriding toString() of Vehicle Class
public String toString()
{
String retString = super.toString();
return retString+" : MaxPassengers-"+maxPassengers;
}
}

//truck class extending vehicle

public class Truck extends Vehicle
{

/*Start of Truck class instance variable declarations*/

double maxWeightLoad = 0.0d;

/*End of Truck class instance variable declarations*/

//Truck Class No-Argument Constructor
public Truck()
{
System.out.println("Enter Default Constructor for Truck Class");
}

//Truck Class Parameterized Constructor
public Truck(String make, double weight, double height, double length, double maxSpeed, int numberDoors, boolean isConvertable, int numberSeats, int numberAxels, double maxWeightLoad)
{
super(make,weight,height,length,maxSpeed,numberDoors,isConvertable,numberSeats,numberAxels);
this.maxWeightLoad = maxWeightLoad;
System.out.println("This is a Truck");
}

//Overriding toString() of Vehicle Class
public String toString()
{
String retString = super.toString();
return retString+" : MaxWeightLoad-"+maxWeightLoad+" Tonns";
}
}

//tester class with main method


public class Testor
{
public Testor()
{
System.out.println("Enter Default Constructor for Testor Class");
}
public static void main(String[] args)
{
Vehicle vehicle1 = new Car("Mercedez AMG Benz",1050,1.5,4,220.3,4,false,4,2,4);
System.out.println(vehicle1.toString());
Vehicle vehicle2 = new Bus("Tata Marcopolo",2050,2.1,7,100.4,3,true,72,3,72);
System.out.println(vehicle2.toString());
Vehicle vehicle3 = new Truck("Ashok Layland",1980,2.2,9.8,80.7,3,false,4,3,72);
System.out.println(vehicle3.toString());
}
}

//uml diagram

//Result of the code


Related Solutions

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...
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.   ...
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.
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...
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?
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.**
- 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...
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...
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT