Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch): with one integer argument and one character
argument that draws a filled square of side n using the character stored in ch.
Input value of n = 2, ch = ‘O’
Output:
OO
OO
(ii) void polygon(int x, int y): with two integer arguments that draws a filled
rectangle of length x and breadth y, using the symbol ‘@’.
Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
(iii) void polygon(): with no arguments that draws a filled triangle shown below.
Example:
Output:
*
**
***
Program
class Poly
{
public static 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 static void polygon(int x, int y)
{
for(int i = 1; i <= y; i++)
{
for(int j = 1; j <= x; j++)
{
System.out.print("@");
}
System.out.println();
}
}
public static void polygon()
{
for(int i=0;i<3;i++)
{
for(int j= 0;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void main(String args[])
{
polygon(5,'o');
polygon(4,7);
polygon();
}
}
{
public static 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 static void polygon(int x, int y)
{
for(int i = 1; i <= y; i++)
{
for(int j = 1; j <= x; j++)
{
System.out.print("@");
}
System.out.println();
}
}
public static void polygon()
{
for(int i=0;i<3;i++)
{
for(int j= 0;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void main(String args[])
{
polygon(5,'o');
polygon(4,7);
polygon();
}
}
No comments:
Post a Comment