My mommy always said there were no monsters. No real ones.
But there are.
Ellen Ripley, Alien Resurrection
WARNING: what follows is not proper Java style
package it.yourcompany.adventure;
At the beginning of source files you have to declare the package.
yourcompany.it
)it.youtcompany
)adventure
):Your files then MUST reside in folders like these:
- it
- yourcompany
- adventure
Monster.java
Game.java
Most IDE offer some automation to automatically place the files in the folders
Create a file called Game.java
with a public class Game
inside:
main
methodpackage it.yourcompany.adventure;
public class Game {
public static void main(String[] args){
System.out.println("Welcome to my dungeon, stranger....");
}
}
Run Game
class in Netbeans:
Create a file called Monster.java
with a public class Monster
inside
package it.yourcompany.adventure;
public class Monster {
public String name = "";
public int lifepoints = 0;
}
Java dogma says...
public class Game {
public static void main(String[] args){
System.out.println("Welcome to my dungeon, stranger....");
Monster monster1 = new Monster();
monster1.name = "Cerberus";
monster1.lifepoints = 7;
System.out.println();
System.out.println("monster1.name is : " + monster1.name);
System.out.println("monster1.lifepoints are: " + monster1.lifepoints);
}
}
Run the Game.java
, you should see some output.
This output printing is verbose:
System.out.println("monster1 name is : " + monster1.name);
System.out.println("monster1 lifepoints are: " + monster1.lifepoints);
monster1 name is : Cerberus monster1 lifepoints are: 7
This would be shorter, but....:
System.out.println("monster1.toString() is : " + monster1);
monster1 toString() is : it.yourcompany.adventure.Monster@b44d430f
It shows the physical address, not nice.
After you defined your fields:
toString
method implementation Select all fields:
package it.yourcompany.game;
public class Monster {
public String name = "";
public int lifepoints = 0;
// Java rituals ......................
@Override
public String toString() {
return "Monster{" + "name=" + name + ", lifepoints=" + lifepoints + '}';
}
}
You should now have a printable monster:
public class Game {
public static void main(String[] args){
System.out.println("Welcome to my dungeon, stranger....");
Monster monster1 = new Monster();
monster1.name = "Cerberus";
monster1.lifepoints = 7;
System.out.println();
System.out.println("monster1.toString() is: " + monster1);
}
}
Welcome to my dungeon, stranger....
monster1.toString() is: Monster{name=Cerberus, lifepoints=7}
Are they?
import java.util.List;
public class Game {
public static void main(String[] args){
System.out.println("Welcome to my dungeon, stranger....");
Monster monster1 = new Monster();
monster1.name = "Cerberus";
monster1.lifepoints = 7;
System.out.println();
System.out.println("monster1.toString() is: " + monster1);
Monster monster2 = new Monster();
monster2.name = "Cerberus";
monster2.lifepoints = 7;
System.out.println();
System.out.println("monster2.toString() is: " + monster2);
System.out.println();
System.out.println("monster1.equals(monster2) : " + monster1.equals(monster2));
System.out.println();
System.out.println("List.of(monster1).contains(monster2): " + List.of(monster1).contains(monster2));
}
}
Welcome to my dungeon, stranger....
monster1.toString() is : Monster{name=Cerberus, lifepoints=7}
monster2.toString() is : Monster{name=Cerberus, lifepoints=7}
monster1.equals(monster2) : false
List.of(monster1).contains(monster2): false
That's not ok.
Both .equals()
and in general collections search methods do not work by default with new classes.
Let's fix the problem.
You should now have a ton of more Java bla bla, see all on Github
package it.yourcompany.adventure;
import java.util.Objects;
public class Monster {
// stuff that matters
public String name = "";
public int lifepoints = 0;
// Java rituals ......................
@Override
public String toString() {
return "Monster{" + "name=" + name + ", lifepoints=" + lifepoints + '}';
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + Objects.hashCode(this.name);
hash = 89 * hash + this.lifepoints;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Monster other = (Monster) obj;
if (this.lifepoints != other.lifepoints) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
}
Rerun the Game:
Welcome to my dungeon, stranger....
monster1.toString() is : Hero{name=Cerberus, lifepoints=7}
monster2.toString() is : Hero{name=Cerberus, lifepoints=7}
monster1.equals(monster2) : true
List.of(monster1).contains(monster2): true
Monster monster3 = new Monster();
monster3.name = "Zombo";
monster3.lifepoints = 4;
System.out.println();
System.out.println("monster1.equals(monster3) : " + monster1.equals(monster3));
System.out.println();
System.out.println("List.of(monster1).contains(monster3): " + List.of(monster1).contains(monster3));
System.out.println();
System.out.println("List.of(monster1, monster3).contains(monster3): "
+ List.of(monster1, monster3).contains(monster3));
monster1.equals(monster3) : false List.of(monster1).contains(monster3): false List.of(monster1, monster3).contains(monster3): true