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 8 of 8

Thread: Classes and Array of objects

  1. #1
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Classes and Array of objects

    How do i create an array (of size 5) in the AssetMain class for each of the classes created
    If the classes all derive from a common base class, you could create an array of that class:
    TheBaseClass[] assets new TheBaseClass[5];

    If there is no base class, the type of the array would be Object. However this is not recommended to mix different data types in an array.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Classes and Array of objects

    Ok, but my classes already exist as i have mentioned. I instantiated the array classes but now i need a method to add, and set a certain array index as well as print the properties of my classes.

    I have tried to accomplish this. However how do I use the same method add,sell and print for different classes? I have made an example of the house classs in the code.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Classes and Array of objects

    If your super class has the functions then each subclass can use them. For example, if your house class has a sell function then you could do "flat.sell()". It inherits all of the functions and variables from the parent.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Classes and Array of objects

    Work on the methods one at a time.
    What is the add() method supposed to do?


    This line of code makes it look like you are copying and pasting lines without thinking:
     public static void print() //method to add properties to an array of a object
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Classes and Array of objects

    the add() is suppose to add the location of the house,the current value,original cost,etc.
    Like in the code. Apologies for my amateur mistakes Norm.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Classes and Array of objects

    Is the add() method supposed to gather the information needed to create a House object and then create an object?
    When it has a House object, what is it supposed to do with that object?
    What class is the add() method supposed to be in?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Classes and Array of objects

    I managed to create all my methods. Thansks

Similar Threads

  1. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 08:17 PM
  2. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 11:35 AM
  3. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  4. Creating classes with generic objects
    By rbt in forum Collections and Generics
    Replies: 2
    Last Post: April 23rd, 2012, 08:08 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM