Page 257 - CA_Blue( J )_Class10
P. 257
(iii) double volume(double l, 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 [2018]
Ans. class overload
{
double volume(double r)
{
return 4.0 / 3 * 22.0 / 7 * r * r * r;
}
double volume(double h, double r)
{
return 22.0 / 7 * r * r * h;
}
double volume(double l, double b, double h) {
return l * b * h;
}
public static void main()
{
overload obj = new overload();
System.out.println(obj.volume(5));
System.out.println(obj.volume(4,6));
System.out.println(obj.volume(3,2,1));
}
}
13. Design a class to overload a function check() as follows: [2017]
void check(String str, char ch) – to find and print the frequency of a character in a string.
Example:
INPUT: str = "success" ch = 's'
OUTPUT: Number of s present is = 3.
void check(String s1) – to display only vowels from s1, after converting it to lowercase.
Example:
INPUT:
s1 = "computer"
OUTPUT:
o u e
Ans. class Overload_string
{
public static void check(String str, char ch){
int count = 0,i;
for(i = 0; i < str.length(); i++){
if(ch == str.charAt(i))
count++;
}
System.out.println("Number of "+ ch + " present is= " + count);
}
public static void check(String s1) {
char ch;int i;
s1 = s1.toLowerCase();
for(i = 0; i < s1.length(); i++)
{
ch = s1.charAt(i);
if(ch=='a' || ch=='e' || ch=='i' || ch=='0' || ch=='u')
{ System.out.print(ch + " "); }
}
}
public static void main()
{
Overload_string ob= new Overload_string();
ob.check("India is my country",'i');
255
User-defined Methods 255

