Reputation: 1
So I've created a three classes; PetStore, Pet, and Bird. The PetStore class is the main class, pet extends PetStore and then Bird extends off of Pet
And now I've created a driver class with the following code.
public class Main{
public static void main(String[] args){
//create a pet objects
Bird macaw = new Bird("hyacinth macaw", 300.99, 48.0);
//create a pet store
PetStore happyPetsInc = new PetStore();
//add the pets to the pet store
happyPetsInc.addPet(macaw);
I'm trying to add the bird object to the arraylist in PetStore.
I'm getting the error: "incompatible types: Bird cannont be converted to java.lang.String"
someone flagged this and said to post the PetStore and Bird Class so here that is:
public class PetStore
{
//varibles
String pet;
//ArrayList
ArrayList<String> Pets = new ArrayList<String>();
//constructor
public PetStore(){
}
/**
* add the paramameter to the ArrayList
* @param pet
* @return void
*/
public void addPet(String pet){
Pets.add(pet);
}
/**
* removes the paramater to the ArrayList
* @param pet
* @return true(success) false(failure)
*/
public boolean sellPet(String pet){
this.pet = pet;
if (Pets.contains(pet)){
Pets.remove(pet);
return true;
}else{
return false;
}
}
/**
* counts the number of elements in the ArrayList
* @param none
* @return int, the number of the elements in Pets
*/
public int getInventoryCount(){
return Pets.size();
}
/**
* displays information about the pets in the ArrayList
* @param none
* @return void
*/
public String toString(){
for (int i = 0; i < Pets.size(); i++){
System.out.println("Pet #" + (i + 1));
String index = Pets.get(i);
return index.toString();
}
return "\n";
}
}
public class Bird extends Pet
{
//varibale
double wingspan;
//constuctor
public Bird(String species, double cost, double wingspan){
super(species, cost);
this.wingspan = wingspan;
}
/**
* Sets the wingspan of the bird
* @param wingspan
* @return none
*/
public void setWingspan(double wingspan){
this.wingspan = wingspan;
}
/**
* Gets the wingspan of the bird
* @param none
* @return double
*/
public double getWingspan(){
return this.wingspan;
}
/**
* Displays strings describing the bird
* @param none
* @return String
*/
public String toString(){
return "Species: " + species + "\nCost: $" + cost + "\nBird (Wingspan: " + wingspan + " inches)";
}
}
Upvotes: 0
Views: 236
Reputation: 21
you should define a Pet class, then in PetStore change
String pet;
to
Pet pet;
and also
ArrayList<String> Pets = new ArrayList<String>();
to
ArrayList<Pet> Pets = new ArrayList<Pet>();
Upvotes: 0
Reputation: 23655
The idea of object oriented programming is, to have classes and instances of classes (objects) to represent real life objects. Hence, a PetStore is about pets, not strings. So do this:
In PetStore replace
ArrayList<String> Pets ...
addPet(String pet)...
sellPet(String pet)...
with
ArrayList<Pet> Pets
addPet(Pet pet)
sellPet(Pet pet)
Also, in PetStore.toString() replace
String index = Pets.get(i);
With
Pet index = Pets.get(i);
Upvotes: 0