Computer Applications
A private Cab service company provides service within the city at the following rates:
| AC CAR | NON AC CAR | |
|---|---|---|
| Upto 5 KM | ₹150/- | ₹120/- |
| Beyond 5 KM | ₹10/- PER KM | ₹08/- PER KM |
Design a class CabService with the following description:
Member variables /data members:
String car_type — To store the type of car (AC or NON AC)
double km — To store the kilometer travelled
double bill — To calculate and store the bill amount
Member methods:
CabService() — Default constructor to initialize data members. String data members to '' '' and double data members to 0.0.
void accept() — To accept car_type and km (using Scanner class only).
void calculate() — To calculate the bill as per the rules given above.
void display() — To display the bill as per the following format:
CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:
Create an object of the class in the main method and invoke the member methods.
Java
Java Classes
122 Likes
Answer
import java.util.Scanner;
public class CabService
{
String car_type;
double km;
double bill;
public CabService() {
car_type = "";
km = 0.0;
bill = 0.0;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter car type: ");
car_type = in.nextLine();
System.out.print("Enter kilometer: ");
km = in.nextDouble();
}
public void calculate() {
if (km <= 5) {
if (car_type.equals("AC"))
bill = 150;
else
bill = 120;
}
else {
if (car_type.equals("AC"))
bill = 150 + 10 * (km - 5);
else
bill = 120 + 8 * (km - 5);
}
}
public void display() {
System.out.println("Car Type: " + car_type);
System.out.println("Kilometer Travelled: " + km);
System.out.println("Total Bill: " + bill);
}
public static void main(String args[]) {
CabService obj = new CabService();
obj.accept();
obj.calculate();
obj.display();
}
}Output

Answered By
56 Likes