ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2012 ICSE Computer Science Paper



Share with a Friend

Solved 2012 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Function Overloading - Polygon Program - ICSE 2012 Computer Science

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.
(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 ‘@’.
(iii) void polygon(): with no arguments that draws a filled triangle shown below.

Example:
(i) Input value of n = 2, ch = ‘O’
Output:
OO
OO
(ii) Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

class Overload{ 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 = 1; i <= 3; i++){ for(int j = 1; j <= i; j++) System.out.print("*"); System.out.println(); } } }

Output