File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""The Art and Science of Java" Chapter 4 Question 2" Watch ""The Art and Science of Java" Chapter 4 Question 2" New topic
Author

"The Art and Science of Java" Chapter 4 Question 2

Darrin Altman
Greenhorn

Joined: Jul 10, 2011
Posts: 15

I need to take the song "This old man" and make the necessary changes of the number and rhyming word. I was able to do it but I believe I was supposed to use a for statement or a while statement. Here is what I came up with. Any suggestions on improving this. Thank you for any help.

shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Hi Darrin,

Looks like switch-case will be useful for you.

Have a look at this sample program.http://www.cafeaulait.org/course/week2/42.html

If you are printing upto n, for(int b =0;b<n;b++). Use the switch case inside this for loop

switch(b)
case(1):
break;
case(n):
break;
default:
break;

Thanks




Darrin Altman
Greenhorn

Joined: Jul 10, 2011
Posts: 15

shuba gopal wrote:Hi Darrin,

Looks like switch-case will be useful for you.

Have a look at this sample program.http://www.cafeaulait.org/course/week2/42.html

If you are printing upto n, for(int b =0;b<n;b++). Use the switch case inside this for loop

switch(b)
case(1):
break;
case(n):
break;
default:
break;

Thanks






Thank you for your help. I didn't think about using the switch statement inside of the for statement. I will re write the program to make sure I get it. I sure hope I don't wear out my welcome, I seem to have trouble with each programming exercise.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Darrin, that is the learning process. dont fret about it
In case you dont get it ask more questions
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Darrin, I hope this helps. Please feel free to ask if you have any more questions about for loops.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40058
    
  28
Maylom Ventura,
Your post was moved to a new topic.
Goran Dragutinovic
Greenhorn

Joined: Jul 11, 2013
Posts: 2

Here is what I got. (After about 4-5 attempts at pulling my hair out of course).


import acm.program.*;

public class Chapter4 extends ConsoleProgram {
public void run() {
int i = 1;
String verse2 = "He played knick-knack on my ";
for (i=1; i<=10; i++) {
println("This old man he played " + i + ".");
switch (i){
case 1: println(verse2 + "thumb."); break;
case 2: println(verse2 + "shoe."); break;
case 3: println(verse2 + "knee."); break;
case 4: println(verse2 + "door."); break;
case 5: println(verse2 + "hive."); break;
case 6: println(verse2 + "sticks."); break;
case 7: println(verse2 + "heaven."); break;
case 8: println(verse2 + "pate."); break;
case 9: println(verse2 + "spine."); break;
case 10: println(verse2 + "shin."); break;
}
}
}
}

Hopefully that will help someone out who, like myself, is using the Art of Java book in 2013....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "The Art and Science of Java" Chapter 4 Question 2