-3

Sorry, I know that this code can be shortened significantly here and there, I just haven't really gotten how inheritance really works.

My question is, how do I make it so that the methods within my machoke and machop class don't print out those redundant stats.

public class FP {
    public void string() {
        System.out.println("A Standard fighting pokemon's stats are:");
    }

    public double weight() {
        double fWeight = 65;
        System.out.println("Weight: " + fWeight);
        return fWeight;
    }

    public double arm() {
        double fArm = 21;
        System.out.println("Arm Reach: " + fArm);
        return fArm;
    }

    public double leg() {
        double fLeg = 22;
        System.out.println("Leg Reach: " +fLeg);
        return fLeg;
    }

    public double punch() {
        double fPunch = 40;
        System.out.println("Punch Damage: " + fPunch);
        return fPunch;
    }

    public double kick() {
        double fKick = 35;
        System.out.println("Kick Damage: " + fKick);
        return fKick;
    }

    public double bodySlam() {
        double fBodySlam = 150;
        System.out.println("Body Slam damage: " + fBodySlam);
        return fBodySlam;
    }
}



public class Machop extends FP {
    public double DMG_MULTI = 1.5;

    public void string() {
        System.out.println("Machop's stats are:");
    }

    public double bodySlam(){
        double fBodySlam = super.bodySlam()*DMG_MULTI;
        System.out.println("Body Slam damage: "+fBodySlam);
        System.out.println();   
        return fBodySlam;
    }
}


public class Machoke extends Machop {
    public void string() {
        System.out.println("Machoke's stats are:");
    }

    public double weight() {
        double fWeight = super.weight()*1.75;
        System.out.println("Weight: " + fWeight);
        return 0;
    }

    public double arm() {
        double fArm = super.arm()*1.75;
        System.out.println("Arm Reach: " + fArm);
        return fArm;
    }

    public double leg() {
        double fLeg = super.leg()*1.75;
        System.out.println("Leg Reach: " +fLeg);
        return fLeg;
    }

    public double punch() {
        double fPunch = super.punch()*1.75;
        System.out.println("Punch Damage: " + fPunch);
        return fPunch;
    }

    public double kick() {
        double fKick = super.kick()*1.75;
        System.out.println("Kick Damage: " + fKick);
        return fKick;
    }

    public double bodySlam() {
        double fBodySlam = super.bodySlam()*2;
        System.out.println("Body Slam damage: " + fBodySlam);
        return fBodySlam;
    }

    public double submission(){
        double fSubmission = super.bodySlam()*1.5;
        System.out.println("Submission damage: " + fSubmission);
        System.out.println();   
        return 0;
    }
}


public class Call {
    public static void main(String args[]) {
        FP fp = new FP();
        fp.string();
        fp.weight();
        fp.arm();
        fp.leg();
        fp.punch();
        fp.kick();
        fp.bodySlam();
        System.out.println();

        Machop a = new Machop();
        a.string();
        a.weight();
        a.arm();
        a.leg();
        a.punch();
        a.kick();
        a.bodySlam();

        Machoke b = new Machoke();
        b.string();
        b.weight();
        b.arm();
        b.leg();
        b.punch();
        b.kick();
        b.bodySlam();
        b.submission();
    }
}

Output:

A Standard fighting pokemon's stats are:
Weight: 65.0
Arm Reach: 21.0
Leg Reach: 22.0
Punch Damage: 40.0
Kick Damage: 35.0
Body Slam damage: 150.0

Machop's stats are:
Weight: 65.0
Arm Reach: 21.0
Leg Reach: 22.0
Punch Damage: 40.0
Kick Damage: 35.0
Body Slam damage: 150.0
Body Slam damage: 225.0

Machoke's stats are:
Weight: 65.0
Weight: 113.75
Arm Reach: 21.0
Arm Reach: 36.75
Leg Reach: 22.0
Leg Reach: 38.5
Punch Damage: 40.0
Punch Damage: 70.0
Kick Damage: 35.0
Kick Damage: 61.25
Body Slam damage: 150.0
Body Slam damage: 225.0
Body Slam damage: 450.0
Body Slam damage: 150.0
Body Slam damage: 225.0
Submission damage: 337.5

Dorus
  • 7,276
  • 1
  • 30
  • 36
  • 5
    That's a *really* long sample, which makes it hard for us to find the problem. Could you give a *short* but complete example, e.g. just with a single method call? (It sounds like the output is due to `super` calls... if you don't want your methods to write out output, just don't do so...) – Jon Skeet Mar 16 '15 at 13:43
  • 1
    Also please properly format your code example, it makes it much easier to read. – RDM Mar 16 '15 at 13:47
  • To answer your question `"how do I make it so that the methods within my machoke and machop class don't print out those redundant stats."`: remove the `System.out.println` in the methods of the super class `FP`. – SubOptimal Mar 16 '15 at 13:51

2 Answers2

1

Most (if not all) of your functions call super. Because each implementation has a System.out.println(...) statement, your output is being clogged.

Generally, calling the overriden method in the class' super is not necessary unless it contains specific logic that is also part of the overwriting code.

There are a couple of things you could do to clean up your code. For starters, make the class FP abstract. (You should also probably rename the class to FightingPokemon just for readability.) Making the class abstract will prevent you from instantiating it. You don't want to make FightingPokemon instances as this is just a superclass that contains logic for all pokemon. You only want instances of actual pokemon, such as Machop and Machoke.

Secondly, you should remove all the System.out.println code. Replace this by an implementation of toString in each class, which prints all info in one go. That will also allow you to separate printing those stats from calling the functions.

RDM
  • 4,986
  • 4
  • 34
  • 43
0

Get rid of all your println statements and use a printStats routine something like

void printStats() {
    System.out.println("A Standard fighting pokemon's stats are:\n" + 
        "weight: " + weight() + "\n" + ...
Neil Masson
  • 2,609
  • 1
  • 15
  • 23