55502f40dc8b7c769880b10874abc9d0

Simply prints a square with the side of user given argument.

import java.util.Scanner;

public class AsciiSquare
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();

for(int i = 0; i<side; i++)
{
for(int k = 0; k<side; k++)
{
if(i == 0 || k = 0 || i == (side-1) || k == (side-1))
System.out.print("*");
}
System.out.println("");
}

}

}

Refactorings

No refactoring yet !

Fac9eaaa078726c634ad28a3837a896f

Xerothermic

May 18, 2008, May 18, 2008 18:13, permalink

No rating. Login to rate!
import java.util.Scanner;

public class AsciiSquare
{
        public static void main(String[] args)
        {
                Scanner sc = new Scanner(System.in);
                int side = sc.nextInt();

                for(int i = 0; i<side; i++)
                {
                        for(int k = 0; k<side; k++)
                        {
                                if(i == 0 || k == 0 || i == (side-1) || k == (side-1))
                                        System.out.print("*");
                                else
                                        System.out.print(" ");
                        }
                        System.out.println("");
                }

        }

}
5170ca260dbd2cdfd5a887a4dba7636f

Jeremy Weiskotten

May 21, 2008, May 21, 2008 19:10, permalink

1 rating. Login to rate!

Here's a different approach. Should be more efficient, if that matters.

import java.util.Scanner;

public class AsciiSquare {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int side = sc.nextInt();

		char[] edge = new char[side];
		char[] innerSpace = new char[side - 2];

		java.util.Arrays.fill(edge, '*');
		java.util.Arrays.fill(innerSpace, ' ');

		String row = "*" + new String(innerSpace) + "*";
		
		System.out.println(edge);
		for (int i = 0; i < side - 2; i++) {
			System.out.println(row);
		}		
		System.out.println(edge);
	}
}
Ceb4aa3185c3d298282a96f9fc621fb4

Daniel Becker

May 24, 2008, May 24, 2008 14:52, permalink

No rating. Login to rate!

I didn't run any of these through the profiler so I can't be 100% certain these changes will be more efficient.

Starting with Jeremy's code above I made the following changes:
1. Arrays.fill is actually just a for loop (with a range check) in the standard library, I merged them in to one loop with out any range checks.
2. Rather than printing out as a char[] I'm printing as a String.
3. I removed the subtraction operation from the printing for loop.

I strongly suspect that the System.out calls are so slow that everything else is immaterial any way :-) It would probably be faster to write to a string buffer and output it in one go. The print methods flush and have synchronised blocks.

Daniel

import java.util.Scanner;

public class AsciiSquare {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int side = sc.nextInt();

		char[] edge = new char[side];
		char[] row = new char[side];
		for (int i=0; i<side; i++){
		    edge[i] = '*';	
		    row[i] = ' ';
		}

                //Edges
		row[0]='*';
		row[edge.length-1]='*';
		
                //Create the strings once
		String edgeString = new String(edge);
		String rowString = new String(row);
		//Output
		System.out.println(edgeString);
		for (int i = 2; i < side; i++) {
			System.out.println(rowString);
		}		
		System.out.println(edgeString);
	}
}
C91e66fa9cff23cf922e1268ff538040

Maros

May 31, 2008, May 31, 2008 21:09, permalink

No rating. Login to rate!

Quite readable 15 line way of doing it.

import java.util.Scanner;

public class AsciiSquare {
	public static void main(String[] args) {
		int side = new Scanner(System.in).nextInt();
		
		for (int y = 1; y <= side; ++y, System.out.println())
			for (int x = 1; x <= side; ++x)
				System.out.print(isBorder(y, x, side) ? '*' : ' ');
	}

	private static boolean isBorder(int x, int y, int side) {
		return x == 1 || y == 1 || x == side || y == side;
	}
}
31b0767c6dbcc91b37e47fa481342c18

clonecd185

May 9, 2011, May 09, 2011 00:30, permalink

No rating. Login to rate!

Measure for measure.

Your refactoring





Format Copy from initial code

or Cancel