Managing Newborns in a Hospital Nursery

  • Thread starter Thread starter fishturtle1
  • Start date Start date
  • Tags Tags
    Elements
Click For Summary

Discussion Overview

The discussion revolves around a programming homework assignment focused on implementing a class structure for managing a hospital nursery filled with newborn babies. Participants are tasked with creating a Nursery class that utilizes an ArrayList to manage Newborn objects, including methods for adding, removing, and checking for newborns.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes the requirements for the Nursery and Newborn classes, including methods for adding and removing newborns and retrieving their names.
  • A participant reports an issue where the add method appears to replace previously stored objects in the nursery instance variable.
  • Another participant suggests that the problem may stem from using an ArrayList of the wrong type, specifically a String ArrayList instead of a Newborn ArrayList.
  • A participant proposes a solution by changing the ArrayList to store Newborn objects and modifying the add method accordingly.
  • Concerns are raised about the declaration of the Newborn class and the static nature of its instance variables, which may lead to unexpected behavior.
  • One participant shares a corrected version of the Newborn class, changing instance variables from static to non-static, which may resolve the issues experienced with object management.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the issues with the add method and the proper implementation of the Newborn class. There is no consensus on the exact solution, as multiple approaches are discussed and refined.

Contextual Notes

There are unresolved questions regarding the proper use of static versus non-static variables in the Newborn class, which may affect the behavior of the objects being managed. Additionally, the proper initialization of the ArrayList in the Nursery class is a point of contention.

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 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K