Managing Newborns in a Hospital Nursery

  • Thread starter Thread starter fishturtle1
  • Start date Start date
  • Tags Tags
    Elements
AI Thread Summary
The discussion focuses on the implementation of a Nursery class that manages Newborn objects in a hospital nursery setting. The initial issue arose from using static variables in the Newborn class, which caused all instances to share the same name and length, leading to incorrect behavior when adding new Newborns. By changing the instance variables to non-static, each Newborn object retains its unique properties, resolving the problem. The Nursery class was also corrected to properly initialize an ArrayList of Newborn objects. This adjustment allowed the add method to function correctly, ensuring that the nursery maintains a distinct list of Newborns.
fishturtle1
Messages
393
Reaction score
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.
Provide Javadoc for both classes.

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?
 
Physics news on Phys.org
Somehow, instead of using an <Newborn>ArrayList, I used a <String>ArrayList.

my new code looks like this

Code:
import java.util.ArrayList;

public class Nursery
{
    private static ArrayList<String> nurseList;

    public Nursery()
    {
        nurseList = new ArrayList<>();
    }

    /**
     * @param n the newborn object to be added to the end of the nurseList array.
     */
    public static void add(Newborn n)
    {
        nurseList.add(n.getName());
    }
...

and now it works. I'm really confused as to why this works when I use a string array list but not a Newborn array list.
 
I don't have a Java system installed, so I can't test what I think is going on. In your code for the Nursery class, there is only one data member -- an ArrayList whose underlying type is Newborn. You don't show where this Newborn class is declared.
Your constructor for a Nursery object should specify the underlying type for the ArrayList it allocates, something like this:
Java:
public Nursery()
    {
        nursery = new ArrayList<Newborn>();
    }
The net effect of this, I believe, is that when you first create an empty nursery object, your add() method doesn't work correctly, because your Nursery constructor isn't creating the right kind of ArrayList.
 
Mark44 said:
I don't have a Java system installed, so I can't test what I think is going on. In your code for the Nursery class, there is only one data member -- an ArrayList whose underlying type is Newborn. You don't show where this Newborn class is declared.
Your constructor for a Nursery object should specify the underlying type for the ArrayList it allocates, something like this:
Java:
public Nursery()
    {
        nursery = new ArrayList<Newborn>();
    }
The net effect of this, I believe, is that when you first create an empty nursery object, your add() method doesn't work correctly, because your Nursery constructor isn't creating the right kind of ArrayList.

Thank you for the response. I may have misinterpreted something you said;

Code:
public class Nursery
{
    private ArrayList<Newborn> nurseList;

    public Nursery()
    {
        nurseList = new ArrayList<Newborn>();
    }

    public void add(Newborn n)
    {       
        nurseList.add(n);
    }
...

Here is also my Newborn class, sorry I forgot to include it.

Code:
/**
 * construct a newborn object and have various methods to get and change
 * variables in the constructor
 */
public class Newborn
{
    private static String name;
    private static double length;
   
    /**
     * @param n is the name of the baby
     * @param l is the length of the baby
     */
    public Newborn(String n, double l)
    {
        name = n;
        length = l;
    }
   
    /**
     * @return the name of the baby
     */
    public String getName()
    {
        return name;
    }
   
    /**
     * Let's you set a new name for the baby
     * @param aName the new name of the baby
     */
    public void setName(String aName)
    {
        name = aName;
    }
   
    /**
     * @return the length of the baby
     */
    public double getLength()
    {
        return length;
    }
   
    /**
     * Let's you change the length of the baby
     * @param aLength new length of the baby
     */
    public void setLength(double aLength)
    {
        length = aLength;
    }
       
}

The tester still returns the same array of [Sarah, Sarah, Sarah, Sarah, Sarah] and similar to the other attempts to add.

but when I change the instance variables in my Newborn class to this

Code:
public class Newborn
{
    private String name;
    private double length;

and put the Nursery class as you suggested, the tester passes every test. I'm not sure why the them being static variables made it output what it did.Edit: just looked at the lecture from few days ago, it said that creating static variables is almost never a good idea.. and I guess that's what I did.
 
Last edited:
First version:
Java:
public class Newborn
{
    private static String name;
    private static double length;
    ...

New version:
Java:
public class Newborn
{
    private String name;
    private double length;
    ...
Making the name and length members of the Newborn class static means that all instances of the Newborn class share the same string (and length). When you create new Newborn instances with your earlier version, the new instances overwrite the name and length members.
 
Mark44 said:
First version:
Java:
public class Newborn
{
    private static String name;
    private static double length;
    ...

New version:
Java:
public class Newborn
{
    private String name;
    private double length;
    ...
Making the name and length members of the Newborn class static means that all instances of the Newborn class share the same string (and length). When you create new Newborn instances with your earlier version, the new instances overwrite the name and length members.
Thank you, that makes it clear what was happening.
 
I didn't explicitly say it, and I think you get it, but it won't hurt to add this -- by removing the "static" storage class from the name and length members, each Newborn instance now has its own unshared name and length data member.
 

Similar threads

Replies
7
Views
3K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Back
Top