Search

# Taxi Meter in Java | AskTheCode

Taxi Meter question in Java | #CodeChum | Solutions

Question: It's the middle of the night, you just finished your shift, and the rain is pouring. Luckily, you find a vacant taxi. As you attempt to go in, the driver stops you and tells you that he can't take you anywhere because his taxi meter is broken. Since, you desperately want to go home, you make a deal with the taxi driver. You propose to manually calculate the total fare of your ride, luckily enough the driver trusts you and explains you how the fair is calculated. The taxi has a base fee of P40.00 for the first 250 meters. An additional P2.50 is added for every succeeding 200 meters. Compute and print the total fare that you would need to pay.

Input Format:

A single line containing the total distance traveled.

Input Sample:

250

Output Format:

A single line containing the total cost of the ride.

Output Sample:

P40.00

Code:

import java.util.Scanner;

public class TaxiMeter{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the total distance: ");

int totalDistance = sc.nextInt();

double cost;

if (totalDistance <= 250) {

if (totalDistance == 0) {

cost = 0.00;

System.out.println("Cost= P"+ cost);

}

else{

cost = 40.00;

System.out.println("Cost= P"+ cost);

}

}

else{

totalDistance = totalDistance - 250;

cost = 40;

if (totalDistance % 200 == 0) {

double costPer200Meter = (totalDistance / 200) * 2.50;

cost += costPer200Meter;

System.out.println("Total cost= P"+cost);

}

else{

double costPer200Meter = ((totalDistance / 200) * 2.50) + 2.50;

cost += costPer200Meter;

System.out.println("Total cost= P"+cost);

}

}

}

}

### Recent Posts

See All

#### Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

#### Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

#### Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes