我有一个将动物添加到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));
}
}
最佳答案
构造函数的参数必须顺序相同
所以您将其定义为
public Flying(String inSpecies, double inMass, int inNumWings)
所以它需要被称为
Flying temp = new Flying(species, mass, numWings);