In: Computer Science
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE
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 Grading item: Avoid using Magic Numbers
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the program
Source code
//importing Scanner class for handling user input
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//declaring variables for holding values
int noPassengers, noBags;
double fee;
//creating scanner object for reading user input
Scanner sc = new Scanner(System.in);
//prompt user to enter number of passengers
System.out.print("Enter number of passengers on your ticket: ");
//reading number of passengers
noPassengers = sc.nextInt();
//prompt user to enter total number of bags
System.out.print("Enter number of bags: for this ticket ");
//reading total number of bags
noBags = sc.nextInt();
//calling calculateMyBaggageFees() method
fee = calculateMyBaggageFees(noPassengers, noBags);
//printing the result
System.out.printf("The cost for baggage will be $%.2f", fee);
}
//calculateMyBaggageFees() method definition
public static double calculateMyBaggageFees(int nPassengers, int nBags) {
//calculating total number of free bags allowed
int freeBagsAllowed = nPassengers * 2;
//calculating excessBagFee
double excessBagFee = (nBags - freeBagsAllowed) * 75;
//checking if excessBagFee is negative if yes return 0
if (excessBagFee < 0)
return 0;
//else return excessBagFee
else
return excessBagFee;
}
}
Screen shot of the code

Screen shot of the output
