Pages

Friday, December 30, 2022

ICSE Java Programming Function Overloading 2023 Q4 SPECIMEN PAPER

ICSE Java Programming  2023 Q3 SPECIMEN PAPER Solved.


Question 4

Define a class to overload the method print as follows:

void print ()-to print the format

1

2 3

4 5 6

7 8 9 10

boolean print (int n) -

to check whether the number is a Dudeney number,a number is dudeney if the cube of the sum of the digits is equal to the number itself.

Eg: 512(5+1+2)3 = (8)3 = 512

void print (int a, char ch) -

if ch = s or S print the square of the number else if

ch=c or C print the cube of the number.

Function Overloading in java example.




import java.util.Scanner;
public class ICSEJava2023 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        FunctionOverloading obj = new FunctionOverloading();
        int choice = 0;
        int n;
        char ch;
        System.out.println("Press 1 to print the format:");
        System.out.println("Press 2 to check for Dudeney number:");

        System.out.println("Press 3 to print the square or cube:");
        choice = in.nextInt();
        switch (choice) {
            case 1:
                obj.print();
                break;
            case 2:
                System.out.println("Enter the number:");
                n = in.nextInt();
                if (obj.print(n) == true)
                    System.out.println(" It's a Dudeney numer");
                else
                    System.out.println(" It's not a Dudeney numer");
                break;
            case 3:
                System.out.println("Enter the number:");
                n = in.nextInt();
                System.out.println("Enter the character (c/C or s/S):");
                ch = in.next().charAt(0);
                System.out.println("Enter the height of the cuboid:");
                obj.print(n, ch);
                break;
            default:
                System.out.println("Wrong Choice!");
                break;
        }
        in.close();
    }
}

class FunctionOverloading {
    public void print() {
        int counter = 1;
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print((counter++) + " ");
            }
            System.out.println();
        }
    }

    public boolean print(int n) {
        int sum = 0;
        int temp;
        temp = n;
        while (temp > 0) {
            sum += temp % 10;
            temp /= 10;
        }
        if (sum * sum * sum == n)
            return (true);
        else
            return (false);
    }

    public void print(int a, char ch) {
        if (ch == 's' || ch == 'S')
            System.out.println(a * a);
        else if (ch == 'c' || ch == 'C')
            System.out.println(a * a * a);
        else
            System.out.println("Invalid input!");
    }
}

No comments:

Post a Comment