Java Problem: Program: Q10.15 Notice that in line 18, we have public void Calc B
ID: 3607256 • Letter: J
Question
Java Problem:
Program:
Q10.15 Notice that in line 18, we have public void Calc BMI, which is also has same name as of the class name, why it does not cause syntax erTor? ANS: Why do we need methods in LINE 21 & 22 for what funetion? What is the common name for these kinds of methods? ANS Q10.16 Q10.17 Why do we need methods in LINE 23 & 24 for what function? What is the common name for these kinds of methods? ANS Q10.18 In Line 21, 22, 23, and 24, the method names are prefixed with three specific letters, would the compiler marked them as syntax error if you use other letters for the prefix? ANS: cycle: YES or NOExplanation / Answer
Hi, Please find the answers below. Kindly let me know if you have any doubts!
Ans 1) Though it doesn't cause a syntax error, it is a very bad practice to name a method same as class name. Anyways, this doesn't give a syntax error, because it is legal to name the methods, same as the class name.These methods don't have any special properties. Though the constructor has the same name too, but Java compiler identifies that because constructors have no return type. Banning this explicitly by adding another rule to Java's identifier naming conventions was of no use.
Ans 2) We need setW and setH to set the values of our private instance variables h and w. An object or subclass of Calc_BMI will have to set the values of h and w using these methods only.
Common name for these methods is setters and mutators.
Ans 3) We need getW and getH to fetch the values of our private instance variables h and w. An object or subclass of Calc_BMI will have to fetchthe values of h and w using these methods only.
Common name for these methods is getters and accessors.
Ans 4) No, that won't be a syntax or compile-time error! You can name these randomly but it is generally a good practice to follow this convention.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.