Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Program To Produce A Square

  • 01-12-2011 3:27pm
    #1
    Closed Accounts Posts: 95 ✭✭


    I'm trying to write a program that will produce a square/rectangle according to the user's dimensions. It must be hollow, i.e.

    If the user inputs width 5 and height 5 then...

    *****
    * *
    * *
    * *
    *****

    However, it produces

    ****
    ****
    ****
    ****

    What am I doing wrong?
    import javax.swing.JOptionPane; 
    public class box 
    {    
    public static void main(String[] args)
    	{   
    	int shapeWidth = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the width of your shape"))); 
    	int shapeheight = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the height of your shape")));
    		for (int i = 1; i < shapeheight; i++)
    		{
    		for (int j = shapeheight; j == shapeheight; j++)
    		{
    		for (int k = 1; k < shapeWidth; k++) 
    		{
    		for (int l = shapeWidth; l == shapeWidth; l++)
    		{
    		if (i == 1 ) 
    			System.out.print('*');
    		else if (j == shapeheight) 
    			System.out.print('*');
    		else if 
    			(k == 1)    
    			System.out.print('*');
    		else if (l == shapeWidth) 
    		System.out.print('*');
    		else        
    			System.out.print(' '); 
    		}
    		}
    		}
    			System.out.println();  
    		}   
    	}
     }
    


