Question
Objective: In this activity you will begin practicing basic class construction. You can either work locally on your computer using IntelliJ or directly in the
Objective: In this activity you will begin practicing basic class construction. You can either work locally on your computer using IntelliJ or directly in the zyLab IDE below. IntelliJ is recommended.
Step 1: defining fields of the MovieCreature class
Create an empty class called MovieCreature and put the following enumerated type inside the class:
enum CreatureSize { SMALL, MEDIUM, LARGE }
Next, add fields to the MovieCreature class to store the following information:
a name (of type String)
a date of birth (int)
a date of death (int)
a size (CreatureSize)
Make sure the above fields are marked private to restrict access to them from outside the class.
Step 2: defining constructors
You will define two constructors for the creature class. The first constructor should take (in order) the following parameters:
the name of the creature
the date of birth of the creature
the date of death
the size of the creature (the parameter for this should be of type CreatureSize)
The second (overloaded) constructor you define for the creature class should only take (in this order):
the name of the creature
the date of birth of the creature
the size of the creature (the parameter for this should be of type CreatureSize)
Both constructors should initialize all the fields detailed in Step 1.
Notice that in the first constructor, both a birth and death date must be supplied (thus the creature can be assumed to be dead). But in the second constructor, only a birth date is supplied so the second constructor assumes the creature is still alive. How you would like to track the alive (or dead) status of the creature within the class is up to you.
One straightforward possibility: use the convention that a death date < 0 means the creature is still alive
Step 3: required methods (make sure your method names match these exactly)
Here are the public methods your class should offer:
getSize() -- returns the size of the creature (return type should be: CreatureSize)
getName() -- returns the creature's name,
getDeathYear() -- returns the creature's death year,
setDeathYear(int y) -- takes an int, returns nothing, and updates the creature's deathYear,
isAlive() -- that takes nothing as a parameter and returns true if the creature is still alive; false otherwise.
Lastly, add a toString() method that renders your creature like so:
creature: [name], [size], ([birthDate]), still around today ? [isAlive()]
(note: the square brackets shown above can be omitted, just print the field enclosed inside the square brackets [..])
Step 4: creating some creatures
In the tester file's main method, instantiate the specified creatures c1, c2, and c3 detailed in the "todo" comments.
Handin: Be sure that you're on submit mode; the last submission you make before the deadline will be the one we grade.
public class Tester { public static void main(String[] args) { // todo: uncomment when ready // MovieCreature c1 = new MovieCreature("godzilla", 2013, MovieCreature.CreatureSize.LARGE); // todo: c2 should be a creature named "sam the alien" born in 1981 that died in 2019 (medium sized) // todo: c3 should be a creature named "merman" born in 2012 (small sized) // todo: System.out.println(c1.toString()); // todo: print out c2 and c3 using toString as well. } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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