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: 10384
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?

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: 10384
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;

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: 10384
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.

rlobato
Posts: 10
Joined: Fri Jun 28, 2024 3:08 am
Contact:

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

Post by rlobato »

Well, obviously if the method is being made private, the code in the consume method does not compile. But, on the other side, if it is being made public, the user can temper with bytesUsed.. that's why I chose 3

I must admit it is tricky.

Cheers

giginar
Posts: 3
Joined: Fri Feb 07, 2025 5:36 pm
Contact:

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

Post by giginar »

"Further, that a User should not be able to tamper with the totalBill value" - the answer does not meet with this req, user can tamper with the totalBill value. This question is confusing.

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

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

Post by admin »

"To tamper" means to alter in an objectionable manner. The answer doesn't tamper. It modifies the value in accordance with the requirements presented in the previous sentence i.e., "Your goal is to implement a method addUsage (and other methods, if required) in Bandwidth class such that all the bandwidth used by a User is reflected by the totalUsage field and totalBill is always equal to totalUsage*costPerByte. "

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

totalBill is always equal to totalUsage*costPerByte

but you write: totalBill = totalBill + bytesUsed*costPerByte;

why not saying totalBill is always equal to totalBill+totalUsage*costPerByte

totalUsage*costPerByte != totalBill+totalUsage*costPerByte

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

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

Post by admin »

totalUsage*costPerByte is the functional requirement.
totalBill+totalUsage*costPerByte is an implementation that satisfies the requirement.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

ok thanks

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests