Inheritance Lab
1. Download a CASE Tool such as StarUML.
Create the following classes in your UML diagram, then connect them appropriately using the extends arrow (generalization):
Student
Professor
TeachingAssistant
Employee
Secretary
DepartmentChair
Janitor
SeminarSpeaker
Person
Course
Seminar
Lecture
ComputerLab
Export your diagram as a jpeg and mail it to the grader.
2. Translate this diagram into Java class declarations:
3. In a computer combat game a champion fights all opponent to the death. Here's the design:
Here's the declaration of CombatArena:
public class CombatArena {
private ArrayList<Fighter>
opponents = new ArrayList<Fighter>();
private Fighter champ = new
Fighter("The Champ");
public CombatArena() {
opponents.add(new
DrunkenMaster("Baracho"));
opponents.add(new
SwordFighter("Skeletor"));
// etc.
}
public void fight() {
int i = 0;
while(opponents.size() > 0
&& champ.getHealth() > 0) {
Fighter next = opponents.get(i);
next.hit(champ);
champ.hit(next);
if (next.getHealth() < 0) {
opponents.remove(next);
}
i = (i + 1) % opponents.size();
}
if (champ.getHealth() > 0) {
System.out.println("The
champ wins!");
} else {
System.out.println("The
champ is defeated!");
}
}
public static void main(String[] args)
{
CombatArena arena = new
CombatArena();
arena.fight();
}
}
Here's the declaration of Fighter:
public class Fighter {
private int health;
private String name;
public void decHealth(int amt) {
if (0 < health) {
health -= amt;
System.out.println(name + " has been hurt, his/her health is "
+ health);
}
}
public int getHealth() {
return health;
}
public String getName() {
return name;
}
public Fighter(String nm) {
name = nm;
health = 100;
}
public void hit(Fighter f) {
if (0 < health) {
int blow = (int)(health/5 * Math.random());
f.decHealth(blow);
}
}
}
Here's the sample output:
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 100
Baracho has been hurt, his/her health is 98
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 90
Skeletor has been hurt, his/her health is 90
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 74
Baracho has been hurt, his/her health is 96
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 67
Skeletor has been hurt, his/her health is 83
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 56
Baracho has been hurt, his/her health is 86
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 44
Skeletor has been hurt, his/her health is 83
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 42
Baracho has been hurt, his/her health is 79
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 27
Skeletor has been hurt, his/her health is 81
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 22
Baracho has been hurt, his/her health is 77
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 18
Skeletor has been hurt, his/her health is 81
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 14
Baracho has been hurt, his/her health is 76
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 11
Skeletor has been hurt, his/her health is 81
Baracho barfs on The Champ
The Champ has been hurt, his/her health is 4
Baracho has been hurt, his/her health is 76
Skeletor stabs The Champ
The Champ has been hurt, his/her health is 0
The champ is defeated!
Implement DrunkenMaster and SwordFighter.