Page 495 - Computer science 868 Class 12
P. 495
Member Functions/Methods
Sales(….) : parameterized constructor to assign values to data members of both the
classes
void compute() : calculates the service tax @ 12.4% of the actual sale amount calculates the
fine @ 2.5% of the actual sale amount only if the amount paid by the retailer
to the wholesaler exceeds 30 days calculates the total amount paid by the
retailer as (actual sale amount + service tax + fine)
void show () : displays the data members of the superclass and the total amount
Assume that the superclass Product has been defined. Using the concept of inheritance, specify the class Sales giving the
details of the constructor (…), void compute() ) and void show(). The superclass, main function and algorithm need NOT be
written.
Ans. import java.io.*;
class Product
{
String name;
int code;
double amount;
Product(String n, int c, double p) {
name = n;
code = c;
amount = p;
}
void show() {
System.out.println("Name is :"+ name);
System.out.println("Code is :" + code);
System.out.println("Total Sale Amount: " + amount);
}
}
class Sales extends Product {
int day;
double tax;
double totamt;
double fine = 0.0;
Sales(String n, int c, double p, int d) {
super(n, c, p);
day = d;
}
void compute() {
if(day < 30){ tax = 12.4 * amount /100; totamt = amount + tax; }
if(day > 30) {
tax= 12.4 * amount /100;
fine = 2.5 * amount /100;
totamt = amount + tax + fine;
}
}
void show () {
show();
System.out.println("Total amount to be paid::"+ totamt);
}
}
493
Inheritance, Interfaces and Polymorphism 493

