KnowledgeBoat Logo
|

Computer Applications

Design a class to overload a function manip() as follows:

  1. 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.
  2. 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

BlueJ output of Design a class to overload a function manip() as follows: (a) 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. (b) 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.

Answered By

1 Like


Related Questions