About Question enthuware.ocajp.i.v8.2.1447 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
yndingo
Posts: 1
Joined: Mon Mar 22, 2021 11:36 am
Contact:

About Question enthuware.ocajp.i.v8.2.1447 :

Post by yndingo »

public void addUsage(int bytesUsed) {    
if (bytesUsed > 0) {       
totalUsage = totalUsage + bytesUsed;       
totalBill = totalBill + bytesUsed * costPerByte;  // !=  totalBill = totalUsage*costPerByte
}
}

totalBill is always equal to totalUsage*costPerByte

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1447 :

Post by admin »

Sorry but what is your question?
If you like our products and services, please help us by posting your review here.

catang
Posts: 2
Joined: Sat Mar 27, 2021 7:20 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1447 :

Post by catang »

I think what he is trying to say is the same thing for which I came here to discuss.
The question / business logic says that "totalBill is always equal to totalUsage*costPerByte", but the correct answer does not reflect this requirement.
Can you please detail why is the first option the correct option in spite of this requirement?

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1447 :

Post by admin »

There are two requirements - 1. all the bandwidth used by a User is reflected by the totalUsage field and 2. totalBill is always equal to totalUsage*costPerByte.

So, yes, you could do totalBill = totalUsage*costPerByte; but option 3, which implements it this way is private. You don't want the method to be private because the code in User class invokes bw.addUsage(bytesUsed);. This call will fail to compile if you make addUsage private. That is why option 3 is incorrect.

Option 1 is good because totalBill = totalBill + bytesUsed*costPerByte; still manages to satisfy the required constraint, which is, totalCost must be equal to totalUsage*costPerByte.

totalBill = totalBill + bytesUsed*costPerByte;
=> (bytes used earlier)*costPerByte + (bytes used now) *costPerByte;
=> (bytes used earlier + bytes used now) *costPerByte;
=> (totalUsage + bytesUsed )*costPerByte;
=> totalUsage*costPerByte; //because totalUsage = totalUsage + bytesUsed;
If you like our products and services, please help us by posting your review here.

catang
Posts: 2
Joined: Sat Mar 27, 2021 7:20 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1447 :

Post by catang »

Thank you for your quick reply.

I understand better now what was required to be answered, but practically this is a trick question, because of the way it is presented.
Option 3 was clearly not an option because of the private access modifier.

I do feel like option 4 still might be a good answer because:
1. it clearly uses the requirement mentioned in the business logic: totalBill = totalUsage*costPerByte;
2. the question suggests that more than one method can be created in order to comply with all requirements; "Your goal is to implement a method addUsage (and other methods, if required)"
3. the second method updateTotalBill() could be called in the User class instead of " ... other irrelevant code";

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1447 :

Post by admin »

No, option 4 is absolutely incorrect. What if someone calls addUsage but doesn't call updateTotalBill? This leaves the consistency of the Bandwidth objects at the mercy of other people. Completely undesirable as per the problem statement.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 114 guests