Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Electrical Consumptions The electricity accounts of residents in a very small to

ID: 3708804 • Letter: E

Question

Electrical Consumptions The electricity accounts of residents in a very small town are calculated as follows If 500 units or fewer are used, the cost is 2 cents per unit If more than 500 but not more than 1000 units are used, the cost is $10 for the first 500 units and 5 cents for every unit in excess of 500 If more than 1000 units are used, the cost is $35 for the first 1000 units plus 10 cents for every unit in excess of 1000 . · .A basic service fee of $5 is charged, no matter how much electricity is used. Write a class called Consumptions that contains instance data that represent five residents' electrical consumptions. Define the Consumptions constructor to accept and initialize five consumption Valúes. Include a method that uses a for loop to calculate the total charge for each consumption and also redefine the Java tostring method so that it returns a nicely formatted one or multi-line description of the consumptio consumptions from the standard input stream and also instantiate and update a Consumptions object. The main method should include code to test the object's methods n charges. Create a driver class whose main method allows the user to enter five Test your program with the following values: 200, 500, 700, 1000, and 1500. Results: $9.00, $15.00, $25.00, $40.00, $90.00

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Consumptions.java
--------------------

public class Consumptions {
private int[] units;
public Consumptions(int[] values)
{
units = new int[values.length];
for(int i = 0; i < values.length; i++)
units[i] = values[i];
}

//return charges in dollars
public double getTotalCharge(int units)
{
double total = 0; //basic fee of $5

if(units <= 500)
total = units * 0.02; //0.02 is 2 cents
else if(units <= 1000)
total = 10 + (units - 500) * 0.05; //5 cents for over 500 units
else
total = 35 + (units - 1000) * 0.10; //10 centes for over 1000 units

total += 5; //service fee of $5

return total;
}

public String toString()
{
String s = "";
double charges;
for(int i = 0; i < units.length; i++)
{
charges = getTotalCharge(units[i]);
s += "For " + units[i] + " units, the charge is $" + String.format("%.2f", charges) + " ";
}

return s;
}

}

ConsumptionsDriver.java
---------------
import java.util.Scanner;

public class ConsumptionsDriver {
public static void main(String[] args) {
int[] values = new int[5];
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the consumption units for the 5 residents");
for(int i = 0; i < 5; i++)
{
System.out.print("Resident " + (i+1) + ": ");
values[i] = keyboard.nextInt();
}

Consumptions cons = new Consumptions(values);
System.out.println(cons);

}
}

output
-----
Enter the consumption units for the 5 residents
Resident 1: 200
Resident 2: 500
Resident 3: 700
Resident 4: 1000
Resident 5: 1500
For 200 units, the charge is $9.00
For 500 units, the charge is $15.00
For 700 units, the charge is $25.00
For 1000 units, the charge is $40.00
For 1500 units, the charge is $90.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote