Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please make a Creature class that has: Private fields for: (string) name, (string) description, (int) endurance, (string) filename, an array of phrases (size of
Please make a Creature class that has:
-
- Private fields for:
- (string) name,
- (string) description,
- (int) endurance,
- (string) filename,
- an array of phrases (size of at least 3) that the creature might say,
- and the (int) index of the last phrase used.
- Private fields for:
-
- A constructor that takes no parameters. This constructor should ask the user to supply all the default values for the fields. The last phrase used index field can be default to 0.
- A method to save the creature to a data file that takes no parameters. This method should write all the data for the creature to the file named in the filename field, in a format that would allow it to be loaded by the second constructor.
- A method to save the creature to a data file that takes a string as a parameter. The string passed should be used as the new filename.
- Hint: You can just change the filename field, and then call the save method that doesn't take any parameters. It should then save to the file in the filename field (now with the new value). Doing it this way, you don't have to rewrite the save method!
- A second constructor that takes a filename, stores it in the filename field, and uses that filename to load all of the data for the creature from the file to initialize all of the fields you created. (This means you will create file in any format you need to make this work.)
- A getter method for fields
- name
- description
- endurance
- A setter method for
- Name
- description
- A setter method for the endurance. In this setter method make sure the endurance can get to 0 but cannot go below it. This setter method should have the following functionality:
- If a value < 0 is passed, just set the endurance to 0.
- If endurance is ever set to 0, and the creatures endurance is not already 0, have the creature print a string that shows that it is tired to the console.
Example: Lex meows in exhaustion.
- If the endurance is set to >0 and it was 0, have it print out "
feels better." To the console. - A method that returns false if the creature is knocked out (<= 0 endurance) and true otherwise.
- A method that returns a phrase from the array of phrases the creature might say and updates the index of the last phrase so they will not say the same thing twice in a row, but it will rotate through what it says.
- A method that takes a string as a parameter and changes a phrase in the array to that new string. This method should also update the index of the last phrase used.
For instance:The first time you call the method it should overwrite phrases[0], the second time it should update phrases[1], the third time phrases[2] and the fourth time phrases[0] again.
- A toString() method for this class so if you print the creature object you can see all of the state of the creature.
- Add one additional method of your choosing that returns a string saying how the creature reacts to that behavior.
Example: System.out.println( myCreature.tickle() );Output:You tickle Joe the Robot.He looks at you with emotionless eyes and says, "Ha... ha... ...ha"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres an implementation of the Creature class based on the provided description import javaioBufferedReader import javaioBufferedWriter import javaioFileReader import javaioFileWriter import javaioIOE...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started