Question

Create a Java Program to calculate luggage costs. The Business Rules are: A. Two bags per...

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(),

  1.    Create integers to store the number of Passengers and also the total number of bags
  2.    Prompt for the number of passengers on a ticket.
  3.    Prompt for the total number of bags for all passengers on a ticket.

2. From the main() method:

  1. Pass the number of passengers and the number of bags to a value method named "calculateMyBaggageFees". This method needs to determine the number of free bags allowed. A bag count over the free count should be multiplied by the Excess Bag Charge.

3. In the method calculateMyBaggageFees:

  1. Using the passed passenger count and bag count, calculate the total excess bag fee and return that amount as a double variable. If the result is negative, return 0.

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

Homework Answers

Answer #1

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);
   }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will...
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3 choice:0-999". Create a...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a Java program to display a letter grade based on an actual grade. A =...
Write a Java program to display a letter grade based on an actual grade. A = 90-100 B = 80-89 C = 70-79 D = 60-69 F = less than 60 The program needs to do the following: 1. Create a short integer to store a grade. (See Week 3 - Things to Know - Java Data Types ) 2. Prompt for a grade ( 0-100). 3. Based on the grade given, use IF statements to display the letter grade...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...