announcements‎ > ‎

2015-10-16 HW12

posted Oct 16, 2015, 11:30 AM by Samuel Konstantinovich   [ updated Oct 16, 2015, 10:42 PM ]
Sub class warfare:

-Default constructor super() is called in any sub class, EXCEPT when you explicitly call another version of super(...).
-If the super class has no default constructor, you will get an error if it is implicitly (automatically) called. 

Homework: place in the HW12 directory. You may use my Actor class. I assume you have all the methods in my actor class. 

disclaimer: I believe these three choices to be in poor taste, as they are making light of negative stereotypes that many people (including most of you) combat on a daily basis(outside of the ivory tower that is Stuyvesant). I will give you what you asked for, begrudgingly... because I would be more offended by the prospect of taking away the choice that I gave you initially. My only demand is that you be mindful of how offensive this could become, and you stay away from any descriptions in your text that would make it worse. Basically: Tread carefully with the freedom you are given, don't be a troll... I hate trolls.
1. Write the three sub-classes of Actor: 
Nerd
Jock
Teacher
You should give them a special resource, and also override methods that are needed. 

2. Write down any things you are not sure why we are doing post them on piazza. Period 7 will be more confused because of the fire drill. Never be ashamed of not knowing, only be ashamed of choosing not to learn... and of being late. 

Here is the Actor class:
public class Actor{
    private String name;
    private int strength,agility,intelligence;
    private int health;

    //1. Must override the three consructors
    public Actor(String name, int str, int agi, int intel, int health){
        this.name = name;
        agility = agi;
        strength = str;
        intelligence = intel;
        this.health = health;
    }

    public Actor(String name){
        this(name,10,10,10,20);
    }

    public Actor(){
        this("Peon",10,10,10,20);
    }

    public String toString(){
        return name + " hp: "+health+ "\ns/a/i: "+strength+"/"+agility+"/"+intelligence;
    }


    public int takeDamage(int damage){
        health -= damage;
        return damage;
    }
    public String getName(){return name;}
    public int getInt(){return intelligence;}
    public int getStr(){return strength;}
    public int getAgi(){return agility;}
    public int getHealth(){return health;}

    //2. need to override all methods below this comment
    public String attack(){
        return name+" punches the target";
    }

    public String specialAttack(){
        return name+" wildly attacks the target";
    }

    //how much special resource is left
    public int getResource(){return 0;}

    //what is the name of the special resource
    public String getResourceName(){return "";}
}

Sample Wizard Class:
public class Wizard extends Actor{
    private int mana;

    public Wizard(String name, int str, int agi, int intel, int health){
        super(name,str,agi,intel,health);
        mana = 20;
    }
    public Wizard(String name){
        this(name,8,10,15,15);
    }
    public Wizard(){
        this("Oz the great and powerful");
    }

    public String attack(){
        return getName()+" swings his staff at the target";
    }

    public String specialAttack(){
        return getName()+" blasts the target with cosmic energy";
    }

    public String getResourceName(){
        return "mana";
    }
    public int getResource(){
        return mana;
    }
}
Comments