CL X CH 7 Program
CL X CH 7 Program
PAGE-105
Programming code
public class volume
{
public double volume(double r)
{
double vol = (4 / 3.0) * (22 / 7.0) * r * r * r;
return vol;
}
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
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
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
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;
}