I am struggiling to correctly put together these two constructors, one adds an instance of an array class and the other searches for an int passed-in.

public void Add(Appointment a)
{
    Scanner s=null;

    for (int i=0; i<this.m_Appointment.length; i++)
    {
    m_Appointment[i]=a;
    a.Read(s);
    m_NumElements++;
    }
}

public Appointment Find(int appointmentId)
{

    for (int i=0; i<this.m_Appointment.length; i++)
    {
       //if (Appointment.GetAppointmentId() == m_Appointment[i]) 
         {
            //return appointmentId;
         }
    }
     return null; //if not found, returns null (though I'll println a message saying that wasn't found)
}

Recommended Answers

All 6 Replies

Can you explain what your problem is and ask some specific questions about it?

Can you add some comments to the posted code that describe the steps that they are taking to solve the problem. For example what is the for loop at line 5 supposed to do?

I am struggiling to correctly put together these two constructors, one *adds an instance of an array class and the other *searches for an int that is passed-in.

public void Add(Appointment a) //constructor takes in an array instance to add it to 
                               //array m_Appointment
{
    Scanner s=null;
    for (int i=0; i<this.m_Appointment.length; i++) //for loop to add the instance 
    {                                               //of the array (incomplete/not 
                                                    //working)
    a.Read(s);  //calling functin that reads array instance parameters from a file
    m_Appointment[i]=a;  //supposed to add an array instance to the array 
                         //m_Appointment
    m_NumElements++;
    }
}
public Appointment Find(int appointmentId) //constructor receives an int and 
{                                          //saerches for it in the array

      for (int i=0; i<this.m_Appointment.length; i++)     //forloop to search array      
      {                                                   //(incomplete/not working)
         if (Appointment.GetAppointmentId() == m_Appointment[i]) 
         {
         return appointmentId;  //it's suppose to return that ID if it finds it in  
                                //the array indexes
         }
    }
         return null; //if not found, returns null (though I'll println a message 
                      //saying that wasn't found)
}

What you have posted is code for methods not constructors. Constructors have the same name as the class they are "constructing" and do not have a return value.

What are the steps the add() method should take to add an Appointment object to the m_Appointment array? Where in the array is the Appointment object to be added? How does it find that place to add it? WHen should the Appointment object be created with the contents from the file?

What is the find() method supposed to return? The posted code returns the value of the appointmentId that was passed to it? How should the code detect the Appointment object that it is supposed to search for?
BTW null (line 25) is not a valid int value and the posted code shouldn't compile as coded.

Precisely, I can't put together correctly.

Void Add(Appointment a) Adds an Appointment instance to the collection.
Appointment Find(int appointmentId) Returns the Appointment with the given appointment id and null if not found in the collection.

You need to design (make a list of the steps) what the methods are going to do before writing the code:

What are the steps the add() method should take to add an Appointment object to the m_Appointment array?

What are the steps the find() method should take to find an Appointment object in the m_Appointment array? What will it return?

Make a list of the steps each of these methods needs to do. When the logic is worked out, then try to code them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.