KnowledgeBoat Logo

Computer Applications

Design a class to overload a method volume( ) as follows:

  1. double volume(double r) — with radius (r) as an argument, returns the volume of sphere using the formula:
    V = (4/3) * (22/7) * r * r * r
  2. double volume(double h, double r) — with height(h) and radius(r) as the arguments, returns the volume of a cylinder using the formula:
    V = (22/7) * r * r * h
  3. double volume(double 1, double b, double h) — with length(l), breadth(b) and height(h) as the arguments, returns the volume of a cuboid using the formula:
    V = l*b*h ⇒ (length * breadth * height)

Java

User Defined Methods

ICSE 2018

152 Likes

Answer

public class KboatVolume
{
    double volume(double r) {
        double vol = (4 / 3.0) * (22 / 7.0) * r * r * r;
        return vol;
    }
    
    double volume(double h, double r) {
        double vol = (22 / 7.0) * r * r * h;
        return vol;
    }
    
    double volume(double l, double b, double h) {
        double vol = l * b * h;
        return vol;
    }
    
    public static void main(String args[]) {
        KboatVolume obj = new KboatVolume();
        System.out.println("Sphere Volume = " + 
            obj.volume(6));
        System.out.println("Cylinder Volume = " + 
            obj.volume(5, 3.5));
        System.out.println("Cuboid Volume = " + 
            obj.volume(7.5, 3.5, 2));
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Design a class to overload a function volume( ) as follows: (a) double volume(double r) — with radius (r) as an argument, returns the volume of sphere using the formula: V = (4/3) * (22/7) * r * r * r (b) double volume(double h, double r) — with height(h) and radius(r) as the arguments, returns the volume of a cylinder using the formula: V = (22/7) * r * r * h (c) double volume(double 1, double b, double h) — with length(l), breadth(b) and height(h) as the arguments, returns the volume of a cuboid using the formula: V = l*b*h ⇒ (length * breadth * height)

Answered By

57 Likes


Related Questions