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

Thread: Want to add an object to an array whilst inheriting another object?

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Want to add an object to an array whilst inheriting another object?

    I am new to Java and am using BlueJ.

    Overview of the context of the program.

    I am trying to create a Club Database (arraylist, not actual database) where the user can add a new climber (name, age, gender) and which mountain they've climbed (name, height) to the arraylist.

    I've created the Climber class and the mountain class. I've also created an ArrayList for the climbers and can add to this array. My question is.. How can I add a climber to the climber ArrayList, and be able to add which mountain they've climbed and its height at the same time?

    The method of adding a climber needs to access both the Climber and Mountain class?

    Whilst code solving the issue is appreciated, would be helpful if I was shown in the right direction so I can understand it more!

    Thanks.

     
    import java.util.ArrayList;
    import java.util.Scanner;
    /**
     * Write a description of class ClubStats here.
     *
     * @author (your name)
     * @version (a version number or a date)
     */
    public class ClubStats
    {
        // An ArrayList for storing climber details.
        private ArrayList<Climber> climbers;
        // An ArrayList for storing mountain details.
        private ArrayList<Mountain> mountains;
     
        /**
        * Constructor for objects of class ClubStats
        */
        public ClubStats()
       {
           // Initialise instance variables.
           climbers = new ArrayList<Climber>();
           mountains = new ArrayList<Mountain>();
        }
     
        public void addClimber(Climber newName)
        {
            climbers.add(newName);
        }
     
       public Climber getClimber(String name)
       {
           Climber foundClimber = null;
           int index = 0;
           boolean searching = true;
     
           while(searching && index < climbers.size()) {
               Climber climber = climbers.get(index);
               if(climber.getName().equals(name)) {
                       searching = false;
                       foundClimber = climber;
                    }
                    else {
                        System.out.println(name + " not found");
                        index++;
                    }
                }
                return foundClimber;
            }
     
       public void addMountain(Mountain newName)
       {
           mountains.add(newName);
       }
     
       public Mountain getMountain(String name)
       {
           Mountain foundMountain = null;
           int index = 0;
           boolean searching = true;
     
           while(searching && index < mountains.size()) {
               Mountain mountain = mountains.get(index);
               if(mountain.getName().equals(name)) {
                   searching = false;
                   foundMountain = mountain;
                }
                else {
                    System.out.println(name + " not found");
                    index++;
                }
            }
            return foundMountain;
        }
    }
     
    public class Climber
    {
        // Instance variables.
        // The climber name.
        private String name;
        // The climber age
        private int age;
        // The climber gender.
        private String gender;
     
     
        /**
         * Constructor for objects of class Climber
         */
        public Climber(String newName, int newAge, String newGender)
        {
            // Initialise instance variables.
            name = newName;
            age = newAge;
            gender = newGender;
     
        }
     
        /**
         * Accessor method for climber's name.
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * Set the climber's name.
         */
        public void setName(String newName)
        {
            name = newName;
        }
     
        /**
         * Accessor method for climber's age.
         */
        public int getAge()
        {
            return age;
     
        }
     
        /**
         * Set the climber's age.
         */
        public void setAge(int newAge)
        {
            age = newAge;
     
        }
     
         /**
         * Set the climer's gender.
         */
       public String getGender()
       {
           return gender;
       } 
     
       /**
         * Accessor method for climber's gender.
         */
        public void getGender(String newGender)
        {
            gender = newGender;
     
        }
     
     
    }
     
    public class Mountain
    {
        // Instance variables.
        private double height;
        private String name;
     
        /**
         * Constructor for objects of class Mountain
         */
        public Mountain(String mName, double mHeight)
        {
            // Initialise instance variables
            name = mName;
            height = mHeight;
        }
     
        /**
         * Accessor method for mountain name.
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * Set the mountain name.
         */
        public void setName(String newName)
        {
            name = newName;
        }
     
        /**
         * Accessor method for mountain height.
         */
        public double getHeight()
        {
            // put your code here
            return height;
        }
     
        /**
         * Set the mountain height.
         */
        public void setHeight(double newHeight)
        {
            height = newHeight;
        }
    }

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Want to add an object to an array whilst inheriting another object?

    You have a list of Climbers, and a list of Mountains, but so far no way to associate them with each other.

    If I'm not mistaken, this is a basic one-to-many relationship (one Climber, many Mountains). One basic but good way to represent that is with a HashMap. The key will be a Climber, and the value will be a List of Mountains:

    Map<Climber, List<Mountain>> climberMountainMap = new HashMap<Climber, List<Mountain>>();

    This map could replace your existing climbers list, because I think the map is expected to hold all possible climbers. But I think the mountains list should remain because it acts as the lookup list containing all possible mountains, while the map only associates one climber with those mountains he/she has climbed.

    To your question of how to add a climber and the mountain they climbed at the same time, something simple like this could work:

    public void addClimber(Climber climber, String mountainName, double mountainHeight) {
     
    	// Lookup this mountain in the list by name, see if we already know about it.
    	Mountain mountain = getMountain(mountainName);
     
    	if (mountain == null) {
    		// Haven't seen this mountain before, create one and add it to the lookup list.
    		mountain = new Mountain();
    		mountain.setName(mountainName);
    		mountain.setHeight(mountainHeight);
    		mountains.add(mountain);
    	}
     
    	// See if we already have a list of mountains for this climber.
    	List<Mountain> climberMountains = climberMountainMap.get(climber);
     
    	if (climberMountains == null) {
    		// First mountain for this climber. Create the mountain array list and associate it with the climber.
    		climberMountains = new ArrayList<Mountain>();
    		climberMountainMap.put(climber, climberMountains);
    	}
     
    	// Might need duplicate check:
    	climberMountains.add(mountain);
     
    }
    Hopefully that gives you some ideas.

Similar Threads

  1. Replies: 2
    Last Post: September 26th, 2013, 04:13 PM
  2. make object and add components
    By viper_pranish in forum AWT / Java Swing
    Replies: 3
    Last Post: July 23rd, 2013, 09:17 AM
  3. Add an object to a linked list and a database
    By a21j92 in forum JDBC & Databases
    Replies: 0
    Last Post: March 2nd, 2013, 10:09 AM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. SAX Parser will not add object to arrayList
    By michaelz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 1st, 2010, 02:59 PM