OOP Part 2 #
[0] Melee #
Today’s lab will build on the code from last class.
๐ป
open up your lab_oop_yourgithubusername
repository in IntelliJ
Add Melee class #
๐ป
Make new class Melee
inside the model
package
- Right-click on the package
model
- Select
new > Java class
- Name the class
Melee
- Add the class to
git
when IntelliJ prompts you
Main Method #
๐ป
Add a main()
method to Melee
.
Main methods always have the same declaration:
public static void main(String[] args)
public
: this means it can be accessed outside of this classstatic
: this means this method belongs to the whole class, not just one particular objectvoid
: this means it will not return anythingString[] args
: this means that this method can accept parameters from the user
๐ป
Create 4 Superhuman
objects in the main()
method.
Since they are in the same model, Melee
can access the Superhuman
class. However, it can only access attributes/methods that have the public
access modifier.
Because the main()
method is static
, we can run it even before we have created a constructor for Melee
.
Be sure to test our your main()
method to make sure it’s working before moving on!
Attributes and Constructor #
๐ป
Add the following attributes to the Melee
class.
villains
: anarray
of size 20, that holdsSuperhuman
objectsheroes
: anarray
of size 20, that holdsSuperhuman
objectsvillainPoints
: intheroPoints
: int
How to use arrays:
int[] myArray; // declare a new array variable
myArray = new int[20]; //create an new array of integers and save it in your variable
myArray[0] = 8; //adds the integer 8 at index 0
๐ป
Add a constructor
to Melee
.
The constructor is the method that runs when a new object is created. Remember, the constructor must have the same name as the class. Take a look at the constructor from the other classes in this lab for inspiration.
The Melee
constructor should takes no parameters. It creates arrays for villains
and heroes
. It sets bothvillainPoints
and heroPoints
to zero.
Constructors automatically return an object of their class, so you don’t need any return statement
[1] Fight #
Add Attributes to Superhuman #
We want to make it so that two of the superhumans can fight. However, first we need to know how what power the superhuman has, and how strong they are:
๐ป
Add three attributes: strength
, powers
, and isVillain
to the Superhuman
class.
strength
: an int
that stores how powerful they arepower
: a String
that stores the name of their powerisVillain
: a boolean
that stores whether the super is a villain or not.
๐ป For each new attribute, create a setter and a getter (aka mutator and accessor).
๐ป
Update the Superhuman constructor to include parameters for the new attributes. You will also need to update any Superhuman
objects that you create to include these new attributes.
Add Fight method #
๐ป
Add a method fight()
to the Melee
class.
Parameters
: twoSuperhuman
objects, ahero
and avillain
Return
: oneSuperhuman
object (the winner of the fight)- The winner should be whoever has a higher strength
- The loser should reduce their strength by 1 point
- The method should print a message to the terminal saying who won and what their power was.
- The method should increase
heroPoints
orvillainPoints
, depending on who won the fight.
๐ป
Be sure to thoroughly test fight()
in the main()
method.
[2] Sort the Superhumans #
๐ป
In the main()
method, create at least 10 Superhuman
, and add 5 to each of the arrays, villains
and heroes
.
๐ป
Add a method sortSupers()
, that sorts the Superhumans
array based on their ages.
[3] Deliverables #
โกโจPush to Github #
๐ป Select Commit from the menu on the left.
Select all your updated files. Be sure to include a descriptive commit message.
๐ป Click Commit and Push
๐ป Click Push