Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10) CONSTRUCTORS Q: a) Which methods are the constructors? How do you know? b) W

ID: 3857464 • Letter: 1

Question

10) CONSTRUCTORS Q: a) Which methods are the constructors? How do you know? b) Where are they called? c) What is the purpose of the constructor? d) What gets printed e) What happens if you omit line B? f) What happens if you omit line A, B, H, J, K? g) What happens if you omit only line A? class Date ate private int month, year; public Date() { year = 2000; month = 1; public Date (int m, int y) { year = y; month public int getYr) return year; > public int getMon return month; > public void leap) year - year + month; = m; public class M5 public static void main(String [ args) Date myYear, newYr myYear = new Date(); newYr new Date (7, 2017); myr VI ewyr + myYear.getYr System.out.println (myYear.getMon + "/" newYr.leap) System.out.println (newYr.getMon + "/"+ //I

Explanation / Answer

Hi I'll explain everything here only ..

First let me answer all your questions ...

a) methods A and B i.e public Date() and public Date(int m,int y) are constructors here .. We get to know that these are constructors because constructor have these very important properties :-

i) Name of the method is same as that of class name.

ii) there is no return type of the method.

iii) Generally they are public.

b) Constructors are used in instantiate an object. They are basically called when we create an object in a function .

c) Constructor is used to instantiate the object . it means we need constructor to initialise the class attributes. Eg. here we need to add initial values to month and year. I'll explain this part a little later in this answer.

d) The output of this program is below. Note that I'll explain the output a little later.

1/2000

7/2024

e) If we omit B, then there will not be any parameterized constructor available here.So line H will give compilation error.

f)

If we omit A,B,H,J and K, there will not be any compilation erroe. because there is always a default constructor present for the class even if we dont specify. But that constructor is empty and do nothing . So year and month will have default values i.e. 0 and 0.

g) If we omit line A, then there will be compilation error at line G. Surprised??

This is because, if we have any parameterized constructor in our class, we must have default constructor explicitly specified. else default constructor can not be used.

So now, Let me explain the program .

Here, Date class is having two instance variables month and year both of type int. This class is having two constructors.

There are two basic type of constructors.

Default constructor with no parameters and Parameterized constructor with some paramters used to initialize the instance variables.

Date class is having both type of constructors. Default constructor initialize month with 1 and year with 2000. and Parameterized constructor initialize the month and year with parameters involved im the constructor. .

There are three more methods, getYr() method to get the value of year, getMon() to get the month and leap() to add month to year and save it in year variable.

So this was all about definition of class.

Now lets discuss about main function..

In main, we first have two Date type variables myYear and newYr. These are objects declared for Date class.

Now these two objects are instantiated one with default constructor and other with parameterized constructor.

myYear is instantiated with default constructor, so its month is initialised to 1 and year to 2000.

newYr is instantiated with parameterized constructor with values 7 and 2017, so month got initialised with 7 and year with 2017.

Next line is used to print month and year of myYear object so it prints 1/2000.

next line calls leap() function of newYr object . So it adds 7 to 2017 and saves 2024 in year.

next line is for printing values of newYr. month of newYr contains 7 and year contains 2024 . So it prints 7/2024.

so out put is as follows:-

1/2000

7/2024

hope you understood it. If you still have some questions on this. Please ask in comments I'll answer them.