Computer Science, asked by anushkaverma040, 3 months ago

Question 4.
[15]
private Cab service company provides service within the city at the following rates:
AC CAR
UPTO 5 KM
150/-
BEYOND SKM
10/-PER KM
Design a class CabService with the following description:
Member variables data members
NON AC CAR
120 -
08/- PER KM
String car_type
double km
To store the type of car (AC or NON AC)
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.​

Answers

Answered by mahinderjeetkaur878
0

Here's a possible implementation of the CabService class in Java:

import java.util.Scanner;

public class CabService {

private String car_type;

private double km;

private double bill;

private static final double AC_BASE_FARE = 150.0;

private static final double AC_RATE_PER_KM = 10.0;

private static final double NON_AC_BASE_FARE = 120.0;

private static final double NON_AC_RATE_PER_KM = 8.0;

public CabService() {

car_type = "";

km = 0.0;

bill = 0.0;

}

public void accept() {

Scanner input = new Scanner(System.in);

System.out.print("Enter car type (AC or NON AC): ");

car_type = input.nextLine().toUpperCase();

System.out.print("Enter distance travelled in km: ");

km = input.nextDouble();

}

public void calculate() {

if (car_type.equals("AC")) {

bill = AC_BASE_FARE + km * AC_RATE_PER_KM;

} else if (car_type.equals("NON AC")) {

bill = NON_AC_BASE_FARE + km * NON_AC_RATE_PER_KM;

}

}

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 cab = new CabService();

cab.accept();

cab.calculate();

cab.display();

}

}

  • The CabService class has four instance variables (car_type, km, bill) and four static final variables (AC_BASE_FARE, AC_RATE_PER_KM, NON_AC_BASE_FARE, NON_AC_RATE_PER_KM) to store the data members and the fare rates for AC and NON AC cars.
  • The accept() method accepts the car type and distance travelled from the user using the Scanner class. The calculate() method calculates the bill amount based on the car type and distance travelled using the fare rates. The display() method displays the car type, distance travelled, and the total bill amount in the required format.
  • Finally, the main() method creates an object of the CabService class, calls the accept(), calculate(), and display() methods on the object to input the data, calculate the bill amount, and display the results.

To know more: -

https://brainly.in/question/1983482?referrer=searchResults

https://brainly.in/question/3041613?referrer=searchResults

#SPJ1

Similar questions