找不到合适的构造函数 - 飞行

时间:2016-10-28 05:18:27

标签: java arrays constructor count

我有一个程序可以将动物添加到Zoo数组中。动物分为不同类型,如飞行,陆地和水生。

我有一个子模块,它正在向数组添加一个飞行动物,但我得到了这个错误。

没有为Flying(int,double,string)找到合适的构造函数

这是创建数组的主要Zoo文件,负责将动物添加到数组中。

import java.util.*;
public class Zoo
{
//Private Classfields
private Animals[] animals;
int count;
public final int Maximum_Count = 20;

/********************************************************************
*Default Constructor:
*Import: None
*Export: Address of new Zoo Object
*Assertion: Creates a Default Zoo Object for count and animals array.
********************************************************************/

public Zoo()
{
    count = 0;
    animals = new Animals[Maximum_Count];
}

/*****************************************************************
*Alternate Constructor:
*Import: inCount(Integer), inAnimals(Animals[])
*Export: Address of New Zoo Object
*Assertion: Creates alternate object if valid and fails otherwise.
*****************************************************************/

public Zoo(int inCount, Animals[] inAnimals)
{
    if(validateCount(inCount))
    {
        animals = new Animals[Maximum_Count];                   //Probably Wont have to validate Count
        for(int ii = 0; ii < Maximum_Count; ii++)
        {
            inAnimals[ii] = new Animals(inAnimals[ii]);
        }
    }
}

/*********************************************************************************
*Copy Constructor:
*Import: inZoo(Zoo)
*Export: Address of New Zoo Object
*Assertion: Creates a new Zoo Object with an identical object state as the import.
*********************************************************************************/

public Zoo(Zoo inZoo)
{
    count = inZoo.getCount();
    animals = inZoo.getAnimals();
}

//Mutators

/*********************************
*Submodule: setCount
*Import: inCount(Integer)
*Export: None
*Assertion: Sets count to inCount.
*********************************/

public void setCount(int inCount)
{
    if(validateCount(inCount))
    {
        count = inCount;
    }
    else
    {
        throw new IllegalArgumentException("Invalid Count");
    }
}  

/*************************************
*Submodule: setAnimals
*Import: inAnimals(Animals[])
*Export: None
*Assertion: Sets animals to inAnimals.
*************************************/

public void setAnimals(Animals[] inAnimals)
{
    if(inAnimals == null)
    {
        throw new IllegalArgumentException("Animals can not be found, array is empty.");
    }
    else
    {
        animals = new Animals[inAnimals.length];
        for(int ii = 0; ii < inAnimals.length; ii++)
            {
                animals[ii] = new Animals(inAnimals[ii]);
            }
    }
}

public int getCount()
{
    return count;
}

public Animals[] getAnimals()
{
    Animals[] animalsCopy;
    animalsCopy = new Animals[animals.length];
    for(int ii = 0; ii < animals.length; ii++)
    {
         animalsCopy[ii] = new Animals(animals[ii]);
    }
    return animalsCopy;
}

/****************************************************************************
*Submodule: equals
*Import: inObject(Object)
*Export: same(boolean)
*Assertion: Checks if two Zoo objects are equal according to array and count.
****************************************************************************/

public boolean equals(Object inObject)
{
    Zoo inZoo;
    boolean same = false;
    if(inObject instanceof Zoo)
    {
        inZoo = (Zoo)inObject;
        if(count == inZoo.getCount())
        {
            if(sameAs(animals,inZoo.getAnimals()))
            {
                same = true;
            }
        }
    }
    return same;
}

public String toString()
{
    String outString = "Count: " + count;
    for(int ii = 0; ii < animals.length; ii++)
    {
         outString = outString + ("Animals" + ii + ": " + animals[ii].toString());
    }
    return outString;
}

/**************************************************
*Submodule: addFlying
*Import: None
*Export: None
*Assertion: Adds a Flying Animal to the Zoo(Array).
**************************************************/

public void addFlying()
{
    Scanner sc = new Scanner(System.in);
    String species;
    int numWings;
    double mass;
    System.out.println("Please enter the Species of the Flying Animal.");
    species = sc.nextLine();
    System.out.println("Please enter the Mass of the Flying Animal.");
    mass = sc.nextDouble();
    System.out.println("Please enter the Number of Wings the Flying Animal has.");
    numWings = sc.nextInt();
    Flying temp = new Flying(numWings,mass,species);
    animals[count] = temp;
    count++;
}

/*******************************************************
*Submodule: addTerrestrial
*Import: None
*Export: None
*Assertion: Adds a Terrestrial Animal to the Zoo(Array).
*******************************************************/

public void addTerrestrial()
{
    Scanner sc = new Scanner(System.in);
    String species;
    int numLegs;
    double mass;
    System.out.println("Please enter the Species of the Terrestrial Animal.");
    species = sc.nextLine();
    System.out.println("Please enter the Mass of the Terrestrial Animal.");
    mass = sc.nextDouble();
    System.out.println("Please enter the Number of Legs that the Terrestrial Animal has.");
    numLegs = sc.nextInt();
    Terrestrial temp = new Terrestrial(species,numLegs,mass);
    animals[count] = temp;
    count++;
}

/****************************************************
*Submodule: addAquatic
*Import: None
*Export: None
*Assertion: Adds an Aquatic Animal to the Zoo(Array). 
****************************************************/

public void addAquatic()
{
    Scanner sc = new Scanner(System.in);
    String species;
    int numFins;
    double mass;
    System.out.println("Please enter the Species of the Aquatic Animal.");
    species = sc.nextLine();
    System.out.println("Please enter the Mass of the Aquatic Animal.");
    mass = sc.nextDouble();
    System.out.println("Please enter the Number of Fins that the Aquatic Animal has.");
    numFins = sc.nextInt();
    Aquatic temp = new Aquatic(species,numFins,mass);
    animals[count] = temp;
    count++;
}

/*********************************************************************************
*Submodule: displayAnimals
*Import: None
*Export: None
*Assertion: Converts Flying,Terrestrial and Aquatic to a String and Prints it out.
*********************************************************************************/

public void displayAnimals()
{
    for(int i=0; i<count; i++)
    {
        if(animals[i] instanceof Flying)
        {
            Flying test1 = new Flying((Flying)animals[i]);
            System.out.println(test1.toString());
        }
        if(animals[i] instanceof Terrestrial)
        {
            Terrestrial test2 = new Terrestrial((Terrestrial)animals[i]);
            System.out.println(test2.toString());
        }
        if(animals[i] instanceof Aquatic)
        {
            Aquatic test3 = new Aquatic((Aquatic)animals[i]);
            System.out.println(test3.toString());
        }
    }
}

//Private Submodules

/*************************************************
*Submodule: sameAs
*Import: array1(Object[]), array2(Object[])
*Export: sameAs
*Assertion: Checks if the two arrays are the same.
*************************************************/

private boolean sameAs(Object[] array1, Object[] array2)
{
    boolean sameAs = true;
    if(array1.length != array2.length)
    {
        sameAs = false;
    }
    else
    {
        int count = 0;
        do
        {
            sameAs = array1[count].equals(array2[count]);
            count++;
        }
        while(sameAs && (count < array1.length));
    }
    return sameAs;
}

/***********************************************************************
*Submodule: validateCount
*Import: inCount(Integer)
*Export: valid(boolean)
*Assertion: inCount must be greater than 0 and less than or equal to 20.
***********************************************************************/

private boolean validateCount(int inCount)
{
    return((inCount > 0) && (inCount <= 20));
}
}

