Create a class Cuboid having default and parameterized constructors. Calculate volume for three objects of class Cuboid instantiated using default and parameterized constructor respectively.

 

AIM:  Create a class Cuboid having default and parameterized constructors. Calculate volume for three objects of class Cuboid instantiated using default and parameterized constructor respectively.

 

CODE:

import java.util.Scanner;

public class Cuboid{

            int length,width,height,volume;

           

            Cuboid(){

                        length=1;

                        width=1;

                        height=1;

            }

            Cuboid(int length, int width, int height){

                        this.length=length;

                        this.width=width;

                        this.height=height;

            }

            void volume(){

                        volume=length*width*height;

                        System.out.println("\nLength: "+length+" Width: "+width+" Height: "+height);

                        System.out.println("Volume of Cuboid is: "+volume);

            }

            public static void main(String args[]){

                        int length,width,height;

                        //non-paramitrized

                        System.out.println("\n First cuboid");

                        Cuboid c1 = new Cuboid();

                        c1.volume();

                       

                        System.out.println("\n Second cuboid");

                        Scanner k = new Scanner(System.in);

                        System.out.print("\nEnter length, width, height of cuboid: ");

                        length=k.nextInt();

                        width=k.nextInt();

                        height=k.nextInt();

                        //paramitrized

                        Cuboid c2=new Cuboid(length,width,height);

                        c2.volume();

                        //paramitrized

                       

                        System.out.println("\n Third cuboid");

                        Cuboid c3 = new Cuboid (42,21,11);

                        c3.volume();

            }

}

 

OUTPUT:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program that illustrates interface inheritance. Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. The class Q implements P12. Instantiate Q and invoke each of its methods. Each method displays one of the constants.