Ask Question
16 June, 18:17

Write the code that creates a public class named Vehicle that has one private data member. The private data member is an integer variable named modelYear. Create two constructors: Code a constructor that has no parameters and initializes the modelYear variable to 2018. Code a constructor that has one parameter and uses that parameter value to update the modelYear variable.

+1
Answers (1)
  1. 16 June, 19:17
    0
    Following are the program in java

    public class Vehicle / / class vehicle of type public

    {

    private int modelYear;

    Vehicle () / / no parameters

    {

    modelYear=2018;

    }

    vehicle (int t) / / update the value of modelYear

    {

    modelYear=t;

    }

    public static void main (String args[]) / / main function

    {

    vehicle ob=new vehicle (); / / creating instance

    vehicle ob1=new vehicle (45);

    }

    }

    Explanation:

    Here we create a private data member i. e modelYear of integer type.

    We create default constructor and initializes modelYear variable with 2018.

    we create a parametrized constructor which update the value of modelYear variable.

    From the main function we call both the constructors.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write the code that creates a public class named Vehicle that has one private data member. The private data member is an integer variable ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers