nerdexam
Oracle

1Z0-808 · Question #44

Given the following two classes: ``java public class Customer { ElectricAccount acct = new ElectricAccount(); public void useElectricity(double kWh) { acct.addKwh(kWh); } } public class…

The correct answer is B. ```java public void addKwh(double kWh) { if (kWh > 0) { this.kwh += kWh; this.bill = this.kwh * this.rate; } } ```. Option B is correct because it satisfies all three constraints simultaneously: it is public so Customer can call it, the if (kWh > 0) guard prevents a customer from passing a negative value to reduce the bill, and bill is immediately recalculated as kwh * rate on every valid…

Working with Methods and Encapsulation

Question

Given the following two classes:
public class Customer {
 ElectricAccount acct = new ElectricAccount();

 public void useElectricity(double kWh) {
 acct.addKwh(kWh);
 }
}

public class ElectricAccount {
 private double kwh;
 private double rate = 0.07;
 private double bill;

 //line n1
}
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.

Options

  • A
    public void addKwh(double kWh) {
     this.kwh += kWh;
     this.bill = this.kwh*this.rate;
    }
    
  • B
    public void addKwh(double kWh) {
     if (kWh > 0) {
     this.kwh += kWh;
     this.bill = this.kwh * this.rate;
     }
    }
    
  • C
    private void addKwh(double kWh) {
     if (kWh > 0) {
     this.kwh += kWh;
     this.bill = this.kwh*this.rate;
     }
    }
    
  • D
    public void addKwh(double kWh) {
     if (kWh > 0) {
     this.kwh += kWh;
     setBill(this.kwh);
     }
    }
    
    public void setBill(double kWh) {
     bill = kWh*rate;
    }
    

How the community answered

(27 responses)
  • A
    11% (3)
  • B
    56% (15)
  • C
    26% (7)
  • D
    7% (2)

Explanation

Option B is correct because it satisfies all three constraints simultaneously: it is public so Customer can call it, the if (kWh > 0) guard prevents a customer from passing a negative value to reduce the bill, and bill is immediately recalculated as kwh * rate on every valid update.

Option A fails the tamper-proof requirement - without the positive check, a Customer could call addKwh(-100) to fraudulently reduce their bill.

Option C has the right logic but uses private access, which means Customer.useElectricity() can't call acct.addKwh(kWh) at all - it won't even compile.

Option D introduces a public setBill() method, which hands the Customer a direct backdoor to set bill to any arbitrary value, breaking the tamper-proof requirement.

Memory tip: Two locks, one key - the method needs to be public (so Customer can reach it) but the door inside must check kWh > 0 (so Customer can't abuse it). If anything else is public or the check is missing, one of those locks is broken.

Topics

#encapsulation#access modifiers#class invariants#data protection

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice