GENERAL INTRUCTIONS:
What value would each of these return?Why won't this code compile?
return 8 > Math.rand(); public String theAnswer() { int x = 42; return ""+ x;} public String theQuestion0() { int x = 42; return "x";} public boolean theQuestion1(boolean toBe) {return toBe || !toBe ;} public boolean theQuestion2(boolean toBe) {return toBe && !toBe ;} public boolean theTruth(boolean toBe)
{return theQuestion1(toBe) || theQuestion2(toBe);}
class simple
{
public String greeting(boolean happy)
{
if(happy) return "Yo Ho!";
return "bugger off!"
}
}
INTERFACES
Given the following interface hierarchy:
interface Warrior
{
public int get_Strength();
public int get_Intelligence();
public void reduce_hit_points(int h);
public boolean are_you_dead_yet();
}
interface Fighter extends Warrior
{
public WeaponType getWeapon();
}
interface Magic_User extends Warrior
{
public int get_Magic_Resistance();
public void conduct_spell_dual(Magic_User m);
public boolean learn_new_spell(Spell s);
}
interface Druid extends Magic_User
{
public boolean Can_Heal?(Warrior w);
}
Answer these questions:
- Draw out the hierarchy of interfaces as a tree, with arrows indicating which interfaces extend which interfaces.
- List all of the methods that a Magic_User must implement.
- Can a Druid learn a spell? (why or why not?)
- Let's say someone designs a class, called "Apprentice", that implements Magic_User. Do you know if an Apprentice (that is, an object that implements the Apprentice class) can heal a warrior? Is it possible? Why or why not?
- Let's say we want to create a "Barbarian" class. Barbarians are warriors with no weapons (very primitive). Does the above hierarchy allow the creation of a class that implements just the warrior interface? Why or why not?
- Write the syntax for implementing a class "Elf" that implements as many of the above interfaces as possible.