Results 1 to 9 of 9
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default paintComponent(page), Reverse digit, and Astericks triangle outputter question

    Hi, I have three questions from my book "Java Software Solutions". Great book. Any help greatly appreciated! Thank you. Derek

    page 184

    I don't know what "page" does in the code snippet

    Java Code:
    public void paintComponent(Graphics page);
    
    super.paintComponent (page); //where super is a reference to Jpanel I think.
    __________________________________________________ _______

    page 253 //code to reverse a number input by the user

    here is the snippet

    Java Code:
    int reverse = 0;
    lastDigit = number % 10; Supposedly this outputs the last digit of the number
    reverse = (reverse * 10) + lastDigit;// if reverse is 0, doesn't this output zero?
    number = number / 10;//I don't understand this either
    The book explains it briefly but I still don't understand.
    __________________________________________________ ______

    This is from page 258.

    It prints out a triangle of asterisks

    Java Code:
    public class Stars
    {
                  public static void main (String[] args)
                       final int MAX_ROWS = 10;
                       for (int row = 1; row <= MAX_ROWS; row++)//this runs 10 times
                        {
                              for (int star = 1; star <= row; star++)// shouldn't this only output one star per row? How does it increase per row?
                              {
                                   System.out.print ("*");
    
                                    System.out.println();
                               }
                         }     
    }
    }
    Last edited by silverglade; 05-19-2011 at 07:34 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    For your first question, page is a reference to an instance of Graphics. It's being passed to the parent class's paintComponent() method as a parameter.

    I'm not really sure what your second question is asking. Work through it with a piece of paper and a pencil using different numbers. See what each step does.

    For your third question, step through it with a debugger, or at least add some print statements, to figure out how the value of row is increasing each time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default

    Have you tried putting the code in a program and executing it?
    When I tried,
    2)the output given number=12 was 1
    3)The *s printed in a vertical column in column 1.

    The posted code doesn't make any sense.

  4. #4
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    woops sorry about that . for the star app. When it gets to line 3, row is 3 and star gets set back to 1, I don't understand how that happens.
    Java Code:
    //********************************************************************
    //  Stars.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of nested for loops.
    //********************************************************************
    
    public class Stars
    {
       //-----------------------------------------------------------------
       //  Prints a triangle shape using asterisk (star) characters.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          final int MAX_ROWS = 10;
    
          for (int row = 1; row <= MAX_ROWS; row++)
          {
             for (int star = 1; star <= row; star++)
                System.out.print ("*");
    
             System.out.println();
          }
       }
    }
    Java Code:
    //********************************************************************
    //  ReverseNumber.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of a do loop.
    //********************************************************************
    
    import java.util.Scanner;
    
    public class ReverseNumber
    {
       //-----------------------------------------------------------------
       //  Reverses the digits of an integer mathematically.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          int number, lastDigit, reverse = 0;
    
          Scanner scan = new Scanner (System.in);
    
          System.out.print ("Enter a positive integer: ");
          number = scan.nextInt();
    
          do
          {
             lastDigit = number % 10;
             reverse = (reverse * 10) + lastDigit;
             number = number / 10;
          }
          while (number > 0);
    
          System.out.println ("That number reversed is " + reverse);
       }
    }
    
    }
    Sorry Norm, I messed up copying it from the book I think or playing with it. Here is the code right from the book. thank you

    I am playing with the code in pencil and paper now and added a print statement of the vars to try to see,. don't get it yet though, trying.
    Last edited by silverglade; 05-19-2011 at 08:50 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default

    Have you tried to compile what you posted? It doesn't.

    You're wasting a lot of peoples' time by posting junk.

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default

    See the other forum.
    The OP posted good code there.

  8. #8
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    I GOT IT!!! hahahahaha. Bang goes the pinata busting open with candy. Ok here is the explanation from my genius brain.LOL

    I still don't understand the ReverseNumber app, but here is the star app explanation. Basically, Each entry into the inner for loop resets "star" to one. So that on the 3rd outer loop, then inner, star is reset to 1, and "row' is set to 3, so then the inner loop runs three times, and prints out 3 stars on line 3. Then for the 4th outer loop, row is set to 4, go to inner star loop which is set to 1 again, and loop 4 times on that line. and so on. SWEET MAMA!:cool:

    Thank you Norm! for the post on the other forum, I will examine your answer. thank you! Derek


    Ok that just expanded my brain size by a few millimeters and I feel insane, but I understand the ReverseNumber app. Here is the wacky solution.

    On the first iteration of the do while loop, lastDigit=4, then reverse=4, number = 123, then you output to screen reverse, which is 4.
    Second iteration: lastDigit=3, reverse = 3, number= 12, and output of reverse is 3. Keep doing this until your first input of "1234", is output as "4321" Bing bang boom. Ta da! thank you to all my fans.
    Last edited by silverglade; 05-19-2011 at 09:46 PM.

  9. #9
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    I tried to mark this a solved but it's not showing up in thread tools.

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2011, 12:37 AM
  2. 4 digit restriction
    By GTM in forum Java Applets
    Replies: 3
    Last Post: 12-28-2010, 02:26 AM
  3. Java String Digit Question
    By toaster_man in forum New To Java
    Replies: 5
    Last Post: 12-12-2010, 09:08 PM
  4. a question about pascal triangle
    By rambo126 in forum New To Java
    Replies: 7
    Last Post: 10-30-2010, 07:17 PM
  5. Replies: 1
    Last Post: 08-29-2008, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •