Results 1 to 9 of 9
- 05-19-2011, 07:27 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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
The book explains it briefly but I still don't understand.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
__________________________________________________ ______
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.
- 05-19-2011, 07:47 PM #2
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!
- 05-19-2011, 08:13 PM #3
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.
- 05-19-2011, 08:18 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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(); } } }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 youJava 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); } } }
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.
- 05-19-2011, 08:22 PM #5
Have you tried to compile what you posted? It doesn't.
You're wasting a lot of peoples' time by posting junk.
- 05-19-2011, 08:58 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
this post is now posted on this forum..
paintComponent(page), Reverse digit, and Asterisks triangle outputter question (Beginning Java forum at JavaRanch)
- 05-19-2011, 09:16 PM #7
See the other forum.
The OP posted good code there.
- 05-19-2011, 09:17 PM #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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.
- 05-19-2011, 09:56 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Similar Threads
-
User Enters 4-Digit Integer, Console Returns 1 Digit Per Line
By STANGMMX in forum New To JavaReplies: 0Last Post: 01-23-2011, 12:37 AM -
4 digit restriction
By GTM in forum Java AppletsReplies: 3Last Post: 12-28-2010, 02:26 AM -
Java String Digit Question
By toaster_man in forum New To JavaReplies: 5Last Post: 12-12-2010, 09:08 PM -
a question about pascal triangle
By rambo126 in forum New To JavaReplies: 7Last Post: 10-30-2010, 07:17 PM -
How to add or code for 'Question and Answer' on product page of website
By 82rathi.angara in forum Advanced JavaReplies: 1Last Post: 08-29-2008, 01:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks