This program is in Java Define an abstract UtilityCustomer class that has one in
ID: 3745987 • Letter: T
Question
This program is in Java
Define an abstract UtilityCustomer class that has one instance variable, an account number, and an abstract method, calculateBill, that returns the bill amount as a double.
Also define the constructor, accessors, mutators, and a toString method that outputs the account number.
Define a GasCustomer class that inherits from the UtilityCustomer class, and adds an instance variable cubicMetersUsed and a constant for the price of gas per cubic meter.
Also define an ElectricCustomer class that also inherits from the UtilityCustomer class and adds a kWattHourUsed instance variable along with two constants for the price of electricity per kilowatt hour, plus a flat power delivery fee ($30) that is added to every bill.
For each subclass, define the constructor, accessor, and mutator for its instance variable, a toString method, which calls the toString method of the UtilityCustomer class, and an implementation for the calculateBill method.
Create a client that instantiates several GasCustomer and ElectricCustomer objects, adds them to an ArrayList of UtilityCustomer objects, and then using polymorphism, steps through the ArrayList, calling the calculateBill method and outputting the bill amount for each customer.
Explanation / Answer
UtilityCustomer.java
public abstract class UtilityCustomer {
private int accNo;
public abstract double calculateBill();
public UtilityCustomer(int accNo) {
this.accNo = accNo;
}
public int getAccNo() {
return accNo;
}
public void setAccNo(int accNo) {
this.accNo = accNo;
}
@Override
public String toString() {
return "UtilityCustomer [accNo=" + accNo + "]";
}
}
________________
GasCustomer.java
public class GasCustomer extends UtilityCustomer {
private int cubicMetersUsed;
private final double priceOfGasPerCubiCmeter = 235;
public GasCustomer(int accNo, int cubicMetersUsed) {
super(accNo);
this.cubicMetersUsed = cubicMetersUsed;
}
public int getCubicMetersUsed() {
return cubicMetersUsed;
}
public void setCubicMetersUsed(int cubicMetersUsed) {
this.cubicMetersUsed = cubicMetersUsed;
}
@Override
public double calculateBill() {
return cubicMetersUsed * priceOfGasPerCubiCmeter;
}
@Override
public String toString() {
return super.toString() + " GasCustomer [cubicMetersUsed=" + cubicMetersUsed + "]";
}
}
_________________
ElectricCustomer.java
public class ElectricCustomer extends UtilityCustomer {
private int kWattHourUsed;
private final double KILOWATTHOUR = 120;
private final double DELIVERYFEE = 30;
public ElectricCustomer(int accNo, int kWattHourUsed) {
super(accNo);
this.kWattHourUsed = kWattHourUsed;
}
@Override
public double calculateBill() {
return (kWattHourUsed * KILOWATTHOUR) + DELIVERYFEE;
}
@Override
public String toString() {
return super.toString() + " ElectricCustomer [kWattHourUsed=" + kWattHourUsed + "]";
}
}
___________________
Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList < UtilityCustomer > arl = new ArrayList < UtilityCustomer > ();
arl.add(new GasCustomer(1111, 20));
arl.add(new GasCustomer(2222, 30));
arl.add(new GasCustomer(3333, 40));
arl.add(new ElectricCustomer(4444, 20));
arl.add(new ElectricCustomer(5555, 40));
arl.add(new ElectricCustomer(6666, 35));
for (int i = 0; i < arl.size(); i++) {
if (arl.get(i) instanceof GasCustomer) {
System.out.println("Gas Bill Amount for Customer#" + (i + 1) + ":$" + arl.get(i).calculateBill());
} else {
System.out.println("Electric Bill Amount for Customer#" + (i + 1) + ":$" + arl.get(i).calculateBill());
}
}
}
}
___________________
Output:
Gas Bill Amount for Customer#1:$4700.0
Gas Bill Amount for Customer#2:$7050.0
Gas Bill Amount for Customer#3:$9400.0
Electric Bill Amount for Customer#4:$2430.0
Electric Bill Amount for Customer#5:$4830.0
Electric Bill Amount for Customer#6:$4230.0
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.