KnowledgeBoat Logo
|

Computer Applications

Define a class to overload the method transform as follows:

int transform(int n) – to return the sum of the digits of the given number

Example: n = 458
output : 17

void transform(String s) – to convert the given String to upper case and print

Example: if S = “Blue”
Output : BLUE

void transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops.

Example: if ch = ‘@’
Output :

@@@  
@@@
@@@

Java

Java Method Overloading

10 Likes

Answer

import java.util.Scanner;

public class OverloadTransform {

    int transform(int n) {
        int sum = 0;
        while (n > 0) {
            int d = n % 10;
            sum += d;
            n /= 10;
        }
        return sum;
    }

    void transform(String s) {
        String str = s.toUpperCase();
        System.out.println(str);
    }

    void transform(char ch) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(ch);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        OverloadTransform obj = new OverloadTransform();

        System.out.print("Enter an integer: ");
        int num = sc.nextInt();
        System.out.println("Sum of digits: " + obj.transform(num));

        sc.nextLine();
        System.out.print("Enter a string: ");
        String str = sc.nextLine();
        obj.transform(str);

        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);
        obj.transform(ch);
    }
}

Output

BlueJ output of Define a class to overload the method transform as follows: int transform(int n) – to return the sum of the digits of the given number Example: n = 458 output : 17 void transform(String s) – to convert the given String to upper case and print Example: if S = “Blue” Output : BLUE void transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops. Example: if ch = ‘@’ Output : @@@ @@@ @@@

Answered By

3 Likes


Related Questions