Program opened below. You can scroll through the algorithm, Java code and output.
Program 51
Telephone Bill Using Inheritance
Question
Calculate a telephone bill using inheritance. As the source PDF does not clearly specify the tariff in its extracted question, the example assumes a fixed rental of Rs. 200 and Rs. 1 per call above 100 calls.
Algorithm
Step 1: Start the program and accept the customer's name and number of calls.
Step 2: Store the customer's details in the base class.
Step 3: Create a derived class that inherits the customer details.
Step 4: Set the fixed rental charge according to the tariff stated in the question.
Step 5: Determine the number of calls, if any, that exceed the free-call allowance.
Step 6: Calculate the additional charge for calls beyond the allowance.
Step 7: Add the additional charge to the fixed rental.
Step 8: Store the final bill amount.
Step 9: Display the customer's name.
Step 10: Display the number of calls and the calculated bill on separate lines.
Step 11: Stop the program.
Java program
Javaimport java.util.*;
class Customer
{
String name;
int calls;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Customer name: ");
name = sc.nextLine();
System.out.print("Number of calls: ");
calls = sc.nextInt();
}
}
class TelephoneBill extends Customer
{
void display()
{
// Assumed tariff: Rs. 200 rental includes 100 calls.
// Each additional call costs Rs. 1.
double bill = 200.0;
if (calls > 100)
{
bill += (calls - 100) * 1.0;
}
System.out.println("Customer: " + name);
System.out.println("Calls: " + calls);
System.out.println("Bill amount: Rs. " + bill);
}
public static void main(String[] args)
{
TelephoneBill ob = new TelephoneBill();
ob.input();
ob.display();
}
}Sample output
Input: Customer name: Asha; Number of calls: 120
Output: Customer: Asha
Calls: 120
Bill amount: Rs. 220.0