Q2: Design a class Pattern to overload a function polygon () as follows:
i. Void polygon (int n, char ch) with one integer argument and one character type
argument that draws a filled square of side n using the character stored in ch.
ii.void polygon(int x, int y) -with two integer argument that draws a filled rectangle of
length x and breadth y, using the symbol '@'.
iii.void polygon ()- with no argument that draws a filled triangle shown below
Answers
JAVA CODE
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();
}
}
- Void polygon (int n, char ch) that draws a full square with a side of n using the character stored in ch has one integer parameter and one character type argument..
- void polygon(int x, int y) creates a full rectangle with the symbol "@" using two integer arguments of length x and breadth y.
- Drawing a full triangle using an empty polygon ()- without any arguments is demonstrated below.
#SPJ2