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
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Classes and Array of objects

    How do i create an array (of size 5) in the AssetMain class for each of the classes created.
    The classes I created are House,flat,farm etc.
    My problem now is implementing arrays of objects for the different classes in the AssetMain class.

    In the AssetManager class, create methods to add ( remember to keep the sizes of the arrays in mind), sell assets (This method simply sets the status of an asset at a given index of an array to sold) and print the contents of each of the objects for each of the arrays.



    import java.util.Scanner;
    public class AssetMain {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
     
            Scanner in=new Scanner(System.in);
     
            House[] eros=new House[2];//Creating object eros of class House with array size of 5
            House[] hPark=new House[2];//Creating object hpark of class House with array size 5
     
            for(int k=0;k<eros.length;k++)
            {
                eros[k]=new House();
                hPark[k]=new House();
     
                            add();//method to add properties to the object eros and hPark
                            print();method to print properties to the object eros and hPark
     
            }
    public static void add()//method to add properties to an array of a object
        {
          Scanner in=new Scanner(System.in);
            House[] detailsArray = new House[5];  
     
            for(int j=0;j<detailsArray.length;j++) {
                detailsArray[j] = new House();
     
                System.out.println("Enter area");
                        detailsArray[j].setArea(in.next());
     
                            System.out.println("Enter hectare");
                                    detailsArray[j].setHectare(in.nextDouble());
                                            System.out.println("Enter current value of asset");
                                                    detailsArray[j].setCurentValue(in.nextDouble());
     
                                                            System.out.println("Enter original cost of asset"); 
                                                                    detailsArray[j].setOriginalCost(in.nextDouble());
     
                                                                          System.out.println("Is asset sold");
                                                                                detailsArray[j].setAssetSold(in.next());
     
     
            }
        }
     
         public static void print() //method to add properties to an array of a object
                {
                    House[] asset=new House[2];
                    for(int k=0;k<asset.length;k++)
                    {
                        asset[k]=new House();
                        System.out.println(asset[k]+" "+asset[k].getHectare()+" "+asset[k].getCurentValue()+" "+asset[k].getOriginalCost()+" "+asset[k].getAssetSold());
                    }
                }
           }
    }
     
    }
    Last edited by dynasty; March 26th, 2013 at 07:50 AM. Reason: wraping code with code tags


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    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.

  4. #4
    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!

  5. #5
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    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.

  7. #7
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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?

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    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, 07:17 PM
  2. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 10: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