How should you write methods in the ElectricAccount cla…

Given the following two classes:

How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always
equal to the value of the member variable kwh multiplied by the member variable rate?
Any amount of electricity used by a customer (represented by an instance of the customer class) must
contribute to the customer’s bill (represented by the member variable bill) through the method useElectricity
method. An instance of the customer class should never be able to tamper with or decrease the value of the
member variable bill.

Given the following two classes:

How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always
equal to the value of the member variable kwh multiplied by the member variable rate?
Any amount of electricity used by a customer (represented by an instance of the customer class) must
contribute to the customer’s bill (represented by the member variable bill) through the method useElectricity
method. An instance of the customer class should never be able to tamper with or decrease the value of the
member variable bill.

A.
Option A

B.
Option B

C.
Option C

D.
Option D



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Maxtor

Maxtor

In A option if kWh is negative decrease the value of the
member variable bill, and C option is private.
The B option is only correct.

asa

asa

Only D, need to be if(kWh > 0) and “An instance of the customer class should never be able to tamper with or decrease the value of the
member variable bill.”

Snr DingDong

Snr DingDong

Answer is B.

Option A allows negative kWh.
Option C is private therefore not accessible within the useElectricity method in class Customer.
and Option D is incorrect as method setBill is public you can access it through the instance variable ElectricAccount acct. in class Customer through an instance of class Customer.
ie Customer c = new Customer();
c.acct.setBill(0);

if setBill was private in class ElectricAccount it would be fine