Problem: Create a base class called Vehicle that has the manufacturer’s name (typeString), number of cylinders in the engine (type int), and the owner (type Person). Use thePerson class developed this semester. Create a class automobile that is derived fromVehicle and has additional properties: number of passengers (type int) and weight in tons(type double). Create a class Truck which is derived from Vehicle and has additionalproperties: the load capacity in tons (type double, since it may contain a fractional part)and towing capacity in tons (type double). The classes should have:Two constructors, a default and an overloaded constructorAll appropriate accessor and mutator methods (getters and setters).An ‘equals’ method (must conform to the Person example and the Object class ).A ‘toString’ methodA ‘copy’ constructorA ‘clone’ methodA ‘finalize’ methodWrite a driver (client/test) class that tests ALL the methods. Be sure to invoke each of theconstructors, and ALL methods. Demonstrate polymorphism by creating an array ofvarious types of vehicles with varying property values.

Question
Asked Dec 6, 2019
15 views

Problem: Create a base class called Vehicle that has the manufacturer’s name (type

String), number of cylinders in the engine (type int), and the owner (type Person). Use the

Person class developed this semester. Create a class automobile that is derived from

Vehicle and has additional properties: number of passengers (type int) and weight in tons

(type double). Create a class Truck which is derived from Vehicle and has additional

properties: the load capacity in tons (type double, since it may contain a fractional part)

and towing capacity in tons (type double). The classes should have:

  • Two constructors, a default and an overloaded constructor
  • All appropriate accessor and mutator methods (getters and setters).
  • An ‘equals’ method (must conform to the Person example and the Object class ).
  • A ‘toString’ method
  • A ‘copy’ constructor
  • A ‘clone’ method
  • A ‘finalize’ method

Write a driver (client/test) class that tests ALL the methods. Be sure to invoke each of the

constructors, and ALL methods. Demonstrate polymorphism by creating an array of

various types of vehicles with varying property values.

check_circle

Expert Answer

star
star
star
star
star
1 Rating
Step 1

The java program and approach are given below:

Approach:

  • Defining the class “Vehicle” with required methods and constructors. (variables used in this class are properly commented in the program to understand).
  • Defining the class “Person” with required methods and constructors. (variables used in this class are properly commented in the program to understand).
  • Defining the class “Automobile” that extends the class “Vehicle” with required methods and constructors. (variables used in this class are properly commented in the program to understand).
  • Defining the class “Truck” that extends the class “Vehicle” with required methods and constructors. (variables used in this class are properly commented in the program to understand).
  • Defining the class “Main” to test all methods. Array is also used to store the data of vehicles.   
Step 2

Program:

//defining the class Vehicle

class Vehicle

{

    //declaring the variables

    //to store manufacturer’s name

    private String v_n;

    //to store number of cylinders

    private int c;

    //to store owner info (Person class is used as a data-type)

    private Person ow;      

    //parameterised constructor

    public Vehicle(String v_n, int c, Person ow)

    {

        //this keyword to point the current objects

        this.v_n = v_n;

        this.c = c;

        this.ow = ow;

    }

    //getter(accessor) methods

    //to get name of vehicle

    public String getFN()

    {

        return v_n;

    }

    //getter method to get number of cylinders

    public int getC()

    {

        return c;

    }

    //getter method to get owner info

    public Person getOw()

    {

        return ow;

    }

    //equals method of boolean data-type

    //to check the equality of boolean object

    public boolean equals(Vehicle o)

    {

        return(this.v_n == o.getFN() && this.c == o.getC() && this.ow.equals(o.getOw()));

    }

    //overriding the finalize method

    @Override

    protected void finalize()

    {

        System.out.println(" ");

    }

}

 

//defining the class Person

class Person

{

    //declaring the instance variable

    //to store firstName

    private StringBuffer FN; 

    //to store last name

    private StringBuffer LN;

    //to store the social security number

    private StringBuffer SS_NUM;

    //to store age

    private int OLD;

    //setter(mutator) getter (accessor)methods

    //to set firstName

