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

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...
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 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...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
In java Create a program to calculate interest rate and total balance - the program consists...
In java Create a program to calculate interest rate and total balance - the program consists of (a) A class that encapsulates the interest rate and total balance calculation Constructor should accept initial balance and interest rate Methods: Returns interest rate Returns total balance Set interest rate Set initial balance (b) A test program that uses class from step (A) to perform the following: Set interest rate to 5% and initial balance to 1000 Print interest amount and total balance...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT