KnowledgeBoat Logo
|

Computer Applications

Define a class to overload the method display() with the following variations:

1. void display() — To display the following pattern:

    0 1 0 1 0
    0 1 0 1 0
    0 1 0 1 0

2. int display(int x[]) — To return the smallest element of the array.

Example:

{6, 8, 4, 13, 2, 21}

Smallest element = 2

3. void display(double a, char b) — To calculate and display the area of circle if b is 'C' otherwise calculate and display the area of square if b is 'S'. For any other value of b print "Invalid operation".

Area of circle = πr2

Area of square = side2

public class KboatDisplay
{
    void display() {
        _______(1)_________
            _______(2)_________
                _______(3)_________
                    System.out.print("0  ");
                } else {
                    System.out.print("1  ");
                }
            }
            System.out.println();
        }
    }
    
    _______(4)_________ {
        int min = x[0];
        _______(5)_________
            _______(6)_________
                min = x[i];
            }
        }
        return min;
    }
    
    _______(7)_________ {
        _______(8)_________ {
            double area = Math.PI * a * a;
            System.out.println("Area of Circle: " + area);
        } _______(9)_________ {
            double area = a * a;
            System.out.println("Area of Square: " + area);
        } else {
            _______(10)_________
        }
    }
}

User Defined Methods

1 Like

Answer

  1. for (int i = 0; i < 3; i++) {
  2. for (int j = 0; j < 5; j++) {
  3. if (j % 2 == 0) {
  4. int display(int x[])
  5. for (int i = 1; i < x.length; i++) {
  6. if (x[i] < min) {
  7. void display(double a, char b)
  8. if (b == 'C' || b == 'c')
  9. else if (b == 'S' || b == 's')
  10. System.out.println("Invalid operation");

Explanation

public class KboatDisplay
{
    void display() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 5; j++) {
                if (j % 2 == 0) {
                    System.out.print("0  ");
                } else {
                    System.out.print("1  ");
                }
            }
            System.out.println();
        }
    }
    
    int display(int x[]) {
        int min = x[0];
        for (int i = 1; i < x.length; i++) {
            if (x[i] < min) {
                min = x[i];
            }
        }
        return min;
    }
    
    void display(double a, char b) {
        if (b == 'C' || b == 'c') {
            double area = Math.PI * a * a;
            System.out.println("Area of Circle: " + area);
        } else if (b == 'S' || b == 's') {
            double area = a * a;
            System.out.println("Area of Square: " + area);
        } else {
            System.out.println("Invalid operation");
        }
    }
}

Variable Description Table

Method: void display()

Variable NameData TypePurpose
iintLoop counter variable for controlling the number of rows in the pattern (3 rows).
jintLoop counter variable for controlling the number of columns in the pattern (5 columns).

Method: int display(int x[])

Variable NameData TypePurpose
xint[]Array of integers from which the smallest element is to be found.
minintStores the smallest value found in the array x. Initialized with the first element of the array.
iintLoop counter variable for iterating through the array elements starting from the second element.

Method: void display(double a, char b)

Variable NameData TypePurpose
adoubleRepresents the radius of the circle if b is 'C' or 'c', or the side length of the square if b is 'S' or 's'.
bcharDetermines the operation to perform: 'C' or 'c' for calculating the area of a circle, 'S' or 's' for calculating the area of a square, otherwise triggers an invalid operation message.
areadoubleStores the calculated area of the circle or square based on the value of b.

Program Explanation

Let's take a closer look at this Java program to understand how it works:

void display() Method:

  • This variation of the display() method is parameterless and is responsible for printing a specific pattern.
  • The method uses nested for loops to achieve this pattern output. The outer loop runs three times (i from 0 to 2), which corresponds to the three lines of output.
  • The inner loop runs five times (j from 0 to 4) on each iteration of the outer loop, printing either "0" or "1" followed by two spaces, depending on whether the current index j is even or odd.
  • If the index j is even, "0 " is printed; if odd, "1 " is printed. This creates the alternating pattern "0 1 0 1 0" on each line.
  • System.out.println() is used at the end of each iteration of the outer loop to move to the next line after completing a pattern line.

int display(int x[]) Method:

  • This method takes an integer array as its parameter and returns the smallest element in the array.
  • Initially, it assumes that the first element x[0] is the smallest (min = x[0]).
  • It iterates over the array elements starting from the second element (i from 1 to x.length-1).
  • For each element, it compares the current element x[i] with the current minimum min.
  • If an element x[i] is smaller than the current minimum, min is updated to this element.
  • At the end of the loop, min contains the smallest element of the array, which is then returned.

void display(double a, char b) Method:

  • This method accepts two parameters: a double a, and a character b.
  • It performs different calculations based on the value of the character b.
  • If b is 'C' or 'c', the method calculates the area of a circle using the formula πa² (Math.PI * a * a) and prints it.
  • If b is 'S' or 's', it calculates the area of a square using the formula a², and then prints the result.
  • For any other character value of b, it prints "Invalid operation", indicating that the input character is not one of the expected options ('C' or 'S').

Output

Define a class to overload the method display() with the following variations, void display(). Practice Test ICSE Computer Applications Class 10.
Define a class to overload the method display() with the following variations, void display(). Practice Test ICSE Computer Applications Class 10.
Define a class to overload the method display() with the following variations, void display(). Practice Test ICSE Computer Applications Class 10.

Answered By

2 Likes


Related Questions