Background For this assignment, we will use if , if/else , and if/else if/else s
ID: 641332 • Letter: B
Question
Background
For this assignment, we will use if, if/else, and if/else if/else statements to calculate some values.
Requirements
A certain airline charges luggage fees as follows:
? Each passenger is allowed at most two pieces of luggage.
? Each passenger is allowed at most 50 pounds total without an additional fee.
? Overweight luggage is assessed a charge of $2 per pound over 50 pounds.
? Each piece of luggage must satisfy a size requirement:
The sum of its length, width, and height must be less than or equal to 62 inches.
? Oversized luggage is assessed a charge of $5 for each oversized piece of luggage.
Write a Java program named LuggageFeeCalculator.java that prompts the user for information about
their luggage and calculates the fees for that luggage. Use class constants for the weight allowance, size
allowance, overweight charge and oversize charge.
Expected Output:
Three sample runs of the program: (User input has been underlined in the listings below.)
Weight of first item: 30
Length of first item: 20
Width of first item: 20
Height of first item: 20
Weight of second item: 21
Length of second item: 30
Width of second item: 25
Height of second item: 20
Your luggage is overweight by 1.0 pounds.
Your first piece of luggage is not oversize.
Your second piece of luggage is oversize by 13.0 inches
Total luggage charge is 7.00.
Explanation / Answer
//Java program that prompts the user for luggage
//weight,lenght,width and height
//calculates the overweight and over size
//prints the total cost of overweight of luggage
//LuggageFeeCalculator.java
import java.util.Scanner;
public class LuggageFeeCalculator
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
double weight1,weight2;
double lenght1,lenght2;
double width1,width2;
double height1,height2;
double luggage_extra_weight=0;
double luggage_extra_size=0;
//constance values
final int WEIGHT_ALLOWANCE=50;
final int SIZE_ALLOWANCE=62;
final int OVERWEIGHT_CHARGE=2;
final int OVERSIZE_CHARGE=5;
//prompt user for input values of luggage
System.out.print("Weight of first item: ");
weight1=keyboard.nextDouble();
System.out.print("Length of first item: ");
lenght1=keyboard.nextDouble();
System.out.print("Width of first item: ");
width1=keyboard.nextDouble();
System.out.print("Height of first item: ");
height1=keyboard.nextDouble();
System.out.print("Weight of second item: ");
weight2=keyboard.nextDouble();
System.out.print("Length of second item: ");
lenght2=keyboard.nextDouble();
System.out.print("Width of second item: ");
width2=keyboard.nextDouble();
System.out.print("Height of second item: ");
height2=keyboard.nextDouble();
//checking over weight
if((weight1+weight2)>WEIGHT_ALLOWANCE)
{
luggage_extra_weight=(weight1+weight2-WEIGHT_ALLOWANCE);
System.out.println("Your lugguage is overweight by "+luggage_extra_weight);
}
else
System.out.println("Your lugguage is not overweight");
//checking over size of first luggage
if((lenght1+width1+height1)>SIZE_ALLOWANCE)
{
luggage_extra_size=OVERSIZE_CHARGE;
System.out.println("Your first piece of luggage is oversize by "+luggage_extra_size);
}
else
System.out.println("Your first piece of luggage is not oversize.");
//checking over size of second luggage
if((lenght2+width2+height2)>SIZE_ALLOWANCE)
{
//add oversize cost to luggage extra size
luggage_extra_size+=OVERSIZE_CHARGE;
System.out.println("Your second piece of luggage is oversize by "+
(lenght2+width2+height2-SIZE_ALLOWANCE));
}
else
System.out.println("Your second piece of luggage is not oversize.");
//calculate total cost of the luggage
double extra_cost=luggage_extra_weight*OVERWEIGHT_CHARGE+
luggage_extra_size;
System.out.println("Total luggage charge is "+extra_cost);
}
}
------------------------------------------------------------------------------------
Sample output:
Weight of first item: 30
Length of first item: 20
Width of first item: 20
Height of first item: 20
Weight of second item: 21
Length of second item: 30
Width of second item: 25
Height of second item: 20
Your lugguage is overweight by 1.0
Your first piece of luggage is not oversize.
Your second piece of luggage is oversize by 13.0
Total luggage charge is 7.0
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.