Detail Inheritance ISC Computer Science 2012 Theory
A super class Detail has been defined to store the details of a customer. Define a sub class Bill to compute the monthly telephone charge of the customer as per the chart given below:
NUMBER OF CALLS | RATE |
---|---|
1 - 100 | Only rental charge |
101 - 200 | 60 paise per call + rental charge |
201 - 300 | 80 paise per call + rental charge |
Above 300 | 1 rupee per call + rental charge |
The details of both the classes are given below:
Class name: Detail
Data members/instance variables:
name: to store the name of the customer
address: to store the address of the customer
telno: to store the phone number of the customer
rent: to store the monthly rental charge
Member functions:
Detail(...): parameterized constructor to assign values to data members
void show(): to display the details of the customer
Class name: Bill
Data members/instance variables:
n: to store the number of calls
amt: to store the amount to be paid by the customer
Member functions:
Bill(...): parameterized constructor to assign values to data members of both classes and to initialize amt = 0.0
void cal(): calculates the monthly telephone charge as per the chart given above
void show(): displays the details of the customer and amount to be paid
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.
class Detail{
protected String name;
protected String address;
protected int telno;
protected double rent;
public Detail(String n, String a, int t, double r){
name = n;
address = a;
telno = t;
rent = r;
}
public void show(){
System.out.println("Customer name: " + name);
System.out.println("Address: " + address);
System.out.println("Telephone No: " + telno);
System.out.println("Monthly rent: " + rent);
}
}
class Bill extends Detail{
int n;
double amt;
public Bill(String n, String a, int t, double r, int num){
super(n, a, t, r);
this.n = num;
amt = 0.0;
}
public void cal(){
if(n <= 100)
amt = rent;
else if(n <= 200)
amt = 0.6 * n + rent;
else if(n <= 300)
amt = 0.8 * n + rent;
else
amt = n + rent;
}
public void show(){
super.show();
System.out.println("Number of calls: " + n);
System.out.println("Amount: " + amt);
}
}
class Telephone{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Customer name: ");
String n = in.nextLine();
System.out.print("Address: ");
String a = in.nextLine();
System.out.print("Telephone number: ");
int t = Integer.parseInt(in.nextLine());
System.out.print("Rent: ");
double r = Double.parseDouble(in.nextLine());
System.out.print("Number of calls: ");
int num = Integer.parseInt(in.nextLine());
Bill obj = new Bill(n, a, t, r, num);
obj.cal();
obj.show();
}
}
Comments
Post a Comment