Using function overloading generate a square and a rectangle pattern.
Take all required inputs from main functions and invoke the function using class object.
Java
import java.util.*;
public class PolygonGen
{
public void genPolygon(int s)
{
for(int i=1;i<=s;i++)
{
for(int j=1;j<=s;j++)
{
if(i==1||i==s)
{
System.out.print("o ");
}
else if(j==1||j==s)
{
System.out.print("o ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
public void genPolygon(int l,int b)
{
for(int i=1;i<=l;i++)
{
for(int j=1;j<=b;j++)
{
if(i==1||i==l)
{
System.out.print("o ");
}
else if(j==1||j==b)
{
System.out.print("o ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String args[])
{
int s=0,l=0,b=0;
Scanner sc=new Scanner(System.in);
PolygonGen ob=new PolygonGen();
System.out.print("Enter side of sqaure: ");
s=sc.nextInt();
ob.genPolygon(s);
System.out.print("Enter length of rectangle: ");
l=sc.nextInt();
System.out.print("Enter breadth of rectangle: ");
b=sc.nextInt();
ob.genPolygon(l,b);
}
}Java