Question

In: Computer Science

Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...

Using a Java.

2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey.

The program should then calculate and display the fare of journey based on the following criteria:
Note: Use Switch...case and if...else construct

First (1) Class

Second (1) Class

Third (3) Class

First 100 mile

$ 3 per mile

$ 2 per mile

$ 1.50 per mile

Next 150 mile

$ 2.50 per mile

$ 1.50 per mile

$ 1 per mile

Remaining

$ 2 per mile

$ 1 per mile

$ 0.50 per mile

Rubric:
- Input, usage of appropriate data types - 2 points
- Appropriate usage of if...else in each case of switch construct for each class of journey : 4 points
- Documentation and appropriate variable names- 2 points

The layout of  switch case should be as follows:

switch(cls)//class

{

     case 1: if construct for calculating fare for class 1

     case 2: if construct for calculating fare for class 2

     case 3: if construct for calculating fare for class 3

     default: Display it is an invalid class

}

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.Scanner;

public class calculate_fare {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        System.out.print("Enter number of miles: ");

        int miles = scnr.nextInt();

        System.out.print("Enter class of journey: ");

        int cls = scnr.nextInt();

        double fare = 0.0;

        switch (cls) {

        case 1:

            // for first 100 miles

            if (miles <= 100)

                fare = miles * 3.00;

            else {

                fare = 100 * 3.00;

                // next 150 miles

                if ((miles - 100) <= 150)

                    fare += (miles - 100) * 2.50;

                else {

                    fare += 150 * 2.50;

                    // remaining

                    fare += (miles - 250) * 2.00;

                }

            }

            System.out.println("Total fare is: " + fare);

            break;

        case 2:

            // for first 100 miles

            if (miles <= 100)

                fare = miles * 2.00;

            else {

                fare = 100 * 2.00;

                // next 150 miles

                if ((miles - 100) <= 150)

                    fare += (miles - 100) * 1.50;

                else {

                    fare += 150 * 1.50;

                    // remaining

                    fare += (miles - 250) * 1.00;

                }

            }

            System.out.println("Total fare is: " + fare);

            break;

        case 3:

            // for first 100 miles

            if (miles <= 100)

                fare = miles * 1.50;

            else {

                fare = 100 * 1.50;

                // next 150 miles

                if ((miles - 100) <= 150)

                    fare += (miles - 100) * 1.00;

                else {

                    fare += 150 * 1.00;

                    // remaining

                    fare += (miles - 250) * 0.50;

                }

            }

            System.out.println("Total fare is: " + fare);

            break;

        default:

            System.out.println("Invalid class");

        }

    }

}


Related Solutions

C++ Write a program using switch-case-break that will take an input number as for example 2...
C++ Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
write a java program to evaluate the highest number input by user, cannot use math class,...
write a java program to evaluate the highest number input by user, cannot use math class, must use overloading . Must compute four computeHighNum methods. Here is what I have. import java.util.Scanner; public class Overload { public static void main(String[] args){ Scanner input = new Scanner(System.in); int number1 = input.nextInt(); int number2 = input.nextInt(); int number3 = input.nextInt();    //int maximum = maximum(number1, number2 ); System.out.printf("Highest integer is ");       // int number1 = input.nextInt(); // int number2 =...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Please write a java program (using dialog box statements for user input) that has the following...
Please write a java program (using dialog box statements for user input) that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled,...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT