0

Fixed all other issues. Currently trying to deduct 10% from currentPowerLevel in the function fightCrime. However, without a local variable I have to deal with a users inputted number and deduct 10% from it. Problem being that normally I would have a local variable with a set number that I could simply revisit and subtract the number multiplied by .1.

package superherodriver;

/**
 * 
 * @author Daniel
 */
public class SuperHero 
{
    private String name;
    private String superPower;
    private boolean canFly;
    private int crimesStopped;
    private final double MAX_POWER_LEVEL;
    private double currentPowerLevel;

    public SuperHero()
    {
        MAX_POWER_LEVEL = 100;
        name = "";
        superPower = "";
        canFly = false;
        crimesStopped = 0;
        currentPowerLevel = 0;

    }

    //Define non-default constructor
    public SuperHero(String aName, String aSuperPower, double aMaxPowerLevel, boolean aCanFly)
    {
        MAX_POWER_LEVEL = aMaxPowerLevel;
        this.name = aName;
        this.superPower = aSuperPower;
        this.canFly = aCanFly;
        this.crimesStopped = 0;
        this.currentPowerLevel = MAX_POWER_LEVEL;
    }

    //Define getters and setters

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSuperPower() {
        return superPower;
    }

    public void setSuperPower(String superPower) {
        this.superPower = superPower;
    }

    public boolean isCanFly() {
        return canFly;
    }

    public void setCanFly(boolean canFly) {
        this.canFly = canFly;
    }

    public int getCrimesStopped() {
        return crimesStopped;
    }

    public void setCrimesStopped(int crimesStopped) {
        this.crimesStopped = crimesStopped;
    }

    public double getCurrentPowerLevel() {
        return currentPowerLevel;
    }

    public void setCurrentPowerLevel(double currentPowerLevel) {
        this.currentPowerLevel = currentPowerLevel;
    }


}

package superherodriver;
import java.util.Scanner;
import java.util.Random;

/**
 * 
 * @author Daniel
 */

public class SuperHeroDriver {

static Scanner kb = new Scanner(System.in);
public static SuperHero superman;
public static SuperHero wonderwomen;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String name, superPower, answer;
        double powerLevel;
        boolean fly;


        System.out.println("What is the name of your favorite super hero?");
        name = kb.nextLine();
        System.out.println("What is your super hero's power?");
        superPower = kb.nextLine();
        System.out.println("Can your super hero fly? (yes or no)");

        while(true){
            answer = kb.nextLine();
            if(answer.equalsIgnoreCase("yes")){
                fly = true;
                break;
            } else if(answer.equalsIgnoreCase("no")){
                fly = false;
                break;
            } else {
                System.out.println("Sorry, you did not enter an approved response. Please answer yes or no.");
            }
        }
        do{
            System.out.println("What is your starting power level? Choose between (0-100)");
            powerLevel = kb.nextDouble();
        } while(powerLevel < 0 || powerLevel > 100);

        kb.nextLine();

        // Non-default contructor
        superman = new SuperHero(name, superPower, powerLevel, fly);
        // Default constructor
        wonderwomen = new SuperHero();

        System.out.println("What is the name of your favorite super hero?");
        wonderwomen.setName(kb.nextLine());
        System.out.println("What is your super hero's power?");
        wonderwomen.setSuperPower(kb.nextLine());
        System.out.println("Can your super hero fly? (yes or no)");

        while(true){
            answer = kb.nextLine();
            if(answer.equalsIgnoreCase("yes")){
                wonderwomen.setCanFly(fly = true);
                break;
            } else if(answer.equalsIgnoreCase("no")){
                wonderwomen.setCanFly(fly = false);
                break;
            } else {
                System.out.println("Sorry, you did not enter an approved response. Please answer yes or no.");
            }
        }

        do{
            System.out.println("What is your starting power level? Choose between (0-100)");
            wonderwomen.setCurrentPowerLevel(kb.nextDouble());
        } while(wonderwomen.getCurrentPowerLevel() < 0 || wonderwomen.getCurrentPowerLevel() > 100);

        do{
            fightCrime(superman, wonderwomen);
            System.out.println("Superman's current power level is " + superman.getCurrentPowerLevel() + " and stopped " + superman.getCrimesStopped() + " crimes.");
            System.out.println("Wonder woman's current power level is " + wonderwomen.getCurrentPowerLevel() + " and stopped " + wonderwomen.getCrimesStopped() + " crimes.");

        }while(superman.getCurrentPowerLevel() > 0 && superman.getCrimesStopped() < 10);

    }

    public static void fightCrime(SuperHero superman, SuperHero wonderwomen){
        Random randomNumber = new Random();
        int ranNum1, ranNum2;       
        ranNum1 = randomNumber.nextInt(11) + 1;
        ranNum2 = randomNumber.nextInt(11) + 1;
        // Stop crime if random number is Even, lose power if odd
        if(ranNum1 % 2 == 0){
            System.out.println("Crime was stopped!");
            superman.setCrimesStopped(superman.getCrimesStopped() + 1);
        }
        else{
            System.out.println("My powers are getting weaker!");
            superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);
        }

        if(ranNum2 % 2 == 0){
            System.out.println("Crime was stopped!");
            wonderwomen.setCrimesStopped(wonderwomen.getCrimesStopped() + 1);
        }
        else{
            System.out.println("My powers are getting weaker!");
            wonderwomen.setCurrentPowerLevel(wonderwomen.getCurrentPowerLevel() - 10);
        }

    }

}
Danny
  • 51
  • 8
  • 1
    Please read [How to create a **Minimal**, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Andreas Apr 01 '16 at 21:41
  • 2
    I didn't understand the problem really well, but from what I could understand you can just call `superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10)` – dambros Apr 01 '16 at 21:42
  • After seeing this comment I just realized since i'm calling the fight function an increment isn't need, it will continuously call the function and apply the -10 like you put. – Danny Apr 01 '16 at 21:48
  • Maybe this is the answer for your skipped next question http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – RubioRic Apr 01 '16 at 21:52

1 Answers1

1

How do I prevent a prompt from being skipped?

Because Scanner.nextDouble() doesn't consume the newline that is generated when the user hits Enter, you need to manually do that using Scanner.nextLine(), otherwise next time you try to read using that Scanner it will immediately return an empty result.

powerLevel = kb.nextDouble();
kb.nextLine();

How do I increment a getter?

By getting the value, incrementing it, and sending the new value to the setter.

superman.setCrimesStopped(superman.getCrimesStopped() + 10);
superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);        
Magnus W
  • 11,902
  • 10
  • 61
  • 135
  • Thank you, that was very useful. I had forgotten when a user hits enter it results in a new line. If i want to deduct a % of the current power level for how could I do that with a getter? Normally I would have a local variable store a set number multiplied by .1 and deduct that from the local variable. However, since it's a users input the number is unknown. – Danny Apr 01 '16 at 23:59
  • If I understand you correctly, you would just multiply the value you get from the getter, and send the result to the setter. If local variables makes it more clear then use them, otherwise you can just do `superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() * 0.90);`. This will multiply by `0.90`, i.e. deduct `10%` of the power. – Magnus W Apr 02 '16 at 07:55