Question 9 of 76 from exam 1Z0-808: Java SE 8 Programmer I

Question 9 of 76 from exam 1Z0-808: Java SE 8 Programmer I

Question

Given the following two classes:

public class Customer {
Electricaccount acct = new ElectricAccount ();

public void useElectricity (double kwh) {
acct. addkWwh (kWh) 3
}

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

ffline nl

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) 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

this.bill

kwh;
this. kwh*this. rate;

)

©D) public void addKWh (double kwh) {
if (kwh > 0) {
this. kWh kWh;
setBill (this. kwh) ;

)

j

public void setBill(double kwh) {
bill = kwh*rate;

}

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

AC.