In: Computer Science
An airline describes airfare as follows. A normal ticket's base cost is $300. Persons aged 60 or over have a base cost of $290. Children 2 or under have $0 base cost. A carry-on bag costs $10. A first checked bag is free, second is $25, and each additional is $50. Given inputs of age, carry-on (0 or 1), and checked bags (0 or greater), compute the total airfare.
Hints:
First use an if-else statements to assign airFare with the base cost
Use another if statement to update airFare for a carryOn
Finally, use another if-else statement to update airFare for checked bags
Think carefully about what expression correctly calculates checked bag cost when bags are 3 or more
program code needed to finish (Airfare.java)
import java.util.Scanner;
public class Airfare {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int passengerAge;
int carryOns;
int checkedBags;
int airFare;
passengerAge = scnr.nextInt();
carryOns = scnr.nextInt();
checkedBags = scnr.nextInt();
/* Type your code here. */
System.out.println(airFare);
}
}
Ans)
Code :
Output:
Explanation :
From line 14 to 19, the code will put the base cost in the airFare variable. First it will check if the passenger is of age 2 or lower then it will assign 0 to it. If the passenger's age is greater than or equal to 60 then the base cost will be 290 and it will be assigned to the airFare. If the age is between 2 to 60( both exclusive), code will assign 300 to airFare.
In line 21, the code will check for the carry-on bags. If it is 1 then code will update the value of airFare by adding 10 to it.
If line 24, the code will check for the checked bags. If it is 1, then the code will do nothing. If it is 2, the code will update the value of airFare by adding 25 to it. If it is 3 or more, the code will update the value of airFare by first adding 25 and then 50 for each extra bag to it.
Understanding the Output based on a Example:
First we have entered the age of passenger as 50. So the base cost will be 300.
airFare = 300
Now we have entered the carry-on bags as 1.
airFare = 300 + 10
airFare = 310
Now we have entered the checked bags as 5
So for first bag nothing to be done and for 2nd bag we have to add 25 and for extra we have to add 50. We have 3 extra bags.
airFare = 310 + (25 + (3 * 50))
airFare = 485
That is how our output is 485.
Hope you got it. Do like it, if it helps you.