YUHEE1984 / CatsOneday

1 stars 0 forks source link

05 - Cat Breeding #5

Closed TheBeege closed 2 months ago

TheBeege commented 3 months ago

Goal

Learn about the idea of "message passing," and get experience with solving known problems.

This one is going to be a bit tougher, but you can do it!

Software Engineering Theory

You've already learned about some core ideas in object-oriented programming. An objects "knows" things and "does" things. An object knows things by storing attributes with data. An object does things by executing logic with methods.

There's another way to think about methods: message passing. This way of thinking about methods is especially common in simulation-style applications like video games. Imagine you're making a fighting game. Two characters are fighting each other. One character punches another character. The second character should take some amount of damage, right? But what factors decide how much damage that character takes? The first character can apply some amount of force to the punch. The second character has some amount of muscle or fat to help absorb the impact of the punch.

If we were to model these fighters in code, we would need to combine information from the first and second fighter together to determine how much damage the second fighter takes. We usually do this by passing some information between the two objects, and let each of them decide their own part of the interaction. So a punch method may look like this:

void punch(Fighter otherFighter, String location) {
    otherFighter.receiveHit("punch", this.punchForce, location);
}

The Fighter object would need the receiveHit method to receive the "message" and decide how to respond to the punch, its force, and its location. It might look like this:

void receiveHit(String hitType, int forceAmount, String location) {
    // These fighters are boxers, so they take reduced damage from punches
    int damageAmount = forceAmount;
    if (hitType == "punch") {
        damageAmount = damageAmount / 2;
        // We could also write the above as damageAmount /= 2;, but I want to make sure you can read it
    }
    this.health = this.health - damageAmount;
    // We could also write this as this.health -= damageAmount;
}

This is a good example of message passing. The first fighter "sends a message" to the second fighter that they're punching. The second fighter decides how to handle the received "message" of being punched, especially for special cases.

But let's make love, not war. Let's get to cat breeding!

Steps

Cat Breeding

  1. In your Cat class, create a new method: "breed." It should return an ArrayList<Cat>, and it should take in another Cat as a parameter. Don't forget to import java.util.ArrayList. It should be a public method.
  2. Create another method on Cat called handleBreeding. This should be a private method, since only Cat objects will ever use it. It should take in color, eye color, and fur pattern as parameters. (You can look at these attributes in your Cat class to determine the data types.) It should output an ArrayList of Cats.
  3. In your breed method, call the handleBreeding method of the other Cat from the parameters. For color, eye color, and fur pattern, provide the attributes from this Cat. Return the output of calling that method.
  4. In your handleBreeding method, randomly choose how many cats should be born, between 2 and 6.
    • "But Beege, how do I randomly choose?!" Hell, I don't know, but the internet does! Search online how to select a random number within some range. This type of work is very, very, very common in software engineering. We always look for examples of how to do basic things because we can't remember everything all the time.
  5. After determining how many cats will be born, we need a place to store the cats, and then we need to create each one. Create an ArrayList to store the cats in.
    • "But Beege, I don't know how to create an ArrayList!" Search :) (Or more likely, you learned it in one of your books but can't remember. In either case, an online search will be faster.)
  6. Next, write a loop that will run for once for each cat. So if 5 cats will be born, then the loop should run 5 times.
  7. In the loop, randomly choose between the color received as a parameter and the color of the current Cat object.
  8. Do the same thing for eye color and fur pattern.
  9. Randomly choose a weight between 3 and 5 kilograms. (We're not going to consider the kittens growing up, for now.)
  10. Randomly choose between male and female.
  11. Finally, add the new Cat to the ArrayList. For, just set the name to a blank string.
  12. Outside of the loop, return the ArrayList.

Great job! This is good, early, proper software engineering problem and practice. You've modeled a (simplified) real-world interaction in code that is usable. So let's use it!

Making Things Nice

  1. In your Cat class, add the Setter annotation to the name attribute.
  2. Back in your App.java, create a second Cat object.
  3. Have your first Cat breed with your second Cat. Store the output in an ArrayList.
  4. Create a String array of 6 names. Come up with some nice names! Store them in the array.
  5. Loop through each Cat in the ArrayList.
  6. Randomly select a name from your array of names and use setName to set the Cat's name in the loop.
  7. Print out the toString text for each Cat.

You did it! You created methods to allow cats to breed. You then tried out breeding some cats and got to see the new cats. Excellently done!

YUHEE1984 commented 2 months ago

...