Page 1 of 1

I've spent two weeks trying to make this subclass work

PostPosted: Wed Aug 26, 2015 5:18 pm
by ghostheadx2
So, I have an Alien subclass, that I want to make so that when I roll the die, flip the coin, whatever then:

if the roll is equal to block, return "nothing happens"

if the roll is greater than block, subtract the block from the base damage

if the roll is less than block, then return "blocked, zero damage"

This is not what's happening, and I have code commented out below that also isn't working. Could someone help me with this. This is the subclass:

Code: Select all
//Alien Class

class Alien extends Fighter{
   
   Alien(int block, String name, int health, int attack, int defense){
      super(name, health, attack, defense);
      setBlock(block);
   }

     
      private int block;
     
      private void setBlock(int block){
         this.block = block;
      }
     
      public int getBlock(){
         return block;
      }

   public int attacked(int baseDamage){
      if (super.roll==block){
         return 0;
         //return "nothing happens";
      }
       return baseDamage - block;
   }
}
/*     
      else{
         double a = Alien.roll();
         Alien.health-= a;
      }
      if (Alien.health > 0){
         System.out.println(health);
      }
       
   }
   
   public void attack(fighter opponent){
      opponent.attacked();
     
      if(opponent.health==0){
         super.victorious;
         System.exit(0);
      }
   }
     
}

*/


And this is the superclass:

Code: Select all
import java.util.*;

public class Fighter {
    public Fighter(String name, int health, int attack, int defense) {
        setName(name);
        setHealth(health);
        setAttack(attack);
        setDefense(defense);
    }
   
    public double roll;
    public String name;
    public int health;
    public int attack;
    public int defense;
   
    private void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    private void setHealth(int health) {
        this.health = health;
    }

    public int getHealth() {
        return health;
    }

    private void setAttack(int attack) {
        this.attack = attack;
    }

    public int getAttack() {
        return attack;
    }

    private void setDefense(int defense) {
        this.defense = defense;
    }

    public int getDefense() {
        return defense;
    }
    public double roll(double block){
        block = (int)(Math.random() * 100);
        return block;
    }
}


I want to fix the subclass because the superclass works perfectly. The huge portion of commented out code would be nice if someone wanted to answer but right now I'm only desperate to fix the code before the "/* */" comment. If someone wants to help me with what's in between, I know I've probably fucked the whole thing up by asking.

Please let me know if I'm spamming or posting something too often or whatever. Thanks so much.

sincerely,

your friendly neighborhood clutz, ghostheadx2

Re: I've spent two weeks trying to make this subclass work

PostPosted: Wed Aug 26, 2015 5:50 pm
by cyberdrain
ghostheadx2 wrote:
Code: Select all
public int attacked(int baseDamage)

The keyword here is int, as far as I know this means the value returned has to be an integer. You cannot return a string "nothing happens" then. Then again, I've never really programmed Java, so I might be wrong here.

Re: I've spent two weeks trying to make this subclass work

PostPosted: Wed Aug 26, 2015 6:11 pm
by ghostheadx2
I know. That's why I switched it to return 0. So then, should it be void and I just PRINT (rather than return), the values?

Re: I've spent two weeks trying to make this subclass work

PostPosted: Wed Aug 26, 2015 6:14 pm
by ghost107
ghostheadx2 wrote:So, I have an Alien subclass, that I want to make so that when I roll the die, flip the coin, whatever then:

if the roll is equal to block, return "nothing happens"

if the roll is greater than block, subtract the block from the base damage

if the roll is less than block, then return "blocked, zero damage"

This is not what's happening, and I have code commented out below that also isn't working. Could someone help me with this. This is the subclass:
Code: Select all
public int attacked(int baseDamage){
      if (super.roll==block){
         return 0;
         //return "nothing happens";
      }
       return baseDamage - block;
   }
}


You want to return a string when the method return type is int(it should be string).

Things noticed, class access modifiers(public, protected private).
- public means it can be accessed from inside and outside of the class that contains the public modifier
- protected means it can be accessed from inside the class that inherits the class that contains the protected modifier, also it be can accessed from inside the class that contains protected modifiers.
- private means it can only be accessed from the class that contains the private access modifier.

To put it simply if you have public getters and setters methods for the class attributes, you shouldn't make the class attributes public(make them private or protected if they need to be accessed from a inherited class), because having getters and setters for them is pointless.

ghostheadx2 wrote:Please let me know if I'm spamming or posting something too often or whatever. Thanks so much.

You can create a Thread called "Your java projects" and post your problem there, and when the problem is solved just continue with your next problem on the same thread(its easier then spamming threads).

Re: I've spent two weeks trying to make this subclass work

PostPosted: Tue May 21, 2019 2:13 pm
by sarataylor3
After years of not sleeping well and not finding the right guide for natural products online, she decided to do the research so others don’t suffer when buying a new mattress as she did. For the past 3 years, she studied everything about sleep and natural product to put the best natural mattress buying guide online.
regards
best mattress for back pain