Comments

  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    [PHP]
    int width = 5;
    int height = 5;

    for (int rows = 0; rows < height; ++rows)
    {
    // If its the first line or the last line in a row,
    // print a full line of stars
    if (rows == 0 || rows == (height-1))
    {
    for( int cols = 0; cols < width; ++cols)
    System.out.print("*");

    continue;
    }

    // Any other line...
    System.out.print ("*");

    // Fill the hollow space
    for (int cols = 1; cols < (width-2); ++cols)
    {
    System.out.print (" ");
    }

    System.out.print ("*");
    }
    [/PHP]

    First and last line is easy, you just need to figure out the hollow space.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Anima wrote: »
    [PHP]
    int width = 5;
    int height = 5;

    for (int rows = 0; rows < height; ++rows)
    {
    // If its the first line or the last line in a row,
    // print a full line of stars
    if (rows == 0 || rows == (height-1))
    {
    for( int cols = 0; cols < width; ++cols)
    System.out.print("*");

    continue;
    }

    // Any other line...
    System.out.print ("*");

    // Fill the hollow space
    for (int cols = 1; cols < (width-2); ++cols)
    {
    System.out.print (" ");
    }

    System.out.print ("*");
    }
    [/PHP]

    First and last line is easy, you just need to figure out the hollow space.

    Thanks. But the problem is I MUST use FOUR for loops. Very frustrating as I reckon this is the last program I will ever write (final week of course and we can say programming hasn't been the thing I've done that I've found the most fun lol).


  • Registered Users, Registered Users 2 Posts: 411 ✭✭jk86


    The first issue is that you are starting your for loops with "i=1". This will only draw 4 characters, you need to start the indexes at 0.

    Are you sure you must use four loops? That's doesn't really make much sense.

    It "should" be one loop for the height and then one for the width. What are the other two loops needed for?

    Here's some code using 2 loops (c# but easily converted to Java)
    int width = 5;
                int height = 5;
    
    
                for (int i = 0; i < height; i++)
                {
                    string line = "";
    
                    for (int j = 0; j < width; j++)
                    {
                        if (i == 0 || i == height - 1) //we're on the top or bottom line
                        {
                            line += "*";
                        }
    
                        else if (j == 0 || j == width - 1) //we're on a central line but we're on the first or last column
                        {
                            line += "*";
                        }
    
                        else
                        {
                            line += " ";
                        }
                    }
    
                    Console.WriteLine(line);
                }
    


  • Registered Users, Registered Users 2 Posts: 428 ✭✭[Rasta]


    Tried my best to get 4 loops, but I could only manage 3...
    int width = 7;
            int height = 8;
            
            for(int i=0; i<height; i++)
            {
                //if its first or last line, we want to print all *****           
                if(i==0 || i==height-1)
                {
                    for(int z=0; z<width; z++)
                    {
                        System.out.print("*");                    
                    }
                    System.out.print("\n");
                }
                //else if its a middle line
                else
                {
                    for(int x=0; x<width; x++)
                    {
                        //if its first or last character we want that character to be a *
                        if(x==0 || x==width-1)
                        {
                            System.out.print("*");
                        }
                        //else print a space
                        else
                        {
                            System.out.print(" ");
                        }
                    }
                    System.out.print("\n");
                }            
            }
    


  • Registered Users, Registered Users 2 Posts: 411 ✭✭jk86


    If it needs to be 4 loops, you could add two outer fors that will only loop once.
    for (int a = 0; i < 1; a++)
    {
        for (int b = 0; b < 1; b++)
        {
            //put actual code here
            
            print "this loop is useless"
        }
        
        print "so is this";
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    It has to be 4 loops!? *sigh*

    Well, do these 4 loops so:
    1. Loop by width, printing * each time
    2. Loop by height, print newline on each iteration
    3. Loop by width, if first/last position print *, otherwise print a space.
    4. Last loop is identical to 1.

    Between 3 and 4, make sure to print a newline.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Hi guys, thanks to all!

    I agree, I got it working with two loops. not with four though...

    I'll have a go with what you suggested.


  • Closed Accounts Posts: 95 ✭✭The Crab


    I got this, it uses four loops, it prints the right size but it fills the space in the middle rather than leaving it blank :confused::confused::confused: so hard...
    import javax.swing.JOptionPane; 
    public class box 
    {    
    public static void main(String[] args)
    	{   
    	int shapeWidth = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the width of your shape"))); 
    	int shapeheight = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the height of your shape")));
    		for (int i = 0; i < shapeheight; i++)
    		{
    		for (int j = shapeheight; j == shapeheight; j++)
    		{
    		for (int k = 0; k < shapeWidth; k++) 
    		{
    		for (int l = shapeWidth; l == shapeWidth; l++)
    		{
    		if (i == 0) 
    			System.out.print('*');
    		else if (j == shapeheight) 
    			System.out.print('*');
    		else if 
    			(l != 0 && l != shapeWidth)    
    			System.out.print(' ');
    		}
    		}
    		}
    			System.out.println();  
    		}   
    	}
     }
    


  • Closed Accounts Posts: 95 ✭✭The Crab


    It has to be 4 loops!? *sigh*

    Well, do these 4 loops so:
    1. Loop by width, printing * each time
    2. Loop by height, print newline on each iteration
    3. Loop by width, if first/last position print *, otherwise print a space.
    4. Last loop is identical to 1.

    Between 3 and 4, make sure to print a newline.
    import javax.swing.JOptionPane; 
    public class box1 
    {    
    public static void main(String[] args)
    	{   
    	int shapeWidth = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the width of your shape"))); 
    	int shapeheight = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the height of your shape")));
    		for (int i = 0; i < shapeWidth; i++)
    		{
    		for (int j = shapeheight; j < shapeheight; j++)
    		{
    		for (int k = 0; k < shapeWidth; k++) 
    		{
    		for (int l = shapeWidth; l == shapeWidth; l++)
    		{
    		if (i < shapeWidth) 
    			System.out.print('*');
    		else if (j < shapeheight) 
    			System.out.print('*');
    		else if 
    			(k == 0 || k == shapeWidth)    
    			System.out.print('*');
    		else
    			System.out.print(' ');
    		}
    		}
    		}
    			System.out.println();  
    		}   
    	}
     }
    

    I tried this based on your advice. I know it probably isn't what you told me to do and I've misunderstood but it prints a blank space (it actually looks like the size of the box with no *.

    My thinking is that the answer lies somewhere between the two.

    Is there ever a day when computer programming gets easier or if I find it highly frustrating now is it likely I always would?


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    It has to be 4 loops!? *sigh*

    Well, do these 4 loops so:
    1. Loop by width, printing * each time
    2. Loop by height, print newline on each iteration
    3. Loop by width, if first/last position print *, otherwise print a space.
    4. Last loop is identical to 1.

    Between 3 and 4, make sure to print a newline.

    I should have said that Loop 3 is nested in Loop 2, whereas Loop 1 and Loop 4 (identical) are not nested and do not nest any other loop.
    for (int w = 0; w < width; w++) {
    	System.out.print("*");
    }
    
    System.out.println();
    
    int hollowSpaces = width - 2;
    
    for (int h = 0; h < height; h++) {
    	System.out.print("*");
    	for (int w = 0; w < hollowSpaces; w++) {
    		System.out.print(" ");
    	}
    	System.out.println("*");
    }
    
    for (int w = 0; w < width; w++) {
    	System.out.print("*");
    }
    
    System.out.println();
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    The Crab wrote: »
    Is there ever a day when computer programming gets easier or if I find it highly frustrating now is it likely I always would?

    It does, but the key is to not look at it as frustration, but rather a challenge. You have to a bit of determination and enthusiasm to solve a given problem.


  • Closed Accounts Posts: 95 ✭✭The Crab


    It does, but the key is to not look at it as frustration, but rather a challenge. You have to a bit of determination and enthusiasm to solve a given problem.

    Thanks! Hmm, that's why I think I'm not really cut out for it. I am quite an irritable person unless I'm studying something I enjoy. I'm not thick, and I'm (sorry for sounding arrogant here) far from uneducated but I do tend to get irritated quickly by something I don't find fun.

    It could also be that after a long semester requiring many many many hours of effort I'm fed up and need a break.


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    The Crab wrote: »
    Thanks! Hmm, that's why I think I'm not really cut out for it. I am quite an irritable person unless I'm studying something I enjoy. I'm not thick, and I'm (sorry for sounding arrogant here) far from uneducated but I do tend to get irritated quickly by something I don't find fun.

    It could also be that after a long semester requiring many many many hours of effort I'm fed up and need a break.

    Yeah, I think most people are the same way.

    However, you will probably find it a nicer experience if you try to program things that are fun and/or useful, rather than drawing ASCII rectangles. College is unlikely to provide this for you, however. And while frustrating, stupid little tasks are there to help you learn to do the real stuff.

    Luckily, there is loads of "fun" things you can do with computers, from games, web development, little gimmicks, automation of tasks (so you can get work done quicker and have more time for fun), mobile development, e.t.c. They won't always be fun, but it will have a purpose. An objective.

    That is what is really great about programming (and computers), solving real problems for fun and(/or) profit.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Yeah, I think most people are the same way.

    However, you will probably find it a nicer experience if you try to program things that are fun and/or useful, rather than drawing ASCII rectangles. College is unlikely to provide this for you, however. And while frustrating, stupid little tasks are there to help you learn to do the real stuff.

    Luckily, there is loads of "fun" things you can do with computers, from games, web development, little gimmicks, automation of tasks (so you can get work done quicker and have more time for fun), mobile development, e.t.c. They won't always be fun, but it will have a purpose. An objective.

    That is what is really great about programming (and computers), solving real problems for fun and(/or) profit.

    Ah ok, so I can't expect learning programming in college to be much fun. Its just a means to an end, whereas if I do my own stuff then I could get more out of it. Never thought of it like that before, probably because everything I studied at college before had been enjoyable.


Advertisement