Challenge 2: Calling a Constructor from a Constructor
Can you call a constructor from another constructor? A solution is placed in the "show solution" section to help you, but we would suggest you try to solve it on your own first.
We'll cover the following
Problem Statement
A class can have multiple parameterized constructors which can call each other.
You are given a partially completed code of a VendingMachine
class in the editor. Implement a new parametrized constructor by making use of the already declared constructor which assigns values to all the declared fields.
For the ease of understanding, all the fields are declared as string
irrespective of the type of data they store.
Input
_count, _capacity, _moneyCollected
Output
When the parameterized constructor that you implemented, is called, the declared fields should be initialized accordingly.
Sample Input
VendingMachine vendy = new VendingMachine("Count: 45", "Capacity: 50", "Money Collected: 440$");
Sample Output
All the fields should store the respective data.
Get hands-on with 1400+ tech skills courses.