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)_________
}
}
}
Answer
for (int i = 0; i < 3; i++) {for (int j = 0; j < 5; j++) {if (j % 2 == 0) {int display(int x[])for (int i = 1; i < x.length; i++) {if (x[i] < min) {void display(double a, char b)if (b == 'C' || b == 'c')else if (b == 'S' || b == 's')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 Name | Data Type | Purpose |
|---|---|---|
i | int | Loop counter variable for controlling the number of rows in the pattern (3 rows). |
j | int | Loop counter variable for controlling the number of columns in the pattern (5 columns). |
Method: int display(int x[])
| Variable Name | Data Type | Purpose |
|---|---|---|
x | int[] | Array of integers from which the smallest element is to be found. |
min | int | Stores the smallest value found in the array x. Initialized with the first element of the array. |
i | int | Loop counter variable for iterating through the array elements starting from the second element. |
Method: void display(double a, char b)
| Variable Name | Data Type | Purpose |
|---|---|---|
a | double | Represents the radius of the circle if b is 'C' or 'c', or the side length of the square if b is 'S' or 's'. |
b | char | Determines 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. |
area | double | Stores 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
forloops to achieve this pattern output. The outer loop runs three times (ifrom 0 to 2), which corresponds to the three lines of output. - The inner loop runs five times (
jfrom 0 to 4) on each iteration of the outer loop, printing either "0" or "1" followed by two spaces, depending on whether the current indexjis even or odd. - If the index
jis 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 (
ifrom 1 tox.length-1). - For each element, it compares the current element
x[i]with the current minimummin. - If an element
x[i]is smaller than the current minimum,minis updated to this element. - At the end of the loop,
mincontains 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 characterb. - It performs different calculations based on the value of the character
b. - If
bis 'C' or 'c', the method calculates the area of a circle using the formula πa² (Math.PI * a * a) and prints it. - If
bis '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



Related Questions
Assertion (A): An argument is a value that is passed to a method when it is called.
Reason (R): Variables which are declared in a method prototype to receive values are called actual parameters
- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
- Assertion (A) is true and Reason (R) is false
- Assertion (A) is false and Reason (R) is true
Method prototype for the method compute which accepts two integer arguments and returns true/false.
- void compute (int a, int b)
- boolean compute (int a, int b)
- Boolean compute (int a,b)
- int compute (int a, int b)
Assertion (A): Static method can access static and instance variables.
Reason (R): Static variable can be accessed only by static method.
- Assertion and Reason both are correct.
- Assertion is true and Reason is false.
- Assertion is false and Reason is true.
- Assertion and Reason both are false.