KnowledgeBoat Logo

Computer Applications

A computerised bus charges fare from each of its passengers based on the distance travelled as per the tariff given below:

Distance (in km)Charges
First 5 km₹80
Next 10 km₹10/km
More than 15 km₹8/km

As the passenger enters the bus, the computer prompts 'Enter distance you intend to travel'. On entering the distance, it prints his ticket and the control goes back for the next passenger. At the end of journey, the computer prints the following:

  1. the number of passenger travelled
  2. total fare received

Write a program to perform the above task.
[Hint: Perform the task based on user controlled loop]

Java

Java Iterative Stmts

ICSE

39 Likes

Answer

import java.util.Scanner;

public class KboatBusTravel
{
    public void busTravel() {
        
        Scanner in = new Scanner(System.in);
        int dist, tf = 0, tp = 0;
        System.out.println("Enter distance as -1 to complete the journey");
        while (true) {
            System.out.print("Enter distance you intend to travel: ");
            dist = in.nextInt();
            int f = 0;
            if (dist == -1) {
                break;
            }
            else if (dist <= 5) {
                f = 80;   
            }
            else if (dist <= 15) {
                f = 80 + ((dist - 5) * 10);   
            }
            else {
                f = 80 + 100 + ((dist - 15) * 8);  
            }
            
            tf += f;
            tp++;
            
            System.out.println("Your fare is " + f);
        }
        
        System.out.println("Total Passengers: " + tp);
        System.out.println("Total Fare: " + tf);
    }
}

Output

BlueJ output of A computerised bus charges fare from each of its passengers based on the distance travelled as per the tariff given below: As the passenger enters the bus, the computer prompts 'Enter distance you intend to travel'. On entering the distance, it prints his ticket and the control goes back for the next passenger. At the end of journey, the computer prints the following: (a) the number of passenger travelled (b) total fare received Write a program to perform the above task. [Hint: Perform the task based on user controlled loop]

Answered By

17 Likes


Related Questions