Create a Java Program to calculate luggage costs.
The Business Rules are:
A. Two bags per person are free.
B. The Excess Bag Charge is $75 per bag.
The program needs to do the following:
1. In your main method(),
2. From the main() method:
3. In the method calculateMyBaggageFees:
3. In the main() method, display the cost for baggage. This is only possible if the method calculateMyBaggageFees is a value method.
Example Input/Output
Enter number of passengers on your ticket:2
Enter number of bags for this ticket:5 The cost for baggage will be $75.00
Code:
// import the scanner class to read input from the user
import java.util.Scanner;
public class LuggageCost {
// method calculateMyBaggageFees takes two
parameters passengers and no_bags
// it calculates the extra cost of the baggage
// cost for extra bag is 75
// this function return a double value
static double calculateMyBaggageFees(int passengers
,int no_bags) {
// calculate the number of free
bags
// we know that each person can have
2 bags so 2*passengers will give free bags
int free_bags = 2*passengers;
// now calculate the extra
bags
// we know total bags and free bags
so subtract it we get extra bags
int extra_bags =no_bags - free_bags
;
// calculate the cost for extra
bags
// we know cost for one extra bag is
75
double cost = 75 *
extra_bags;
// check if the cost is -ve
value
// if it is negative means there is
no extra bags we simply return 0
if(cost<0) {
return 0;
}
// else return the cost of the extra
bags
else {
return
cost;
}
}
public static void main(String[] args) {
// declare the variables passengers
and no_bags
int passengers , no_bags;
// create an object for scanner to
read input from the user
Scanner in = new
Scanner(System.in);
// prompt the user to enter
passengers ticket
System.out.print("Enter number of
passengers on your ticket: ");
// read the value and store it in
passengers
passengers = in.nextInt();
// prompt the user to enter number
of bags for ticket
System.out.print("Enter number of
bags for this ticket: ");
// store the value in no_bags
no_bags = in.nextInt();
// now call the function
calculateMyBaggageFees with passengers and no_bags as
parameters
double result =
calculateMyBaggageFees(passengers,no_bags);
// print the result
System.out.println("The cost for
baggage will be $"+result);
}
}
Get Answers For Free
Most questions answered within 1 hours.