Methods in Classes
Part A
Create a project using the usual naming convention. You will need a class with a main method.
☑ Add Bunny.java to your project (you will need to fix the package statements).
The following methods will not need the keyword static (they aren't in the same class as main). (hint: these methods will be called later from main, using Bunnys that you will have created in main. You do not need to know what those variables will be called to write these methods)
In all these methods, use this. in front of all instance variables.
☑ To Bunny, add a method breed which takes one Bunny as parameter. It breeds the passed-in Bunny with the current Bunny (i.e. this), and makes a third, new Bunny. Return a new Bunny that has as its name a combination of the two Bunny names, and as its carrotCount the sum of the two carrotCounts.
So if first is a Bunny named "Firstbun" who has 1 carrot and
second is a Bunny named "Secondbun"
who has 2 carrots, then
first.breed(second) would return a Bunny whose name is "Firstbun-Secondbun"
and who has 3 carrots and
second.breed(first) would return a Bunny whose name is "Secondbun-Firstbun" and who has 3 carrots.
Test in your main that this works; to do this you will need to create several Bunnys in main and give them names and carrotCounts.(hint: the breed method is returning a Bunny)
☑ To Bunny add a method swapCarrots, which takes a Bunny as parameter. In the method, swap the two Bunnys' carrotcounts, so if Bunny buster has carrotcount 7 and Bunny babs has carrotcount 99, then if we call buster.swapCarrots(babs) then buster ends up with 99 carrots and babs ends up with 7.
Test in your main that this works.
Part B
☑ Create a new class Dog which has instance variables to hold a name, an age, and a best friend (which is another Dog).
☑ Add a toString that produces a String of the form
Fluffy (age 8) whose best friend is Scruffy
(using the best friend's name) or, if the dog has no best friend (best friend is still null), just include the dog's own name and age.
☑ Add a method becomeFriends which takes another Dog as parameter. Set the best friend instance variables so that each dog becomes the other dog's best friend.
test in your main that this works.
☑ Add a method buyDogBeer. If the dog is at least 21 in dog years (7 dog years in 1 human year) report that the dog has bought some dog beer. Otherwise if the dog has a best friend and that best friend is at least 21 in dog years, call the best friend's buyDogBeer method. If neither is of age or the dog is friendless, report that the dog has no access to dog beer.
test in your main that this works.
☑ [EC] To Bunny, add a method lineUpForDinner, which takes two Bunny variables. It announces which Bunny is in charge of lining them up, and then and prints the three Bunnys' names in the order they will line up for dinner. They should go in increasing order of carrotcount -- so the hungriest Bunny goes first. So if flopsy has carrotcount 9, mopsy has carrotcount 3, and peter has carrotcount 6, then peter.lineUpForDinner(flopsy, mopsy) would print something like "Peter is lining us up for dinner: Mopsy, Peter, Flopsy"