Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Beginner: For loop excercise (stuck)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner: For loop excercise (stuck)

    Hi everyone

    First post on these forums and I'm hoping this will be a good spot to hang out.

    I recently started on my new education here in Denmark which focuses on system development and obviously a large part of that is programming. I'm going through some of the initial programming excercises and were doing for loops atm.

    We are using the book "Building Java Programs - A back to basics approach (2nd edition)" and im doing an excercise that goes like this:

    4. Write a method called printSquare that accepts a minimum and maximum integer and prints a square of lines of
    increasing numbers. The first line should start with the minimum, and each line that follows should start with the
    next-higher number. The sequence of numbers on a line wraps back to the minimum after it hits the maximum. For
    example, the call printSquare(3, 7);

    should produce the following output:

    34567
    45673
    56734
    67345
    73456

    If the maximum passed is less than the minimum, the method produces no output.


    Currently my code is looking like this, but I can't seem to get the last part right:

    public class Opg4Kap3 {
     
    	public static void main(String[] args) {
     
    	printSquare(3, 7);
     
    	}
     
    	public static void printSquare(int a, int b) {
     
    		for (int k=a ; k<=b ; k++) {
    			System.out.print(k);
     
    				for (int i=k+1 ; i<=b ; i++) {
    					System.out.print(i);
     
    				}
     
    				for (int r=a ; r<=6 ; r++) {
    					System.out.print(r);
    				}
     
    				System.out.println();	
    		} 
     
     
    	}
     
    }

    If you compile it and run it you can see I'm getting this output, which is close but no cigar:

    345673456
    45673456
    5673456
    673456
    73456

    We don't need to turn this in or anything so I'm not really interested in the final answer, was just hoping someone could give me a clue to the next step so I can finish it though, since it annoys me I can't get the last part right (or if I have to do something completely different to begin with?).

    Also this is not supposed to be done with anything but a method and for loops so no if statement tips or anything.

    Thanks in advance


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Beginner: For loop excercise (stuck)

    To get you thinking on the path, begin with what your problem is. The problem to be solved in code is you want to print some result based on some input.
    Your input is minimum and maximum. Your output is to look something like this:
    [min+0][min+1][min+2]...up to...[min+n=max]
    [min+1][min+2]...up to...[min+n=max][min+0]
    [min+2]...up to...[min+n=max][min+0][min+1]
    ...up to...[min+n=max][min+0][min+1][min+2]
    [min+n=max][min+0][min+1][min+2]...up to...

    So once you see the pattern, you can tell you are printing the same thing max-min times and just shifting the output over by one element.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner: For loop excercise (stuck)

    Quote Originally Posted by jps View Post
    To get you thinking on the path, begin with what your problem is. The problem to be solved in code is you want to print some result based on some input.
    Your input is minimum and maximum. Your output is to look something like this:
    [min+0][min+1][min+2]...up to...[min+n=max]
    [min+1][min+2]...up to...[min+n=max][min+0]
    [min+2]...up to...[min+n=max][min+0][min+1]
    ...up to...[min+n=max][min+0][min+1][min+2]
    [min+n=max][min+0][min+1][min+2]...up to...

    So once you see the pattern, you can tell you are printing the same thing max-min times and just shifting the output over by one element.
    Thanks for the tip.

    I decided to leave it for a while and come back to it, as your answer still didn't make me able to see the answer clearly enough.
    Now many hours later I sat down, looked at it and suddenly was able to solve it in less than 5 min. Go figure.

    Do you think this would be a good solution? I tried changing the numbers like this and it works as far as I can tell.

    public class Opg4Kap3 {
     
    	public static void main(String[] args) {
     
    	printSquare(3, 7);
     
    	}
     
    	public static void printSquare(int a, int b) {
     
    		for (int k=a ; k<=b ; k++) {
     
    			System.out.print(k);
     
    			for (int i=k+1 ; i<=b ; i++) {
    				System.out.print(i);
    			}
     
    			for (int q=a; q<k; q++) {
    					System.out.print(q);
    			}
     
    			System.out.println();	
    		} 
     
    	}
     
    }

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Beginner: For loop excercise (stuck)

    Does the method print the correct output for any given input? If so you have solved the problem in code in at least one of the possible ways.
    Did you try different input values?

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner: For loop excercise (stuck)

    As far as I can tell so far, it seems to make perfects squares of numbers and print them in the correct sequence they ask for in the excercise. So I think I solved it, you could try compiling it yourself if you have time and tell me if you can make it fail with a certain pair of numbers.

    But I'm pretty sure it's working as intended like this?

Similar Threads

  1. Beginner for loop
    By gerre in forum Loops & Control Statements
    Replies: 11
    Last Post: August 2nd, 2012, 03:43 AM
  2. [SOLVED] Beginner, stuck on implementing while loop, compiles fine but still won't run
    By GregC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2012, 05:36 PM
  3. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 09:20 AM
  4. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM
  5. Stuck -- Loop Pattern
    By CodeNewb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 11th, 2010, 03:02 AM