You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The last unit test, "Checking that code contains petList.add(new Cat())" fails unless a call to the constructor of Cat is passed null.
Specifically,
import java.util.; // for ArrayList
public class Pet
{
private String name;
private String type;
public Pet(String n, String t)
{
name = n;
type = t;
}
public String toString()
{
return name + " is a " + type;
}
public static void main(String[] args)
{
ArrayList<Pet> petList = new ArrayList<Pet>();
petList.add(new Pet("Sammy","hamster"));
petList.add(new Dog("Fido"));
petList.add(new Cat("Fluffy"));
petList.add(new Cat("?"));
petList.add(new Cat(null)); // passes with this statement; fails otherwise
// This loop will work for all subclasses of Pet
for(Pet p : petList)
{
System.out.println(p);
}
}
}
class Dog extends Pet
{
public Dog(String n)
{
super(n, "dog");
}
}
class Cat extends Pet
{
public Cat(String n)
{
super(n, "cat");
}
}
The text was updated successfully, but these errors were encountered:
The last unit test, "Checking that code contains petList.add(new Cat())" fails unless a call to the constructor of Cat is passed null.
Specifically,
import java.util.; // for ArrayList
public class Pet
{
private String name;
private String type;
}
class Dog extends Pet
{
public Dog(String n)
{
super(n, "dog");
}
}
class Cat extends Pet
{
public Cat(String n)
{
super(n, "cat");
}
}
The text was updated successfully, but these errors were encountered: