0% found this document useful (0 votes)
13 views7 pages

CL X CH 7 Program

Program java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

CL X CH 7 Program

Program java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CHAPTER-7

PAGE-105

B.(1) Design a class to overload a function 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

V = l*b*h ⇒ (length * breadth * height)


height(h) as the arguments, returns the volume of a cuboid using the formula:

Programming code
public class volume
{
public double volume(double r)
{
double vol = (4 / 3.0) * (22 / 7.0) * r * r * r;
return vol;
}

public double volume(double h, double r)


{
double vol = (22 / 7.0) * r * r * h;
return vol;
}

public double volume(double l, double b, double h)


{
double vol = l * b * h;
return vol;
}

public static void main()


{
volume obj = new volume();
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));
}
}

OUTPUT
Sphere Volume = 905.142857142857
Cylinder Volume = 192.5
Cuboid Volume = 52.5

(2) Write a menu driven program to display the pattern as per user’s choice.

Pattern 1

ABCDE
ABCD
ABC
AB
A

Pattern 2

B
LL
UUU
EEEE

For an incorrect option, an appropriate error message should be displayed.

Programming code
import java.util.Scanner;
public class Pattern
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for pattern 1");
System.out.println("Enter 2 for Pattern 2");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice)
{
case 1:
for (int i = 69; i >= 65; i--)
{
for (int j = 65; j <= i; j++)
{
System.out.print((char)j);
}
System.out.println();
}
break;
case 2:
String word = "BLUE";
int len = word.length();
for(int i = 0; i < len; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(word.charAt(i));
}
System.out.println();
}
break;
default:
System.out.println("Incorrect choice");
break;

}
}
}
OUTPUT
Enter 1 for pattern 1
Enter 2 for Pattern 2
Enter your choice: 1
ABCDE
ABCD
ABC
AB
A
Enter 1 for pattern 1
Enter 2 for Pattern 2
Enter your choice: 3
Incorrect choice
Enter 1 for pattern 1
Enter 2 for Pattern 2
Enter your choice: 2
B
LL
UUU
EEEE

(4) Design a class to overload a function Joystring( ) as follows:

1. void Joystring(String s, char ch1, char ch2) with one string argument and two character
arguments that replaces the character argument ch1 with the character argument ch2
in the given String s and prints the new string.
Example:
Input value of s = "TECHNALAGY"
ch1 = 'A'
ch2 = 'O'
Output: "TECHNOLOGY"
2. void Joystring(String s) with one string argument that prints the position of the first
space and the last space of the given String s.
Example:
Input value of s = "Cloud computing means Internet based computing"
Output:
First index: 5
Last Index: 36
3. void Joystring(String s1, String s2) with two string arguments that combines the two
strings with a space between them and prints the resultant string.
Example:
Input value of s1 = "COMMON WEALTH"
Input value of s2 = "GAMES"
Output: COMMON WEALTH GAMES

(Use library functions)


Programming code 
public class StringOverload
{
public void joystring(String s, char ch1, char ch2)
{
String newStr = s.replace(ch1, ch2);
System.out.println(newStr);
}
public void joystring(String s)
{
int f = s.indexOf(' ');
int l = s.lastIndexOf(' ');
System.out.println("First index: " + f);
System.out.println("Last index: " + l);
}
public void joystring(String s1, String s2)
{
String newStr = s1.concat(" ").concat(s2);
System.out.println(newStr);
}

public static void main()


{
StringOverload obj = new StringOverload();
obj.joystring("TECHNALAGY", 'A', 'O');
obj.joystring("Cloud computing means Internet based computing");
obj.joystring("COMMON WEALTH", "GAMES");
}
}
Output 
TECHNOLOGY
First index: 5
Last index: 36

COMMON WEALTH GAMES

(5) Design a class to overload a function area( ) as follows:

1. double area (double a, double b, double c) with three double arguments, returns the
area of a scalene triangle using the formula:
area = √(s(s-a)(s-b)(s-c))
where s = (a+b+c) / 2
2. double area (int a, int b, int height) with three integer arguments, returns the area of a
trapezium using the formula:
area = (1/2)height(a + b)
3. double area (double diagonal1, double diagonal2) with two double arguments, returns
the area of a rhombus using the formula:
area = 1/2(diagonal1 x diagonal2)
Programming code 
import java.util.Scanner;
public class area
{
double area(double a, double b, double c)
{
double s = (a + b + c) / 2;
double x = s * (s-a) * (s-b) * (s-c);
double result = Math.sqrt(x);
return result;
}
double area (int a, int b, int height)
{
double result = (1.0 / 2.0) * height * (a + b);
return result;
}

double area (double diagonal1, double diagonal2)


{
double result = 1.0 / 2.0 * diagonal1 * diagonal2;
return result;
}
public static void main()
{
area obj =new area();
System.out.println(" Area of Scalene triangle "+obj.area(10.01,2.36,9.45));
System.out.println(" Area of Trapezium "+obj.area(11.55,14.59));
System.out.println(" Area of Rhombus "+obj.area(13.66,78.55));
}
}
Output 
Area of Scalene triangle 11.07115969535261
Area of Trapezium 84.25725
Area of Rhombus 536.4965

You might also like