Telephone Bill Inheritance Program in Java and Python
ISC 2012 inheritance solution using superclass Detail and subclass Bill with algorithm, explanation, Java program and simple Python program.
Question:
A superclass Detail has been defined to store the details of a customer. Define a subclass Bill to compute the monthly telephone charge of the customer as per the chart given below:
The details of both the classes are given below:
Specify the class Detail giving details of the constructor and void show(). Using the concept of inheritance, specify the class Bill giving details of the constructor, void cal() and void show().
The main function and algorithm need not be written in the original theory answer, but a main method is included below to show how the classes can be executed.
Algorithm:
Step 1: Start.
Step 2: Define a superclass named Detail.
Step 3: Declare variables to store customer name, address, telephone number and monthly rent.
Step 4: In the Detail constructor, accept values and assign them to the data members.
Step 5: In show() of Detail, display customer details and monthly rental charge.
Step 6: Define a subclass named Bill that extends Detail.
Step 7: Declare n to store number of calls and amt to store the bill amount.
Step 8: In the Bill constructor, call the superclass constructor using super(), store the number of calls and initialize amt to 0.0.
Step 9: In cal(), if calls are from 1 to 100, assign only the rent to amt.
Step 10: If calls are from 101 to 200, calculate amt = 0.6 * n + rent.
Step 11: If calls are from 201 to 300, calculate amt = 0.8 * n + rent.
Step 12: If calls are above 300, calculate amt = 1.0 * n + rent.
Step 13: In show() of Bill, call super.show(), then display the number of calls and amount to be paid.
Step 14: In main(), accept all data, create a Bill object, call cal() and then call show().
Step 15: Stop.
Explanation:
This program uses inheritance because customer details and billing details are related but not identical. The superclass Detail stores general information about the customer: name, address, telephone number and monthly rent. These values describe the customer and are useful irrespective of how the bill is calculated. The subclass Bill extends Detail because a telephone bill needs all these customer details along with extra information such as the number of calls and the final amount.
The constructor of Detail receives the basic customer data and assigns it to the instance variables. Its show() method displays only the customer-related details. The subclass constructor receives all values, including the number of calls. It uses super(n1, a1, t1, r1) to pass the inherited values to the superclass constructor. This keeps initialization organized: the superclass initializes its own fields, while the subclass initializes n and amt.
The cal() method applies the rate chart. If the number of calls is from 1 to 100, only the rental charge is paid. If the calls fall between 101 and 200, the bill is calculated as 60 paisa per call plus rent, so the program uses 0.6 * n + rent. For 201 to 300 calls, the rate becomes 80 paisa per call, and above 300 calls it becomes 1 rupee per call. The calculation follows the selected slab rate for the total number of calls.
The subclass show() method demonstrates method overriding. It first calls super.show() so that the customer details are displayed by the superclass method. Then it adds subclass-specific output: number of calls and the amount to be paid. This avoids rewriting the customer display code in the subclass and shows how inherited behavior can be extended. In main(), the program accepts all input, creates a Bill object, calculates the bill and then displays the complete result.
Java Program:
/**
* The superclass Detail stores customer details.
* The subclass Bill calculates the monthly telephone bill.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2012 Question 12
*/
import java.util.Scanner;
class Detail
{
String name;
String address;
long telno;
double rent;
Detail(String n1, String a1, long t1, double r1)
{
name = n1;
address = a1;
telno = t1;
rent = r1;
}
void show()
{
System.out.println("Name of customer = " + name);
System.out.println("Address = " + address);
System.out.println("Telephone Number = " + telno);
System.out.println("Monthly Rental = Rs. " + rent);
}
}
class Bill extends Detail
{
int n;
double amt;
Bill(String n1, String a1, long t1, double r1, int c)
{
super(n1, a1, t1, r1); // Initialize inherited customer details.
n = c;
amt = 0.0;
}
void cal()
{
/*
* The amount is calculated according to the slab in which
* the total number of calls falls.
*/
if(n >= 1 && n <= 100)
amt = rent;
else if(n >= 101 && n <= 200)
amt = 0.6 * n + rent;
else if(n >= 201 && n <= 300)
amt = 0.8 * n + rent;
else
amt = 1.0 * n + rent;
}
void show()
{
super.show(); // Display customer details from the superclass.
System.out.println("No. of calls = " + n);
System.out.println("Amount to be paid = Rs. " + amt);
}
}
public class Question12_ISC2012
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name: ");
String n1 = sc.nextLine();
System.out.print("Enter the address: ");
String a1 = sc.nextLine();
System.out.print("Enter the telephone number: ");
long t1 = sc.nextLong();
System.out.print("Enter the monthly rental: ");
double r1 = sc.nextDouble();
System.out.print("Enter the number of calls: ");
int c = sc.nextInt();
Bill ob = new Bill(n1, a1, t1, r1, c);
System.out.println("*** Output ***");
ob.cal();
ob.show();
}
}Equivalent Python Program:
class Detail:
def __init__(self, n1, a1, t1, r1):
self.name = n1
self.address = a1
self.telno = t1
self.rent = r1
def show(self):
print("Name of customer =", self.name)
print("Address =", self.address)
print("Telephone Number =", self.telno)
print("Monthly Rental = Rs.", self.rent)
class Bill(Detail):
def __init__(self, n1, a1, t1, r1, c):
super().__init__(n1, a1, t1, r1) # Initialize inherited details.
self.n = c
self.amt = 0.0
def cal(self):
# Apply the rate according to the call slab.
if self.n >= 1 and self.n <= 100:
self.amt = self.rent
elif self.n >= 101 and self.n <= 200:
self.amt = 0.6 * self.n + self.rent
elif self.n >= 201 and self.n <= 300:
self.amt = 0.8 * self.n + self.rent
else:
self.amt = 1.0 * self.n + self.rent
def show(self):
super().show()
print("No. of calls =", self.n)
print("Amount to be paid = Rs.", self.amt)
n1 = input("Enter the name: ")
a1 = input("Enter the address: ")
t1 = int(input("Enter the telephone number: "))
r1 = float(input("Enter the monthly rental: "))
c = int(input("Enter the number of calls: "))
ob = Bill(n1, a1, t1, r1, c)
print("*** Output ***")
ob.cal()
ob.show()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.