Question

create a java code with scanner and formatt Write a generic program to find the total...

create a java code with scanner and formatt

Write a generic program to find the total cost of the business trip by creating a class file called lastname_trip_cost.java. Add three instance variables for the type of airline ticket, the type of hotel and the total number of nights staying at the hotel. Add methods to return values for

* the cost of airline ticket

* total number of days less than complete weeks

* total number of whole weeks

* cost of hotel

Create a file called lastname_trip_cost_demo.java. In this file add an object to pass the three values to the class. Add statements to print the values shown in the output.

Round trip airfare rates from New York to Orlando   
Economy class $300.00   
Business class $800.00   
First class $1,000.00   

Hotel Rates Daily Rate Weekly
3 Star Hotel $120.00 $750.00
4 Star Hotel $160.00 $1,000.00
5 Star Hotel $200.00 $1,250.00

User Input:   
Enter the type of airline ticket - 1 for Economy, 2 for business and 3 for First class = 2
Enter the type of hotel - 1 for 3 star, 2 for 4 star and 3 for 5 star = 1
Enter the total number of nights staying at the hotel = 15

Expected Output:   
Cost of airline ticket = $800.00
Total number of days less than complete weeks = 15 - 7(2) = 1
Total number of whole weeks = 2
Cost of hotel = 750(2) + 120(1) = $1620
Total cost = 800 + 1620 = $2420

Homework Answers

Answer #1

// trip_cost.java

public class trip_cost
{
   // attributes
   private String airline_ticket;
   private int hotel_type;
   private int total_number_of_nights;
  
   // constructor to initialize the attributes
   public trip_cost(String airline_ticket, int hotel_type, int total_number_of_nights)
   {
       this.airline_ticket = airline_ticket;
       this.hotel_type = hotel_type;
       this.total_number_of_nights = total_number_of_nights;
   }
  
   // method to return the cost of airline ticket based on the type of the ticket
   public double getCostOfTicket()
   {
       if(airline_ticket.equalsIgnoreCase("Economy"))
           return 300;
       else if(airline_ticket.equalsIgnoreCase("Business"))
           return 800;
       else
           return 1000;
   }

   // method to return the number of days stayed in hotel which is less than complete weeks
   public int daysLessThanCompleteWeeks()
   {
       return total_number_of_nights%7;
   }
  
   // method to return the number of complete weeks stayed in the hotel
   public int completeWeeks()
   {
       return total_number_of_nights/7;
   }
  
   // method to calculate and return the total hotel cost
   public double hotelCost()
   {
       // hotel cost is determined by the type of hotel and number of whole weeks and number of days less than whole weeks
       if(hotel_type == 3)
           return (completeWeeks()*750 + daysLessThanCompleteWeeks()*120);
       else if(hotel_type == 4)
           return (completeWeeks()*1000 + daysLessThanCompleteWeeks()*160);
       else
           return (completeWeeks()*1250 + daysLessThanCompleteWeeks()*200);
   }
}

//end of trip_cost.java

// trip_cost_demo.java

import java.util.Scanner;

public class trip_cost_demo {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       int choice;
       String airline_ticket;
       int hotel_type, number_of_nights;
       // input the type of airline ticket choice
       System.out.print("Enter the type of airline ticket - 1 for Economy, 2 for business and 3 for First class: ");
       choice = scan.nextInt();
       // based on ticket type choice determine the ticket type
       if(choice == 1)
           airline_ticket = "Economy";
       else if(choice == 2)
           airline_ticket = "Business";
       else
           airline_ticket = "First";
      
       // input the hotel type choice
       System.out.print("Enter the type of hotel - 1 for 3 star, 2 for 4 star and 3 for 5 star: ");
       choice = scan.nextInt();
      
       // based on hotel choice, determine hotel type
       if(choice == 1)
           hotel_type = 3;
       else if(choice == 2)
           hotel_type = 4;
       else
           hotel_type = 5;
      
       // input the number of nights the user stayed
       System.out.print("Enter the total number of nights staying at the hotel: ");
       number_of_nights = scan.nextInt();
      
       // create a new object of trip_cost
       trip_cost trip = new trip_cost(airline_ticket, hotel_type, number_of_nights);
      
       // get the airline cost
       double airline_cost = trip.getCostOfTicket();
       // get the total hotel cost
       double hotel_cost = trip.hotelCost();
      
       // display the summary data containing all information
       System.out.printf("\nCost of airline ticket: $%.2f",airline_cost);
       System.out.printf("\nTotal number of days less than complete weeks: %d",trip.daysLessThanCompleteWeeks());
       System.out.printf("\nTotal number of whole weeks: %d",trip.completeWeeks());
       System.out.printf("\nCost of hotel: $%.2f",hotel_cost);
       System.out.printf("\nTotal cost: $%.2f",airline_cost+hotel_cost);
      
   }

}

//end of trip_cost_demo.java

Output:

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
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
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(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers on a ticket....
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
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,...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and Jelly Sandwiches. The program must create two sandwiches based on user input. The sandwich information for both must then print out their details and determine if the two sandwiches are equal. Requirements: Write a class called Bread with the following Instance Variables Name: The name brand of the bread. o   Calories: The number of calories per slice assumed to be between 50 and 250 inclusively....
EVERYTHING HAS TO BE DONE IN C#. (I need pseudocode and source code please) Problem Statement:...
EVERYTHING HAS TO BE DONE IN C#. (I need pseudocode and source code please) Problem Statement: Write an abstract class called Vacation that includes three class variables named: cost, budget, and destination. It has an abstract method, KeptToBudget, returning how much the vacation is over or under budget using cost. This class has two non-abstract subclasses, each with their own class variables: All-Inclusive Vacation: brand (such as ClubMed, Delta Vacations, etc.) rating (number of stars) total cost (all-inclusive means that...
Write a complete C++ program as described below. Documentation is required; see section. Starter code and...
Write a complete C++ program as described below. Documentation is required; see section. Starter code and input files are given to you. Please go through them on Titanium before beginning to program. Each item that is scanned at a cash register reduces the grocery store’s inventory – i.e., the number of items decreases by one. Your program should code this logic. A text file contains the initial inventory (i.e., number of items) of every type of item carried in the...
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...