Question

In: Computer Science

Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....

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

  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

Grading item: Avoid using Magic Numbers

Solutions

Expert Solution

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

  • Program declare variables to hold number of passengers,number of bags and fee
  • Program prompt user to enter number of passengers and total number of bags and read these values into variables
  • calculateMyBaggageFees() method is called to calculate luggage fees
  • This method find freeBagsAllowed by multiplying number of passenger by 2
  • Then excessBagFee is calculated by multiplying excess bags(nBags - freeBagsAllowed) with 75
  • If excessBagFee is negative,zero is returned otherwise excessBagFee is returned.
  • The result is printed from main() method
  • For example if number of passengers are 3 and total bags are 11.Then free bags allowed are 6 (3x2)
  • Excess bags are 5 (11-6)
  • Excess bag fee is 5x75=375

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


Related Solutions

Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE 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" through "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...
Using eclipse IDE, create a java project and call it applets. Create a package and call...
Using eclipse IDE, create a java project and call it applets. Create a package and call it multimedia. Download an image and save it in package folder. In the package, create a java applet where you load the image. Use the drawImage() method to display the image, scaled to different sizes. Create animation in Java using the image. In the same package folder, download a sound. Include the sound in your applets and make it repeated while the applet executes....
Task #1 The if Statement, Comparing Strings, and Flags (JAVA using Eclipse IDE 14) Create the...
Task #1 The if Statement, Comparing Strings, and Flags (JAVA using Eclipse IDE 14) Create the file PizzaOrder.java. Prompt the user to input the type of pizza that they want to order. In the end, you should print out the final order and price. Here is a sample output. Welcome to May and Adam’s Pizzeria Enter your first name: Amy Pizza Size(inches)     Cost         10            $10.99         12            $12.99         14            $14.99         16            $16.99 What size pizza would you...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write 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...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT