Electrical Consumptions The electricity accounts of residents in a very small to
ID: 3707944 • 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.00Explanation / Answer
public class Consumptions {
// declaring the consumption values
public int conVal1, conVal2, conVal3, conVal4, conVal5;
// constructor
public Consumptions(int val1, int val2, int val3, int val4, int val5) {
this.conVal1 = val1;
this.conVal2 = val2;
this.conVal3 = val3;
this.conVal4 = val4;
this.conVal5 = val5;
}
public void calculateConsumptionCost() {
int[] valueArray = new int[5];
// storing the values in array
// so many possible ways are there but this way of implementation is so
// easy
for (int i = 0; i < 5; i++) {
if (i == 0) {
valueArray[i] = conVal1;
}
if (i == 1) {
valueArray[i] = conVal2;
}
if (i == 2) {
valueArray[i] = conVal3;
}
if (i == 3) {
valueArray[i] = conVal4;
}
if (i == 4) {
valueArray[i] = conVal5;
}
}
// for calculating the prices and printing the result
for (int i = 0; i < 5; i++) {
if (valueArray[i] < 500) {
int price = 0;
int units = valueArray[i];
int cents = units * 2;
int dollar = cents / 100;
int servicecharge = 5;
price = dollar + servicecharge;
System.out.print("the results are $" + price);
}
if (valueArray[i] >= 500 && valueArray[i] < 1000) {
int price = 0;
int totalunits = valueArray[i];
// calculating remaining units except 500
int remainingunits = totalunits - 500;
int remainingunitscostincents = remainingunits * 5;
int remainingunitscostindollars = remainingunitscostincents / 100;
int dollar = 10 + remainingunitscostindollars;
int servicecharge = 5;
price = dollar + servicecharge;
System.out.print(",$" + price);
}
if (valueArray[i] >= 1000) {
int price = 0;
int totalunits = valueArray[i];
// calculating remaining units except 500
int remainingunits = totalunits - 1000;
int remainingunitscostincents = remainingunits * 10;
int remainingunitscostindollars = remainingunitscostincents / 100;
int dollar = 35 + remainingunitscostindollars;
int servicecharge = 5;
price = dollar + servicecharge;
System.out.print(",$" + price);
}
}
}
public static void main(String[] args) {
Consumptions c = new Consumptions(200, 500, 700, 1000, 1500);
c.calculateConsumptionCost();
}
}
output
the results are $9,$15,$25,$40,$90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.