    public void setFN(String st)

    {

        FN = new StringBuffer(st);

    }

    public StringBuffer getFN()

    {

        return FN;

    }

    //to set lastname

    public void setLN(String st)

    {

        LN = new StringBuffer(st);

    }

    public StringBuffer getLastName()

    {

        return LN;

    }

    //to set age

    public void setAge(int a)

    {

        OLD = a;

    }

    public int getAge()

    {

    return OLD;

    }

    //to set social security number

    public void setSs_num(String st)

    {

    SS_NUM = new StringBuffer(st);

    }

    public StringBuffer getSsn()

    {

    return SS_NUM;

    }

    public Person()

    {

        this(" ", " ", 0, " ");

        System.out.println(" ");

    }

    //parameterized constructor

    public Person(String F_N, String L_N, int ag, String s)

    {

        FN = new StringBuffer(F_N);

        LN = new StringBuffer(L_N);

        OLD = ag;

        SS_NUM = new StringBuffer(s);

    }

    //toString method

    public String toString()

    {

        String st = " " + FN + " " + LN + " " + OLD + " " + SS_NUM + " ";

        return st;

    }

    //equals method

    //passing object(of class Object) as an argument

    public boolean equals(Object m)

    {

        //if objects are same

        if (this == m)

        {

            //returns true

            return true;   

        }

        //if the argument passed is null

        if (m == null)

        {

            //returns false

            return false;  

        }

        //if argument not matched

        if (getClass() != m.getClass()) 

        {

            //returns false

            return false;

        }

        //creating the object of class Object

        Person d = (Person) m;

        if ((FN.toString().equals(d.FN.toString())) && (LN.toString().equals(d.LN.toString()))

        && (OLD == d.OLD))

        {

            return true;

        }

        else

        {

            return false;

        }

    }

    //finalize method

    public void finalize()

    {

        System.out.println(" ");

    }

}

 

//defining the class Automobile

//it is derived from class Vehicle

class Automobile extends Vehicle

{

    //declaring the variables

    int num_pas;        //to store number of passengers

    double wt;                //to store weight in tons

    //parameterized constructor

    public Automobile(String n, int c, Person ow, int num_pas, double wt)

    {

        super(n, c, ow);      //super keywor...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: 1:02 PM QWhat is your question? B Consigerrtne two nOstS A aNa B are connected through a direct link...

A: Given data:Bandwidth of the link = 10bits/sec.Propagation speed = 2x10 m/secDistance = 5000 kmConver...

question_answer

Q: What is pseudo code?

A: Pseudocode is used in algorithm and programming based fields to outlines or represents the implement...

question_answer

Q: What are formatted input and output statements?

A: Formatting –The C or C++ language allows a programmer to format an input to process the value for fu...

question_answer

Q: For the following problems assume 1 kilobyte (KB) 1024 kilobytes 1024 bytes and 1 megabyte (MB) For ...

A: Hey, since there are multiple questions posted, we will answer first question. If you want any speci...

question_answer

Q: Simplify the following expression in (1) sum of products and (2) products of sums: a) F = AC’ + B’D+...

A: Simplifying the expressionsThe notation in the Boolean algebra can be simplified by grouping or expa...

question_answer

Q: Most modern TCP implementations use pseudo-random number generators (PRNG) to determine starting seq...

A: The ith number can be generated if (i-1)th number is generated, and thus the entire series can be ge...

question_answer

Q: Simplify the following Boolean function and expression, using four-variable maps  b) F = x’z + w’xy’...

A: (a) F = x’z + w’xy’ + w(x’y + xy’)= x’z + w’xy’ + wx’y + wxy’ Mapping the above expression into the ...

question_answer

Q: To be done in C++ The only part I need done is the portion where it reads an existing students txt a...

A: Programming Instructions:Create a structure with the name ‘student’ and declare all required variabl...

question_answer

Q: If originally x = 4, y = 0, and z = 2, which is the value of x, y and z after executing the followin...

A: Code(in Java)-class Main{            public static void main(String[] args)            {            ...