Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Darrin Altman
Greenhorn
Posts: 15
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darrin, that is the learning process. dont fret about it
In case you dont get it ask more questions
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darrin, I hope this helps. Please feel free to ask if you have any more questions about for loops.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49773
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maylom Ventura,
Your post was moved to a new topic.
 
Goran Dragutinovic
Greenhorn
Posts: 2
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic