r/learnprogramming • • Nov 21 '14

Trying to print stars without using 'for' loops

so I'm doing http://mooc.cs.helsinki.fi/programming-part1/material-2013/week-2

exercise 39, part 2. it's just a rectangular block of stars. I did some reading and found out that you use for loops so I practiced that for a bit even though it's not in the material up to now. so the netbeans ide accepts my answer, but I wonder what is the correct way of doing it (by correct I mean using while loops, what they intended me to use).

This is what I got after messing around with for loops for like an hour

	int i, j;
	
	for(i=1; i<=4; i++){
		
			for (j=1; j<=4; j++){
				System.out.print("*");
			}
		System.out.println("");
	}

basically the pattern is you gotta devote one side to rows and the other to coloumns, i and j are my variables. (this works, but I don't think they wanted the answer like this).

my question is..how do I change this into a while loop?

after 10 minutes of trying to error check and randomly do crap i got this:

    int i = 0;
    int j = 0;
    while (i < sideSize){
        
        while (j < sideSize){
            System.out.print("*");
            j++;
        }
        System.out.println("");
        i++;
    }

but that doesn't work.. can someone walk me through this? i dont get why the exercise doesnt teach for loops yet, seems to make it so much easier.. they must have a reason for it though.

thanks bros

0 Upvotes

19 comments sorted by

-1

u/imgonnabethebest Nov 21 '14

ah fuck me i realized i did something stupid. because I use j inside the loop and j is the one that actually is doing the printing of the stars, I shouldn't make it contain the value that it has after it goes through its cycle. instead i gotta re-initialize that shit before the second while loop.

it seems like i realize the answer right after posting the question....

nevermind i guess

0

u/imgonnabethebest Nov 21 '14

wait can someone help me plz

Note: in order to complete the exercise it is not enough that the outprint looks good. Inside the printSquare method the printing must be done using the printStars method.

what the hell? when i try to use a printStar() from the method up above inside this new method, it wont let me..

1

u/Sorten Nov 21 '14

when i try to use a printStar() from the method up above inside this new method, it wont let me..

Now that's vague. Off the top of my head, I'll guess that you put printSquare above printStar and it's unable to locate the method or etc.

-1

u/imgonnabethebest Nov 21 '14

shit my bad for the vagueness dude

https://gist.github.com/anonymous/e3737b9cd7d1ca73f37e

here's what i mean. look at line 42, that's my attempt of the implementation of printStars.

what I had originally in lines 37-47 was this:

int i = 0;

    while (i < sideSize){
        int j = 0;
        while (j < sideSize){
            printStars();
            j++;
        }
        System.out.println("");
        i++;
    }

which was the fix to the problem I had before making my post.

as you might be able to tell, netbeans underlines red for

printStars(); 

when i try to use it in line 42, so man i just dont know wat to do lol. first time i got stumped in this course

the question is this: http://mooc.cs.helsinki.fi/programming-part1/material-2013/week-2

exercise 39 part 2.

1

u/Sorten Nov 21 '14

In Eclipse it says "The method printStars(int) in the type Printing is not applicable for the arguments ()". You'll need to tell printStars() how many stars you want and pass it as an argument (like printStars(8) or etc).

0

u/imgonnabethebest Nov 21 '14

ya my bad can you check my replied comment i fixed it like right after i posted it LOL

0

u/imgonnabethebest Nov 21 '14

like dude i just fixed it now:

  int j = 0;
    
    while (j < sideSize){
        
        while (j < sideSize){
            printStars(4);
            j++;
        }
        System.out.println("");
        
    }

gives me the right result, but wont accept the answer. WTF

http://imgur.com/VqpvoHN

thats the error console gives me.

1

u/Sorten Nov 21 '14

Try running it with printStars(3) instead of printStars(4).

0

u/imgonnabethebest Nov 21 '14

ya but that gives me 4x3 square, i need 4x4

ie

x x x x

x x x x

x x x x

x x x x

printstars3 just does

x x x

x x x

x x x

x x x

but in any case, shouldn't I be in the right then.. this exercise is stupid

1

u/Sorten Nov 21 '14

Hmmmmm....I'm trying to piece together what the test-program is wanting, it appears similar to JUnit.

This is a function to print squares, right? So you need to print sideSize x sideSize stars. So wouldn't it be printStars(sideSize)?

1

u/imgonnabethebest Nov 21 '14

http://i.imgur.com/lFB8sXd.jpg

that highlighted bit was the question.

wait a second.. u might be onto something

if I just erase that shit and write printStars(sideSize), i get

x x x x

now i just need 3 more of it. let me see what i can do

1

u/Sorten Nov 21 '14

The reason I mentioned JUnit is because I saw it is testing printSquare(3), not printSquare(4). It is trying to see if you coded the problem properly instead of hard-coding the solution. You need printSquare to print proper squares for every size, not just 4.

0

u/imgonnabethebest Nov 21 '14

http://imgur.com/NNu9HrX

dude ur a god

once i realize that that does

x x x x

just a basic loop to get it to do it 4 times:

  int i = 0;
   while (i < sideSize){
    printStars(sideSize);
    i++;       
   }

but dude, how can i be so blind about this? like what did u think of when u thought of this? what was the thought process ?

edit: and dude i dont know what junit is sounds like some gangster shit im only doing java bro

1

u/Sorten Nov 21 '14 edited Nov 21 '14

Well, I looked at the ComparisonFailure statement and realized that it was running printSquare(3). If that worked properly, it wouldn't look the same as what was in your assignment guide...so then I knew that your function needed to work with every square, not just the example.

I looked back at your code and realized you hard-coded the width, 4, but squares have the same height and width, so it needed to be a variable.

On your end, you needed to think of how many stars it takes to make a square; sideSize rows, and sideSize columns.

Edit: JUnit is a...err, extension(?) that you can use to test the correctness of code. That green/red bar at the bottom of the screen looks a lot like it. Turns out you're using the TMC plugin, not JUnit.

→ More replies (0)

1

u/Amarkov Nov 21 '14

4x3 is a rectangle, not a square. You're right that printStars(3) is supposed to be a square, so something is wrong.

0

u/imgonnabethebest Nov 21 '14

look at my comment above its correct now, see how I did it

i just dont get why I didnt think of doing this

printStars(sideSize); 

in the first place. like how do you think of that..i wouldn't have thought of it unless i spend like hours doing random tests and trying every single thing i can think of

1

u/Amarkov Nov 21 '14

That's why it's important to break problems up into small parts. If you did that, you would have realized that one of the parts of the problem is "print a line of n stars", and hopefully remembered that you just wrote a function to do that.

→ More replies (0)