Computer Applications
Design a class to overload a function manip() as follows:
- void manip(String str, int p) with one String argument and one integer argument. It displays the characters of even positions of String, if p is an even number otherwise, it displays the characters of odd positions.
- void manip(int a, char ch) with one integer argument and one character argument. It computes the square root of the integer arguments if ch is 's', else it computes the cube root of the integers.
Java
User Defined Methods
8 Likes
Answer
import java.util.Scanner;
public class KboatManip
{
public void manip(String str, int p) {
int len = str.length();
int s = 0;
if (p % 2 == 0) {
s = 1;
}
for (int i = s; i < len; i = i + 2) {
char ch = str.charAt(i);
System.out.println(ch);
}
}
public void manip(int a, char ch) {
double res = 0;
if (ch == 's') {
res = Math.sqrt(a);
}
else {
res = Math.cbrt(a);
}
System.out.println(res);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
KboatManip obj = new KboatManip();
System.out.print("Enter string: ");
String s = in.nextLine();
System.out.println("P is even");
obj.manip(s, 8);
System.out.println("\nP is odd");
obj.manip(s, 9);
System.out.print("Enter a number: ");
int num = in.nextInt();
System.out.println("\nch is 's'");
obj.manip(num, 's');
System.out.println("\nch is not 's'");
obj.manip(num, 'z');
}
}
Variable Description Table
Program Explanation
Output

Answered By
1 Like
Related Questions
Design a class to overload a method series( ) as follows:
- double series(double n) with one double argument and returns the sum of the series.
sum = (1/1) + (1/2) + (1/3) + ………. + (1/n) - double series(double a, double n) with two double arguments and returns the sum of the series.
sum = (1/a2) + (4/a5) + (7/a8) + (10/a11) + ………. to n terms
- double series(double n) with one double argument and returns the sum of the series.
Design a class to overload the method display(…..) as follows:
- void display(int num) — checks and prints whether the number is a perfect square or not.
- void display(String str, char ch) — checks and prints if the word str contains the letter ch or not.
- void display(String str) — checks and prints the number of special characters present in the word str.
Write a suitable main( ) function.
Design a class to overload the method display(…..) as follows:
- void display(String str, char ch) — checks whether the word str contains the letter ch at the beginning as well as at the end or not. If present, print 'Special Word' otherwise print 'No special word'.
- void display(String str1, String str2) — checks and prints whether both the words are equal or not.
- void display(String str, int n) — prints the character present at nth position in the word str.
Write a suitable main() method.
Design a class to overload a method volume( ) as follows:
- 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 - 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 - 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)
- double volume(double r) — with radius (r) as an argument, returns the volume of sphere using the formula: