• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

homework help

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi everyone, I am new to this site.

So I have this exercise to do which wants me to:

1. Define a method tenGreenBottles that can print out the words to the song Ten Green Bottles. Using a loop for its implementation

2. Then in the same file define a function called xGreenBottles which when given the input x
(as an integer parameter) will produce and return the words of the song for a given value of
x green bottles (starting with x green bottles and removing 1 green bottle each time). Using a
loop for its implementation

3. Finally define a method called xyGreenBottles which will produce the words of a song where, starting with x green bottles, y bottles are removed every time until there are none
left. Your method therefore takes two integer parameters, x and y. Use a loop for its
implementation.

So far I have done the first part of the exercise, which i am not sure if it's is correct. Also could someone please help me out with the other parts of the exercise as i have tried searching Google, but nothing seems to help.

Thanks, this is the code I have so far:


 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ovais
Welcome to CodeRanch!

For the code you have so far, I have two comments to make it better:

1) I recommend naming the tenGreenBottles variable something else. i is a common name for a loop index. (It's confusing because there aren't ten bottles after the first iteration of the loop)
2) What does it do when the loop variable is 3? Also, the i> 3 case and i == 2 case look the same so you could combine it so you only have one if/else.

For the second part of the assignment, you need to refactor (edit) your existing code. Do you know how to pass a parameter to a method? You can create a new method by copying your existing one, adding that parameter and replacing 10 with that parameter. Then you can change your existing method to call it. Post what you get up to with this or what question you have.
 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

The difference between the i>3 case and I == 2 is the plural bottles and bottle

I have a basic idea for passing parameter s but not too much(sorry for being a noob). if you could guide me to the correct path, it would be appreciated.
 
Jeanne Boyarsky
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Ok. I think you want if(tenGreenBottles >= 3). Because right now 3 isn't in any of the groups.

As far as the parameter, here's an example:


Can you change this to a method with a parameter of the right type?

 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So would would it be?

 
Jeanne Boyarsky
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the first step. Then you replace the hard coded number "10" with that variable.
 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So where it says tenGreenBottles = 10; it would be tenGreenBottles = x; ?

Also how would I remove one bottle each time?

 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne has, as usual, given some good advice to you - the names of your classes, methods, variables - these should all be chosen carefully. After the first iteration of your loop, the variable tenGreenBottles will not be 10 green bottles anymore, so that isn't a good choice.

Anyway, I have got you started here, now you need to finish it off.

1) Add comments
2) Instead of having the values x and y hardcoded, go and research into how you can read input from the user
3) Change aux_GreenBottles so that the number of green bottles that fall cannot be greater than the number of green bottles left
e.g. if you call xyGreenBottles with x=30, y=4, then towards the end you will have 2 bottles left, now you can't take away 4 bottles if only 2 are left, can you, so instead of it saying

2 green bottles standing on the wall
2 green bottles standing on the wall
and if 4 green bottles should accidentally fall
there'll be -2 green bottles standing on the wall



it should instead say:

2 green bottles standing on the wall
2 green bottles standing on the wall
and if 2 green bottles should accidentally fall
there'll be 0 green bottles standing on the wall




 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys I appreciate the help. I will get back to you guys once I have figured this out
 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

So I had a look at the code and I understand that I have to use the Scanner class for user input. however i'm not really sure where I am supposed to use it, some help would be appreciated.

I tried everything I can think of..

Thanks...
 
Jeanne Boyarsky
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does it say in the assignment to take the sizes from the user?

You don't have to use a Scanner, but you can. You could just use Console's readLine() method. That gives you a String. Do you know how to convert a String into an int?
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne is right - if the assignment doesn't say you need to take in user input, then you probably need not worry about that and can simply hard-code values in. I only suggested it as it would be nice to have, but if it is too much for you to do, then don't worry about that at the moment.

Did you sort out the other bit I said you need to?
 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I managed to do xGreenBottles where I start from x and take 1 by hard coding a value.

However I also done xyGreenBottles by hard coding it in.

For example I done the following when calling them in the main

xGreenBottles (4);

and for xyGreenBottles I done.

xyGreenBottles (4, 2);

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what does your code look like now?
 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is nothing like the original code, but does this do everything the assignment is asking?

 
Saloon Keeper
Posts: 9556
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've continued to replicate the error in which case '3' is not handled.
 
ovais hussain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the reminder, I have now edited the i>3 case and changed it to i>=3. Other than is this code doing what the assignment is saying?

Thanks
 
Carey Brown
Saloon Keeper
Posts: 9556
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ovais hussain wrote:Hi,

Thanks for the reminder, I have now edited the i>3 case and changed it to i>=3. Other than is this code doing what the assignment is saying?

Thanks

Assuming that your X/Y method is correct you could express the other two in terms of that. Example:
Your X/Y still needs some tweaks as it says: "and if one green bottle should accidently fall".
 
today's feeble attempt to support the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic