2016-10-28 59 views
0

我有一个程序可以将动物添加到动物园数组中。动物分为不同类型,例如飞行,陆地和水生。找不到合适的构造函数 - 用于飞行

我有一个子模块是添加一个飞行动物到数组但是我得到这个错误。

发现飞(INT,双,字符串)

这里没有合适的构造函数是创建数组,并负责将动物到阵列的主动物园文件。

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)); 
} 
} 

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

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)); 
} 
} 
+0

你的问题是什么? – jacefarm

回答

1

构造函数的参数必须以相同的顺序

,所以你有它定义为

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

所以它需要被称为

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

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

错误信息中明确指出“飞(INT,双,字符串)缺点没有发现

这意味着你没有定义接受“诠释,双,字符串”作为参数的构造函数

相关问题