Ticker

6/recent/ticker-posts

Java Week 3: Q3

/*A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main( ) method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.*/


import java.util.Scanner;
class Shape{
   double length, breadth;
   Shape(double l, double b)
{
//Constructor to initialize a Shape object
      length = l;
      breadth= b;
    }
  Shape(double len)
{
  //Constructor to initialize another Shape object
     length = breadth = len;
   }
  double calculate(){  // To calculate the area of a shape object
    return length * breadth ;
  }
}
public class Test1 extends Shape{ 
 

 double height;
Test1(double length,double h)
        //base class constructor with one parameter is called
{
super(length);
height=h;
}

Test1(double length,double breadth,double h)
        //base class constructor having two argument is called
{       
super(length,breadth);
height=h;
}
double calculate()
{   // calculate the volume of the shape
return length*breadth*height;
}

public static void main(String args[]){
Scanner sc = new Scanner(System.in);//Create an object to read input                                                         
double l=sc.nextDouble(); //Read length
double b=sc.nextDouble(); //Read breadth
double h=sc.nextDouble(); //Read height
Test1 myshape1 = new Test1(l,h);
Test1 myshape2 = new Test1(l,b,h);
double volume1;
double volume2;
volume1 = myshape1.calculate();
volume2=myshape2.calculate();
System.out.println(volume1);
System.out.println(volume2);
}
}

Post a Comment

0 Comments