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

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

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

Post by ghostheadx2 on Wed Aug 26, 2015 5:18 pm
([msg=89568]see I've spent two weeks trying to make this subclass work[/msg])

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
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


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

Post by cyberdrain on Wed Aug 26, 2015 5:50 pm
([msg=89569]see Re: I've spent two weeks trying to make this subclass work[/msg])

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.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


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

Post by ghostheadx2 on Wed Aug 26, 2015 6:11 pm
([msg=89572]see Re: I've spent two weeks trying to make this subclass work[/msg])

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?
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


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

Post by ghost107 on Wed Aug 26, 2015 6:14 pm
([msg=89573]see Re: I've spent two weeks trying to make this subclass work[/msg])

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).
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


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

Post by sarataylor3 on Tue May 21, 2019 2:13 pm
([msg=98251]see Re: I've spent two weeks trying to make this subclass work[/msg])

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
sarataylor3
New User
New User
 
Posts: 1
Joined: Tue May 21, 2019 2:11 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests