π» 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
- Steps
Instructions & Requirements
- Create the Class
- Create a new file named
Creature.java. - Define a
publicclass namedCreature.
- Create a new file named
- Declare Instance Variables
private String species;(required)- At least one other
Stringvariable - At least one
intvariable - At least one
doublevariable - At least one
booleanvariableMake sure all instance variables are
private!
- 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
thiskeyword!
- Write Accessor/Getter Methods
- Write a
publicget method for each instance variable to allow external access to their values.Make sure your
returntype matches the instance variable!
- Write a
- Write Mutator/Setter Methods
- Write a
publicset 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!
- Write a
- Write a toString Method
- Override the
toString()method toreturna String representation of the object that includes all instance variables.
- Override the
- 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.
- 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.
- Write a Test Class
- Create a separate
Main.javaclass with amain()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.
- Create a separate
Extension
Make it Interactive!
After ensuring your test class works as intended, update it to allow for user interaction:
- Import the
Scannerclass at the top of your program. - Set up a
Scannerobject instance in themainmethod. - 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 (
voidor 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.