I have to write a fighter class to be used with my assignment program and it is returning errors that I cannot figure out

StackOverflow https://stackoverflow.com/questions/22568838

Question

import java.util.Scanner;

public class Assignment5 
{

public static void main(String[] arg)
{

         Fighter myFighter, enemyFighter;
         Scanner console = new Scanner(System.in);
         int num1, num2, num3;
         String str, another;
         System.out.println ("***  Fighter Game ***");

         do {
                 System.out.println("Create your fighter (Type three integers + name): ");
                 num1 = console.nextInt();   num2 = console.nextInt();   num3 = console.nextInt();
                 str = console.next();
                 if (num1 + num2 + num3 == 10) {
                        myFighter = new Fighter (num1, num2, num3, str);
                       enemyFighter = new Fighter( );
                       enemyFighter.setName ("Enemy");
                       System.out.print( myFighter.getName()+"    ["+myFighter.getPower()+","+myFighter.getSpeed()+","+myFighter.getHeal()+"] ");
                       System.out.print( enemyFighter.getName()+"    ["+enemyFighter.getPower()+","+enemyFighter.getSpeed()+","+enemyFighter.getHeal()+"] ");
                       System.out.println();

                     int fights = 0;
                     boolean gameOver= false;
                     while (fights < 10  &&  !gameOver){
                         System.out.print("Fight[" + fights + "]: ");
                         myFighter.attack (enemyFighter);
                         myFighter.heal ();
                         enemyFighter.attack(myFighter);
                         enemyFighter.heal();
                         myFighter.printInfo();
                         enemyFighter.printInfo();
                         if (enemyFighter.isDead() ||     myFighter.isDead()) gameOver = true;
                         fights ++;
                         System.out.println();
                     }
                     if(myFighter.getHealth() > enemyFighter.getHealth())     System.out.println(" You Win");
                     else System.out.println("You Lost");
                    System.out.println();
                    System.out.print("Play another fight (y/n)? ");
                    another = console.next();
                }
                else {
                    System.out.println("Invalid Inputs. The total of three     numbers should be 10.");
                    another = console.next();
                }
            } while (another.equalsIgnoreCase("y"));
    }
    }

Fighter.java class

    public class Fighter
    {
    private int power;
    private int speed;
    private int heal;
    private int health;
    private String name;

    public Fighter (int num1, int num2, int num3, String str)
    {

    }

    public String getName()
    {
        return name;
    }

    public int getPower()
    {
        return power;
    }

    public int getSpeed()
    {
        return speed;
    }

    public int getHeal()
    {
        return heal;
    }

    public int getHealth()
    {
        return health;
    }

    public void setName(String newName)
    {

    }

    public void setPower(int newPower)
    {

    }

    public void setSpeed(int newSpeed)
    {

    }
    public void setHeal(int newHeal)
    {

    }

    public void setHealth(int newHealth)
    {

    }

    public boolean isDead()
    {
        if(health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void heal()
    {

    }

    public void printInfo()
    {

    }

    public void attack()
    {

    }
    }

The assignment program is followed by the fighter class and i will post the error code for the assignment below

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor Fighter() is undefined
The method attack() in the type Fighter is not applicable for the arguments (Fighter)
The method attack() in the type Fighter is not applicable for the arguments (Fighter)

at Assignment5.main(Assignment5.java:27)

This is the error message that is displayed and I have tried everything to fix it and I'm not sure what else to try, I am not allowed to change the assignment program, only the fighter class so that it will allow the program to run the way it should

Was it helpful?

Solution

You are getting those issues because Fighter doesn't have the proper methods. For example: Fighter has a constructor defined as public Fighter (int num1, int num2, int num3, String str), but you are trying to instantiate a new one with enemyFighter = new Fighter( );. You will need to create another constructor (overload the existing one) that takes no parameters.

The same thing happens with the attack method, it's signature is public void attack() but you are calling it with some parameters: myFighter.attack (enemyFighter);. That means you'll need an overloaded attack method that receives one parameter of type Fighter: public void attack(Fighter fighter)

OTHER TIPS

either provide default constructor in your Fighter class

public Fighter() { /* default initialization */}

or use overloaded one to while creating instances with default values

enemyFighter = new Fighter(0,0,0,"default-name" );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top