- #1
fishturtle1
- 394
- 82
Homework Statement
Now we are going to use the design pattern for collecting objects. We are going to model a hospital nursery filled with newborn babies. A Nursery uses an ArrayList to keep track of Newborn objects. You will write both a Nursery class and a Newborn class.
A Newborn has a name and a length. Provide a constructor that takes name and length, in that order. Provide getters and setters for the instance variables. This is the design pattern for managing properties of objects.
A Nursery has a constructor that takes no parameters. Remember it must initialize the instance variable
It has methods
- add() Adds the specified Newborn to the Nursery
- remove() Removes the first Newborn in the list with the specified name.
- contains() determines if a Newborn with a given name is in the Nursery. Returns true if a Newborn with that name is in the Nursery. Otherwise false.
- nameList() gets an ArrayList names of the Newborn in the Nursery.
Homework Equations
We went over static methods and variables and constants before this lesson.
The Attempt at a Solution
Code:
import java.util.ArrayList;
public class Nursery
{
private ArrayList<Newborn> nursery;
public Nursery()
{
nursery = new ArrayList<>();
}
/**
* @param n the newborn object to be added to the end of the nursery array.
*/
public void add(Newborn n)
{
nursery.add(n);
}
public void remove(String target)
{
boolean found = false;
int i = 0;
while(!found && i < nursery.size() - 1)
{
if(nursery.get(i).getName().equals(target))
{
nursery.remove(nursery.get(i));
found = true;
}
else
{
i++;
}
}
}
public boolean contains(String target)
{
for(Newborn n : nursery)
{
if(n.getName().equals(target))
{
return true;
}
}
return false;
}
public ArrayList<String> nameList()
{
ArrayList<String> storage = new ArrayList<>();
for(Newborn n : nursery)//for each newborn in nursery
{
storage.add(n.getName());//add their name to storage...it only adds the first name each iteration
}
return storage;//return storage
}
}
My problem so far is that in my add method, whenever I add a Newborn object, it replaces any previously stored objects in the nursery instance variable with whatever I'm trying to add. Here is the tester with the results to show what I'm talking about;
Code:
import java.util.ArrayList;
/**
* Tester for Nursery
*/
public class NurseryTesterFinal
{
public static void main(String[] args)
{
Nursery nursery = new Nursery();
nursery.add(new Newborn("Lin", 17.4));
nursery.add(new Newborn("Taran", 21.75));
nursery.add(new Newborn("Sarah", 19));
nursery.add(new Newborn("Miguel", 18.1));
nursery.add(new Newborn("Sarah", 20.2));
System.out.println(nursery.nameList());
System.out.println("Expected: [Lin, Taran, Sarah, Miguel, Sarah]");
nursery.remove("Sarah");
System.out.println(nursery.nameList());
System.out.println("Expected: [Lin, Taran, Miguel, Sarah]");
nursery.remove("Lin");
System.out.println(nursery.nameList());
System.out.println("Expected: [Taran, Miguel, Sarah]");
nursery.add(new Newborn("Aruna", 17.80));
System.out.println(nursery.contains("Sarah"));
System.out.println("Expected: true");
System.out.println(nursery.contains("Kellan"));
System.out.println("Expected: false");
System.out.println(nursery.nameList());
System.out.println("Expected: [Taran, Miguel, Sarah, Aruna]");
}
}
Code:
[Sarah, Sarah, Sarah, Sarah, Sarah]
Expected: [Lin, Taran, Sarah, Miguel, Sarah]
[Sarah, Sarah, Sarah, Sarah]
Expected: [Lin, Taran, Miguel, Sarah]
[Sarah, Sarah, Sarah, Sarah]
Expected: [Taran, Miguel, Sarah]
false
Expected: true
false
Expected: false
[Aruna, Aruna, Aruna, Aruna, Aruna]
Expected: [Taran, Miguel, Sarah, Aruna]
Any advice on how to fix the add method?