πŸ’» Unit 3 Project: Creature Class

Overview & Setup

In this project, you will practice defining and working with Java classes by creating an interactive Creature class. You’ll demonstrate understanding of:

  • instance variables
  • constructors
  • getter and setter methods
  • the toString method
  • behavior methods
πŸ“₯ PROJECT SETUP & SUBMISSION INSTRUCTIONS

Part A:

1. Section

  1. Steps

Instructions & Requirements

  1. Create the Class
    • Create a new file named Creature.java.
    • Define a public class named Creature.
  2. Declare Instance Variables
    • private String species; (required)
    • At least one other String variable
    • At least one int variable
    • At least one double variable
    • At least one boolean variable

      Make sure all instance variables are private!

  3. Write Two Constructors
    • Write a no-argument constructor that sets default values for all instance variables.
    • Write a parameterized constructor that initializes all instance variables based on arguments passed to it.

      Make sure to use the this keyword!

  4. Write Accessor/Getter Methods
    • Write a public get method for each instance variable to allow external access to their values.

      Make sure your return type matches the instance variable!

  5. Write Mutator/Setter Methods
    • Write a public set method for each instance variable to allow external modification of their values.

      Make sure your method accepts a parameter of the same data type as the instance variable!

  6. Write a toString Method
    • Override the toString() method to return a String representation of the object that includes all instance variables.
  7. Write Behavior Methods
    • Add at least TWO additional behavior methods that define actions the creature can perform. These methods can use the instance variables and return results, perform tasks, or indirectly affect the state of instance variables.

      Examples:

      • public void grow(int years) - Increases the age of the creature by a given number of years.
      • public String speak() - Returns a string like β€œThe Dragon lets out a mighty roar!” based on the species.
  8. Write a Test Class
    • Create a separate Main.java class with a main() method that demonstrates all features of your Creature class.

      In the main method:

      • Create at least two Creature objects using different constructors.
      • Use the getter and setter methods to access and modify instance variables.
      • Call the behavior methods and display their outputs.
      • Print the objects using the toString method.

Extension

Make it Interactive!

After ensuring your test class works as intended, update it to allow for user interaction:

  1. Import the Scanner class at the top of your program.
  2. Set up a Scanner object instance in the main method.
  3. Prompt the user to input values for each instance variable, store those values in variables, then pass those variables into your parameterized constructor.

Allow the user to run your behavior methods as they choose, demonstrating the output or effects in a way that makes sense:

while (!userInput.equals("quit") {
  System.out.println("What would you like to do with your creature - speak or grow?");
  userInput = scan.nextLine();
  if (userInput.equals("speak")) {
    System.out.println(creature.speak());
  }
  else if (userInput.equals("grow")) {
    System.out.println("Enter a whole number: ");
    int years = scan.nextInt();
    creature.grow(years);
    System.out.println("Your creature is now " + creature.getAge() + " years old!");
  }
  else {
    System.out.println("That command is not recognized, try again.");
  }
}

Different types of methods (void or returns data, if it needs input or not) will require different handling as seen in the example above.

You can also turn your text-based interactive version into a visual one with a GUI (Graphical User Interface)! See my Java Swing demo: GitHub Swing GUI and make sure to copy your completed Creature.java file in the repository too.