This is Figure 9.10 only for demonstration purposes. Overview of Class Comission
ID: 3802537 • Letter: T
Question
This is Figure 9.10
only for demonstration purposes. Overview of Class ComissionEmployee's Methods and Inntmee Variables Class CommissionEnployee's public services include a constructor nes 13-34) and methods earnings (ines 87-90) and tostring 0ines 93-101), Lines 37-52 declare pub- nie err methods for the class's final instance variables (declared in lines 6-8) firstNane, lastName and social SecurityNumber. These three instance variables are declared final because they do not need to be modified after they're initialized-this is also why we do 366 Chapter 9 Object-oriented Programming Inheritance corresponding ser methods. Lines 55-84 declare public ser and get methods for the class's grosssales and commissionRate instance variables (declared in lines9-10) The class declares its instance variables as private, so objects of other classes cannot di rectly access these variables, 9.4: Commiss on Employee java 2 Commission Employee ss represents an employee paid a percentage of gross sales 4 public class CommissionEmployee extends object private final String firstNane private final String lastName private final String socialSecurityNunbe private double grosssales: gross weekly sales 10 private double comissionRate: commission percentage five-argument constructor 13 public Commission Employee CString firstName, String lastName, String social SecurityNumber, double grosssales. ssionRate) implicit cal to object's default constructor occurs here if grossSales is invalid throw exception if (gross sales 0.0) throw new niegalArgumentExcept tion( "Gross sales must be 0.0 if comi sionkate is invalid throw exception if (commission Rate onRate 1.0) tion "Commission rate must be 0.0 and 1.0") this firstName firstName this last Name last Name this social SecurityNumbe r social SecurityNumber this gross Sales gresssales cosmissionRate this commission Rate end constnactor return first name public string getFirstNameo return firstNase return last name lastName Fig. 9.4 I ComissionEmployee class represents an employee paid a percentage of gross sales
Explanation / Answer
There is 1 difference in the both implementations..
Note: in 9.4, it is written extends Object and in figure 9.10 it is not written but that is not a difference because every class in Java is extending Object class for sure.
The difference is in the earning() and the toString() method.
In the figure 9.4 if you see the earning() method, it directly manipulates on commissionRate and grossSales variables. Whereas the earning() method in figure 9.10 uses the getter methods to get these values.
The same goes with toString() method. Here in figure 9.4 the variables names are directly used to get the values whereas in figure 9.10 they are get by the getter methods. The difference is highlighted in the figure also.
The reason and benefit lies in using figure 9.10. The reason behind doing this is, if you change the instance variable name, you won't be going to change the getter and setter method names. Say for instance, you rewrite firstName as first_name or something like that, and the same way you change the name of every instance variable so in that case you need to change the names throughout the class. It is obvious you will change the name in getter and setter property but also you need to change the whole names inside the toString() and the earning() method.
Whereas if you will change the instance variable name in fig. 9.10; the changes you need to do is inside only the getter and setter methods body, for example if you change firstName to first_name; you won't change getFirstName() method name to getFirst_Name() because there is no need for it, the getFirstName() is saying the clear meaning so you don't require to change the method names.
So, as you are not changing the method names, there won't be any modification in the earning() as well as toString() method as the getter method names remain same. So this is the benefit we get.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.