这是飞行文件,它说找不到合适的构造函数。

import java.util.*;
public class Flying extends Animals
{
//Class Constants.
public static final String type = "Flying";

//Private Classfields
private int numWings;

/*****************************************
*Default Constructor:
*Import: None
*Export: Address of new Flying Object
*Assertion: Creates default Flying Object.
*****************************************/

public Flying()
{
    super();
    int numWings = 0;
}

/**************************************************************************
*Alternate Constructor:
*Import: inMass(Real), inSpecies(String), inNumWings(Integer)
*Export: Address of new Flying Object
*Assertion: Creates the alternate constructor if valid and fails otherwise.
**************************************************************************/

public Flying(String inSpecies, double inMass, int inNumWings)
{
    super(inSpecies, inMass);
    if(validateWings(inNumWings))
    {
        numWings = inNumWings;
    }
    else
    {
        throw new IllegalArgumentException("Invalid Number of Wings");
    }
}

/**************************************************************************
*Copy Constructor:
*Import: inFlying(Flying)
*Export: Address of new Flying Object
*Assertion: Creates an object with an identical object state as the import.
**************************************************************************/

public Flying(Flying inFlying)
{
    super(inFlying);
    numWings = inFlying.getNumWings();
}

//Mutators

/************************************************************************
*Submodule: setNumWings
*Import: inNumWings(Integer)
*Export: None
*Assertion: Sets the numWings to inNumWings if valid and fails otherwise.
************************************************************************/

public void setNumWings(int inNumWings)
{
    if(validateNumWings(inNumWings))
    {
        numWings = inNumWings;
    }
    else
    {
        throw new IllegalArgumentException("Invalid Number of Wings");
    }
}

//Accessors

public int getNumWings()
{
    return numWings;
}

/******************************************************************************
*Submodule: equals
*Import: inObject(Object)
*Export: same
*Assertion: Two flying objects are equal if they have the same number of wings.
******************************************************************************/

public boolean equals(Object inObject)
{
    boolean same = false;
    if(inObject instanceof Flying)
    {
        Flying inFlying = (Flying)inObject;
        if(numWings == inFlying.getNumWings())
        {
            same = true;
        }
    }
    return same;
}

public String toString()
{
    return(type + super.toString() + "Num Wings is: " + numWings);
}

//Private Submodules

/***********************************************************************
*Submodule: validateNumWings
*Import: inNumWings(Integer)
*Export: valid(boolean)
*Assertion: Number of Wings must be even to be valid and greater than 0.
***********************************************************************/

private boolean validateNumWings(int inNumWings)
{
    return((inNumWings % 2 == 0) && (inNumWings > 0));
}
}

2 个答案:

答案 0 :(得分:1)

构造函数的参数需要采用相同的顺序

因此您已将其定义为

public Flying(String inSpecies, double inMass, int inNumWings)

因此需要将其称为

Flying temp = new Flying(species, mass, numWings);

答案 1 :(得分:0)

构造函数中的参数顺序错误。

错误消息明确指出“ Flying(int,double,string)cons not found

这意味着您尚未定义接受“int,double,string”作为参数

的构造函数
相关问题