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 22 of 22

Thread: Need to use mod operator to complete printSquare program

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to use mod operator to complete printSquare program

    I need to make a program that will accept max and min integers and print a square of lines of increasing numbers.

    printSquare(3,6) needs to print:

    3456
    4563
    5634
    6345

    Below is what I have so far and its current output:

    public static void printSquare(int i, int j) {
    int x = 3;
    int y = 6;

    for( i = x; i <= y; i++) {
    for( j = i; j <= y; j++) {

    System.out.print(j);

    }

    System.out.println();

    }

    Ouput:

    3456
    456
    56
    6

    How can I use the mod function to complete this program?


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Need to use mod operator to complete printSquare program

    I'm not seeing how the mod function will come into play at all in this application.

    Questions:
    1. Am i right thinking you are asking about the mod (aka modulus) function that returns the remainder of a division? 135 % 20 = 15
    2. Do i understand your problem correctly. You give a min and max number then print every combination of those numbers using ascending order.
    printSquare(1,3)
    123
    231
    312
    printSquare(4,9)
    456789
    567894
    678945
    789456
    894567
    945678

    --- Update ---

    Assuming this is a homework assignment, is your teacher requiring you to use the mod function?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Yes that is the mod function I am asking about, and yes you are understanding the program correctly. My professor suggested that I use the mod operator, but does not require that I do so.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Need to use mod operator to complete printSquare program

    My initial suggestion is to find a better mechanic for creating your output then displaying all of your output at the end of the program. Teachers will make things more complicated until this is pretty much required. But that's just something to look at in the future.

    I really dont see how mod would fit in here. The way you're going about it is prolly the same way I would. You're very close to being done. Stating what your program does in words can be very beneficial to working this problem out. Right now you're looping through each number and then looping from that number to the END printing each number. You can see that in your output and it is working...now what's next? You have all the pieces and your code works, you just need to hash out the second half of the algorithm. Mull it over for a few minutes and get back to me.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Is it a problem that I am using the ++ operator in my for loop? The other thought I had was that I could initialize an array to print 4 numbers on each line.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Ok back up a second. Nothing in your current code is incorrect. You are just missing the piece of the story After number to max, print from min to the current number". You've already done "Print from current number to max". Just like you can tell the story in English, your code should flow just like the story. Find the missing piece and write it.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    How are you going to have the digits wrap around to the starting digit when the end digit is shown on a line?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Need to use mod operator to complete printSquare program

    I'm not going to spoon feed you norm

    The program isnt doing a println until the end of the main for loop. Just add the code to print the numbers from "min" to "i" after the code that prints from "i" to "max".
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    @Chris.Brown.SPE We posted at the same time. My post was to the OP, not you.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Is this code on the right track to completing the program:

    for( i = y; i >= x; i++);
    for( j = i; j >= y; j++);

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    One problem with the for loops is the ; which ends the statement making for two empty loops.

    Otherwise what are the loops supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    They are supposed to wrap the range of numbers around so that after 6 it goes back to 3. I want to print from min to the current number, and that's what I was trying to do because I have already printed the other half--from the current number to max.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Have you compiled and executed the code to see what it does?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    I tried to run it, but I don't think I am printing it correctly because it didn't result in any output.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    What you posted did NOT have a print statement in it. You need to call a print() method to print anything out.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    I tried to run it, but I don't think I am printing it correctly because it didn't result in any output.

    --- Update ---

    I really appreciate the questions, but I have not executed the program correctly yet and it is due soon. Do you mind helping me figure it out?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Hard to say anything without seeing any code.

    A suggestion: The variable names should reflect what values they hold and how they are used. i,j,x,y have no meanings. min, max row and column carry some meaning and make the code easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    If you look at the first post in this thread, I have written the code that I have so far, and the output that is resulting from it. It is pretty close. Also, I assigned values to x and y, x = 3 (min) and y = 6 (max)

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    What code do you have now? What is the current problem you are having?


    values to x and y, x = 3 (min) and y = 6 (max)
    One change I recommend is to change the variable names to something related to the data that they hold. X and y have no meaning when reading the code.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to use mod operator to complete printSquare program

    The problem is still the same--I can't figure out how to make the numbers wrap around so that 4 numbers are printed out on 4 lines in every combination in ascending order. Right now my output is:

    3456
    456
    56
    6

    and it needs to be:

    3456
    4563
    5634
    6345

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to use mod operator to complete printSquare program

    Look at what is printed on each row.
    The first digit is the row+min
    followed by the digits up through the max digit
    following the max digit is the min digit and print the digits for the rest of that row

    The above requires a counter to know how many digits to print.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Need to use mod operator to complete printSquare program

    mccoya, has this been resolved? Sorry for being AFK, work got busy.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. A quick help to complete my program :D
    By azizmaiden in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 12th, 2013, 08:01 PM
  2. A quick help to complete my program :D
    By azizmaiden in forum AWT / Java Swing
    Replies: 2
    Last Post: March 12th, 2013, 12:04 PM
  3. mod 95 vigninere cipher
    By fortune2k in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 5th, 2010, 09:43 PM

Tags for this Thread