Page 670 - Computer science 868 Class 12
P. 670
A derived class Roots are defined to calculate the roots of the quadratic equation (if possible) with appropriate message. The
details of the class is as follows
Class name : Roots
Data Members
double r1,r2 : To store the roots
Members Methods
Roots(……) : Parameterised constructor to initialise variables of base and derived
class
void calculate() : Calculate the roots r1 and r2 of the equation using formula
4
b
−± b 2 − ac
2 a
along with appropriate message as given below:
d<0 : Roots are imaginary
d=0 : Roots are real and equal
d>0 : Roots are real and unequal
void display() : Display the roots r1 and r2 and the base class data.
Using the concept of inheritance specify the class Roots details of the constructor and the member function void calculate() and
void display(). Base class and main method need not be written
Ans. class Roots extends Quadratic
{ double r1,r2;
Roots(double a1,double b1,double c1) //constructor
{ super(a1,b1,c1);
r1=r2=0;
}
void calculate()
{ super.calculate();
if( d<0)
System.out.println("Roors are imaginary");
else
{ if(d==0)
{ r1= -b/(2*a);
r2=r1;
System.out.println("Roors are real and equal "+r1+" "+r2);
}
else
{ r1=( -b+Math.sqrt(d))/(2*a);
r2=( -b-Math.sqrt(d))/(2*a);
System.out.println(“Roors are real and unequal "+r1+" "+r2);
}}}
void display()
{ super.display();
calculate();
}}
668668 Touchpad Computer Science-XII

