KnowledgeBoat Logo
|

Computer Applications

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

  1. void polygon(int n, char ch) — with one integer and one character type argument to draw a filled square of side n using the character stored in ch.
  2. void polygon(int x, int y) — with two integer arguments that draws a filled rectangle of length x and breadth y, using the symbol '@'.
  3. void polygon() — with no argument that draws a filled triangle shown below:

Example:

  1. Input value of n=2, ch = 'O'
    Output:
    OO
    OO
  2. Input value of x = 2, y = 5
    Output:
    @@@@@
    @@@@@
  3. Output:
    *
    **
    ***

Java

User Defined Methods

ICSE 2012

81 Likes

Answer

public class KboatPolygon
{
    public void polygon(int n, char ch) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(ch);
            }
            System.out.println();
        }
    }
    
    public void polygon(int x, int y) {
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= y; j++) {
                System.out.print('@');
            }
            System.out.println();
        }
    }
    
    public void polygon() {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print('*');
            }
            System.out.println();
        }
    }
    
    public static void main(String args[]) {
        KboatPolygon obj = new KboatPolygon();
        obj.polygon(2, 'o');
        System.out.println();
        obj.polygon(2, 5);
        System.out.println();
        obj.polygon();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Design a class to overload a function polygon() as follows: (a) void polygon(int n, char ch) — with one integer and one character type argument to draw a filled square of side n using the character stored in ch. (b) void polygon(int x, int y) — with two integer arguments that draws a filled rectangle of length x and breadth y, using the symbol '@'. (c) void polygon() — with no argument that draws a filled triangle shown below: Example: (d) Input value of n=2, ch = 'O' Output: OO OO (e) Input value of x = 2, y = 5 Output: @@@@@ @@@@@ (f) Output: * ** ***

Answered By

29 Likes


Related Questions