NPTEL » Programming In Java Week 3 : Programming Assignment 3 Aug 2022

 

Week 3 : Programming Assignment 3

Due on 2022-08-18, 23:59 IST

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{


    //Template code:

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() {

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);

}

}




No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...