On page 426 of Big Java: Late Objects, complete the review exercises: R8.1 What
ID: 3879926 • Letter: O
Question
On page 426 of Big Java: Late Objects, complete the review exercises:
R8.1 What is encapsulation? Why is it useful?
R8.2 What values are returned by the calls reg1.getCount(), reg1.getTotal(), reg2.getCount(), and reg2.getTotal() after these statements?
CashRegister reg1 = new CashRegister();
reg1.addItem(3.25);
reg1.addItem(1.95);
CashRegister reg2 = new CashRegister();
reg2.addItem(3.25);
reg2.clear();
R8.3 Consider the Menu class in Worked Example 8.1. What is displayed when the following calls are executed?
Menu simpleMenu = new Menu();
simpleMenu.addOption("Ok");
simpleMenu.addOption("Cancel");
int response = simpleMenu.getInput();
R8.4 What is the public interface of a class? How does it differ from the implementation of a class?
Explanation / Answer
R8.1 Encapsulation is a process of wrapping up the code and data in a single unit.
--> for example you can the take capsule in capsule you can have mixed of several medicines.
a normal java bean class is encapsulation
class chef{
private String student;
private String expert;
approproiate getters and setters ......
}
the above example is encapsulation as it has datamembers and methods too.
encapsulation is useful to achieve hiding complexity concept
i will explain with an example
lets consider car as a example in a car we know that steering,brake,gear. so we know that gear is for moving upwards and backwards and steering for controlling the car and break is for stop the car if we take any type of car alomost these are same but the main thing was we dont know how steering mechanism happening inside and gear as well as break so these fetures are hidden for us because these details are unnecessary for us.
In the same way in ecapsulation we will hide the unnccessary details and provide the necessary information.
R8.2 after executing
reg1.getCount() method this will return the count of items in reg1 i.e 2.
reg1.getTotal() method will return the total cost of items in reg1 i.e 3.25+1.95=5.2
reg2.getCount() method will return 0 beacuse we are clearing the reg2 with clear method so no items will be there in that as well as for getTotal() too.
R8.3 in this we are having a simpleMenu object and Menu class
for a menu we are adding a optoin OK and ancel to that menu in a dialog box based on user reponse... the reponse variable will store the response of the user request.
R8.4 the public interface of a class are its public properties (variables or fields you can read the values of or assign to) and methods (functions you can call).
interface vs class
1)A class is instantiated to create objects. where as interface can never be instantiated as the methods are unable to perform any action on invoking
2)The methods of a class are defined to perform a specific action where as The methods in an interface are purely abstract.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.