Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Beginner question on where to put an array

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Beginner question on where to put an array

    Hi there,

    I've got a bit of free time at the minute and am teaching myself to program. Making steady progress but have trouble with a practice project I'm trying to do. Its a demo hospital program using a JFrame where you can add patients to an array and print out a list of patients already in the array. The classes I have are Patient (get name, get address etc), Window (the main window where you can print patients from array) and AddPatientWindow (new window that opens when an Add Patient button is pressed on the main Window.)

    My question is, where do I 'put' the Patient Array so that it is available and the same throughout each class? Do I make a new class that extends Arrays? At the minute it is in the Window class but disappears in the AddPatientWindow.

    Also, please let me know if the set up/amount of classes is completely wrong or if there are any other major faults.

    Thanks in advance for any answers.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Beginner question on where to put an array

    A typical design would be to create a new class that defines your data model - I would not extend the Arrays class (in fact, don't even see how this would be beneficial as all the Arrays class contains are static methods), rather write your own to encapsulate your data within this class. In this way, a change to the model is reflected in all references to said model

  3. The Following User Says Thank You to copeg For This Useful Post:

    Diplo (June 3rd, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Beginner question on where to put an array

    Thanks for the reply copeg. Can you elaborate on your answer though? I've read it a few times but still quite sure what you mean.

    What kind of information would this data model have in it?

    Thanks again.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Beginner question on where to put an array

    What kind of information would this data model have in it?
    Whatever information you wish. Java is object oriented, so create classes which make sense. For instance you wish to model a Hospital...so create a class Hospital. This class could contain a List/array of patients which can be accessed using methods you define (like addPatient, getPatient, etc...).
    public class Hospital{
        //define your patients inside this class
     
        ///getter, setter, and add methods can be defined here
    }

    This makes sense from both an object oriented standpoint and from an encapsulation standpoint (encapsulation meaning you 'hide' how Patients from stored from other classes, meaning you could use an array, a List, a Map, and change these as you see fit without breaking your whole project).

  6. The Following User Says Thank You to copeg For This Useful Post:

    Diplo (June 3rd, 2011)

  7. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Beginner question on where to put an array

    OK I think things are a bit clearer now. So instead of having Patients as seperate class I should have it as an inner class? And declare the patient array inside the hospital class with

    public static Patient[] patientArray;

    A follow up question was going to be where the main method should be but it makes sense to put it in the hospital class. Is there any convention as to whether it should be at the start or end of the class?

    Thanks again for your help.

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Beginner question on where to put an array

    Not necessarily an inner class by definition, and not necessarily public or static either (static means that if you make more hospitals, the patients will be shared - not what you want - and public defeats the purpose of trying to encapsulate things). I'd suggest that it should still be a separate class, Hospital just contains instances/Objects of that class. Don't think too hard about it...start laying out some code and think about what things represent - should they be a class of their own

    See the following link for code conventions in java: Code Conventions for the Java Programming Language which may address your questions of where to place things.

  9. The Following User Says Thank You to copeg For This Useful Post:

    Diplo (June 5th, 2011)

Similar Threads

  1. Beginner Question Here (Trying to make things easier)
    By beer-in-box in forum AWT / Java Swing
    Replies: 2
    Last Post: June 1st, 2011, 10:48 AM
  2. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 11:56 AM
  3. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 08:57 PM
  4. Quick, beginner-level Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2011, 12:06 PM
  5. 2D Array Question
    By gmorris1986 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2010, 11:40 AM