Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9.5.3. Superclass Arrays and ArrayLists Unit Test #23

Closed
DebMDuke opened this issue Aug 7, 2020 · 1 comment
Closed

9.5.3. Superclass Arrays and ArrayLists Unit Test #23

DebMDuke opened this issue Aug 7, 2020 · 1 comment

Comments

@DebMDuke
Copy link

DebMDuke commented Aug 7, 2020

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

@bhoffman0
Copy link
Owner

Thanks! I've fixed this and it will be live on 8/8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants