This site requires JavaScript, please enable it in your browser!
Greenfoot back
genju
genju wrote ...

2020/4/22

trouble with superclasses

genju genju

2020/4/22

#
So this is my Soldier Superclass
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Soldier here.
 * 
 * @author (Prayan Jegathees) 
 * @version (0.1)
 */
public abstract class Soldier extends Actor
{
    /**
     * Private variables used for all soldier subclasses 
     *      stats needed to determine the overall stats 
     *      of the character
     *      
     * LIFE is a boolean meant to change & detect the actors "death" 
     * TEAM is a string meant to choose which alliance the actor belongs to 
     */
    private int health;
    private int speed;
    private int atkPower;
    private int atkRate;
    
    private boolean life;
    private String team;
       
    /**
     * Constructor for all Soldiers, 
     */
    
    public Soldier(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        health = hp;
        speed = spd;
        atkPower = aPwr;
        atkRate = aRt;
        
        alliance = team;
        life = true;
    }
    
    /**
     * Attack - An abstract method that is called when the unit subclass 
     *          wants to deal damage to an enemy unit
     */
    public abstract void attack();
    
    /**
     * takeDamage - When soldier subunits are hit by projectiles and lose health
     *              (amount taken varies based on the type of projectile)
     */
    public void takeDamage()
    {
        /*
        if (isTouching(PROJECTILE_ARROW.class))
        {
            health -= 2;
        }
        if (isTouching(PROJECTILE_FIREBL.class))
        {
            health -= 5;
        }
        */
        if (health == 0)
        {
            life = false;
        }
    }
    
    public void die()
    {
        if (!life)
        {
            move(0);
            getImage().setTransparency(50);
            setRotation(90);
        }
    }
}
its for a group project and im planning on combining my work with a friends (hence why i commented what i did in the takeDamage() method. Everything looks fine in this class yet when i create my subclasses and even added the attack method to both, i get an error on the c in the class line (which i will bold) saying "constructor Soldier in class Soldier cannot be applied to given types; and proceeds to list parameters in the constructor of Soldier.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heavy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Heavy extends Soldier
{
    /**
     * Act - do whatever the Heavy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    
    public void attack()
    {
        
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Knight here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Knight extends Soldier
{
    /**
     * Act - do whatever the Knight wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    
    public void attack()
    {
        
    }
}
i dont see whats wrong. i'd assume its because all the private instance variables are not protected meaning that the subclasses wouldn't be able to access them but im not sure. i would change it just to see but i dont want to screw up my code more by accident so im okay with waiting for a response, thanks for the help in advance!
Super_Hippo Super_Hippo

2020/4/22

#
Your Soldier class has one constructor. The one with a bunch of parameters. The subclasses should have a constructor which exists in the super class. So you could either create a constructor in the Soldier class without parameters. And/or the subclasses constructors should match. So the constructor of your Heavy class could look like this.
    public Heavy(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        super(hp, spd, aPwr, aRt, alliance); //class the constructor of the superclass Soldier
        //add stuff here which should happen to Heavy objects upon creation, but not to all Soldiers
    }
You need to login to post a reply.