ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2020 ICSE Computer Science Paper



Share with a Friend

Solved 2020 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Class & Object Based Program - Cab Service - ICSE 2020 Computer Science

Question 4
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 carType – 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 carType 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.

 

import java.util.Scanner; class CabService{ String carType; double km; double bill; public CabService(){ carType = ""; km = 0.0; bill = 0.0; } public void accept(){ Scanner in = new Scanner(System.in); System.out.print("Car type: "); carType = in.nextLine().toUpperCase(); System.out.print("Distance travelled: "); km = Double.parseDouble(in.nextLine()); } public void calculate(){ if(km <= 5){ if(carType.startsWith("AC")){ carType = "AC CAR"; bill = 150.0; } else if(carType.startsWith("NON")){ carType = "NON AC CAR"; bill = 120.0; } } else{ if(carType.startsWith("AC")){ carType = "AC CAR"; bill = 150.0 + (km - 5) * 10; } else if(carType.startsWith("NON")){ bill = 120.0 + (km - 5) * 8; carType = "NON AC CAR"; } } } public void display(){ System.out.println("CAR TYPE: " + carType); 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

 
 OUTPUT 1: 
Car type: AC
Distance travelled: 20
CAR TYPE: AC CAR
KILOMETER TRAVELLED: 20.0
TOTAL BILL: 300.0

 OUTPUT 2: 
Car type: NON AC
Distance travelled: 4
CAR TYPE: NON AC CAR
KILOMETER TRAVELLED: 4.0
TOTAL BILL: 120.0