public class Question_5_Average_Utility_Bill { String[] months = { \"January\",
ID: 3881051 • Letter: P
Question
public class Question_5_Average_Utility_Bill { String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public static void main(String[] args) { Question_5_Average_Utility_Bill billsAverage = new Question_5_Average_Utility_Bill(); billsAverage.billAverages(); } private void billAverages() { double[] bills = getYearBills(); double average = averageBillAmount(bills); System.out.println(String.format("Your average bill is %.2f", average)); printBillTable(bills); } public double[] getYearBills() { // TODO ask user for bill amount for January, then February... // Create a new double array. // Store values the user enters in this array. // Return this array. return null; // replace with your code } public double averageBillAmount(double[] bills) { // TODO Calculate the average value of all the bills, and return this number. return 0; // replace with your code } public void printBillTable(double[] bills) { // TODO display the month name, and bill amounts, in table form. // Use the months array to display the names. // Replace these lines with your code. You'll need to use a loop to display all the months. // String formatting is helpful. Here's some examples to space some columns with exactly 15 character width System.out.println(String.format("| %-15s| %-15s|", "Month", "Bill" )); System.out.println(String.format("| %-15s| %-15.2f|", "January", 44.5995 )); } } ---------------------------------------------------- package question5; import static input.InputUtils.doubleInput; /** * * A parcel delivery company charges the following rates to ship a parcel. • Up to 10 pounds: $2.15 per pound • Up to 20 pounds: $1.55 per pound • Up to 30 pounds: $1.15 per pound The shipping company does not ship parcels that weigh over 30 pounds. So, a parcel that weighs 17 pounds will cost $1.55 x 17 = $26.35. Write a program that asks the user for the weight of a parcel and displays whether it can be shipped, and what it will cost. Optional extra: the most obvious solution to this problem uses if statements for the price bands. Can you think of a different way? Hint – loops and arrays of price and max weights for price? */
Explanation / Answer
1)
Question_5_Average_Utility_Bill.java
import java.util.Scanner;
public class Question_5_Average_Utility_Bill {
String[] months = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
public static void main(String[] args) {
Question_5_Average_Utility_Bill billsAverage = new Question_5_Average_Utility_Bill();
billsAverage.billAverages();
}
private void billAverages() {
double[] bills = getYearBills();
double average = averageBillAmount(bills);
System.out.println(String.format("Your average bill is %.2f", average));
printBillTable(bills);
}
public double[] getYearBills() {
// TODO ask user for bill amount for January, then February...
// Create a new double array.
// Store values the user enters in this array.
// Return this array. return null;
// replace with your code
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
double bills[] = new double[12];
for (int i = 0; i < 12; i++) {
System.out.print("Enter the Bill amount for " + months[i] + ":$");
bills[i] = sc.nextDouble();
}
return bills;
}
public double averageBillAmount(double[] bills) {
double tot = 0.0, avg = 0.0;
for (int i = 0; i < 12; i++) {
tot += bills[i];
}
avg = tot / 12;
return avg;
// replace with your code
}
public void printBillTable(double[] bills) {
// TODO display the month name, and bill amounts, in table form.
// Use the months array to display the names.
// Replace these lines with your code. You'll need to use a loop to
// display all the months.
// String formatting is helpful. Here's some examples to space some
// columns with exactly 15 character width
System.out.println(String.format("| %-15s| %-15s|", "Month", "Bill"));
for (int i = 0; i < 12; i++) {
System.out.println(String.format("| %-15s| %-15.2f|", months[i], bills[i]));
}
}
}
____________________
Output:
Enter the Bill amount for January:$45
Enter the Bill amount for February:$56
Enter the Bill amount for March:$67
Enter the Bill amount for April:$76
Enter the Bill amount for May:$65
Enter the Bill amount for June:$54
Enter the Bill amount for July:$43
Enter the Bill amount for August:$78
Enter the Bill amount for September:$89
Enter the Bill amount for October:$87
Enter the Bill amount for November:$65
Enter the Bill amount for December:$54
Your average bill is 64.92
| Month | Bill |
| January | 45.00 |
| February | 56.00 |
| March | 67.00 |
| April | 76.00 |
| May | 65.00 |
| June | 54.00 |
| July | 43.00 |
| August | 78.00 |
| September | 89.00 |
| October | 87.00 |
| November | 65.00 |
| December | 54.00 |
________________
2)
ParcelCharges.java
import java.util.Scanner;
public class ParcelCharges {
public static void main(String[] args) {
//Declaring variables
double shippingCharges = 0.0, weight;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter the weight of parcel :");
weight = sc.nextDouble();
//Based on the parcel weight calculate the Shipping charges
if (weight <= 10) {
shippingCharges = weight * 2.15;
} else if (weight > 10 && weight <= 20) {
shippingCharges = weight * 1.55;
} else if (weight > 20 && weight <= 30) {
shippingCharges = weight * 1.15;
} else if (weight > 30) {
System.out.println("** Parcel will not be delivered **");
}
//Displaying the output
System.out.println("Shipping Charges :$" + shippingCharges);
}
}
_____________________
Output:
Enter the weight of parcel :17
Shipping Charges :$26.35
________________Could you plz rate me well.Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.