Reputation: 9
So I have this code:
public class Triangle
{
static int num;
public static void main (String[] args)
{
num = Integer.parseInt(args[0]);
Q1();
Q2();
Q3();
Q4();
}
public static void Q1()
{
for(int i=0;i<=num;i++)
{
for(int j=0;j<i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void Q2()
{
for(int i=0;i<=num;i++)
{
for(int j=num;j>0;j--)
{
if (i<j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
public static void Q3()
{
for (int i=0;i<num;i++)
{
for (int j=0;j<i;j++)
{
System.out.print(" ");
}
for (int j=i;j<num;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void Q4()
{
for(int i=0;i<=num;i++)
{
for(int j=num;j>i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
And it outputs something like this:
*
**
***
****
*****
*
**
***
****
*****
*****
****
***
**
*
*****
****
***
**
*
What we need to do is we need to arrange each method to their corresponding quadrant, for example when only Q1 is called it needs to display the asterisks within the limits of Quadrant 1 in a cartesian plane.
So if Q1,Q2,Q3 and Q4 are called, it should look something like this:
**
****
******
********
**********
**********
********
******
****
**
How would I go about in arranging each method to their respective quadrants?
Upvotes: 0
Views: 252
Reputation: 5048
Here there is an example.
Note: The most important thing is the buffer. You must be able to print all quadrants or whatever combinations of them with a buffer.
Note2: You must to respect the Java conventions!
public static void main(String[] args) {
int num = Math.max(1,Integer.parseInt(args[0]));
char[][] buffer = new char[num * 2][num * 2]; // The Buffer is needed
writeQ1(buffer, num);
writeQ2(buffer, num);
writeQ3(buffer, num);
writeQ4(buffer, num);
printBuffer(buffer);
}
// With a buffer, this method is needed
public static void printBuffer(char[][] buffer) {
for (char[] cs : buffer) {
for (char c : cs) {
if (c == 0) {
System.out.print(' ');
} else {
System.out.print(c);
}
}
System.out.println();
}
}
// The next methods can be refactored!
public static void writeQ1(char[][] buffer, int num) {
for (int i = 0; i <= num; i++) {
for (int j = 0; j < i; j++) {
buffer[i][j + num] = '*';
}
}
}
public static void writeQ2(char[][] buffer, int num) {
for (int i = 0; i <= num; i++) {
for (int j = 0; j < i; j++) {
buffer[i][j + (num - i)] = '*';
}
}
}
public static void writeQ3(char[][] buffer, int num) {
for (int i = 0; i <= num; i++) {
for (int j = 0; j < i; j++) {
buffer[2*num - i][j + (num - i)] = '*';
}
}
}
public static void writeQ4(char[][] buffer, int num) {
for (int i = 0; i <= num; i++) {
for (int j = 0; j < i; j++) {
buffer[2*num - i][j + num] = '*';
}
}
}
Upvotes: 1