# Box of asterisks

• 11-01-2011, 11:02 PM
golf_pro90
Box of asterisks
Hello

First year Computer Science student here. I'm completely stuck as how to how get an open box of asterisks as my output.
For example entering a length of 4 and a width of 4 to produce
****
* *
* *
****

any help would be appreciated!! so far I have:

import java.util.Scanner;

public class Stars {

public static void main(String[] args) {

Scanner input = new Scanner( System.in );

int length; // number of stars on a side
int width; // the current column of the square being printed
int row = 0;

// prompt user to enter the length and width

System.out.print("Enter the length: ");
length = input.nextInt();

System.out.print("Enter the width: ");
width = input.nextInt();

System.out.print("*");
System.out.println();

} // end of main
} // end of Stars
• 11-01-2011, 11:19 PM
Junky
Re: Box of asterisks
First and last row is simple: print N stars.

For every other row: print star, print M spaces, print star, newline.

Try drawing what the output would look like for various values: 4,5,6,7 etc on a piece of (graph) paper and you should be able to see a pattern. Once you can determine what that pattern is then writing the code is not that hard.
• 11-01-2011, 11:30 PM
golf_pro90
Re: Box of asterisks
I see where your coming from. I really just don't know how to go about writing the code for that specific part.
• 11-01-2011, 11:39 PM
Junky
Re: Box of asterisks
Have you worked out the pattern/algorithm? If so post it here so we can see. As I said working out the algorithm is the hard part, wiriting the code is the easy part. Why is it easy? Because you know how to declare variables, how to change the value of variables, write loops, write if statements, use print statements. The program is just a bunch of all those things that you know how to do. You just have to get them in the correct order and that is where the algorithm comes in.
• 11-01-2011, 11:49 PM
skaterboy987
Re: Box of asterisks
Ignore this
• 11-01-2011, 11:52 PM
Junky
Re: Box of asterisks
Quote:

Originally Posted by skaterboy987
I did it.

No you didn't. The requirement is for the box to be empty, spaces inside. Even if you did solve please DO NOT post the code. We don't care how clever you are. All you do is enable someone to cheat and hinder their learning if they just copy your code. Ooops! It was only pseudocode which is ok.
• 11-01-2011, 11:53 PM
skaterboy987
Re: Box of asterisks
Sorry, I deleted it.