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

Thread: Trouble adding an object.

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble adding an object.

    I made two classes for my "Backpack". You may ignore these if you wish.

    /**
     * A storage space for items
     *
     * @author Erik
     * @version 1
     */
    public class Canteen
    {
    private double maxVolume;
    private int percentFull;
    public Canteen()
    {
        percentFull=0;
        maxVolume=2;
    }
    public Canteen(double maxVolume, int percentFull)
    {
        this.percentFull=percentFull;
        this.maxVolume=maxVolume;
        if(percentFull>100){
        percentFull=100;}
    }
    public void fill()
    {
        percentFull=100;
    }
    public void empty()
    {
        percentFull=0;
    }
    public boolean sip()
    {
        boolean onesip=true;
        if(percentFull<1){
            onesip=false;
        }else{
            percentFull-=1;}
        return onesip;
    }
    public boolean sip(int numberOfSips)
    {
        boolean enoughwater=true;
        if(numberOfSips>percentFull){
            enoughwater=false;
        }else{
            percentFull-=numberOfSips;}
        return enoughwater;
    }
    public double getMaxVolume()
    {
    return maxVolume;
    }
    public double getPercentFull()
    {
    return percentFull;
    }
    }

    /**
     * Equipment.
     *
     * @author Erik
     * @version 1
     */
    public class Equipment
    {
    private String name;
    private int weight;
    public Equipment(String name, int weight)
    {
    this.name=name;
    this.weight=weight;
    }
    }

    And THIS is the class I'm having trouble with:

    /**
     * Storage for Equipment.
     *
     * @author Erik
     * @version 1
     */
    public class Backpack
    {
    private Canteen theCanteen;
    private Equipment[] theEquipment;
    Backpack()
    {
        this.theCanteen=null;
        this.theEquipment=new Equipment[0];
    }
    public void addCanteen(String Canteen)
    {
    }
    }

    In the addCanteen method, I want to add the Canteen into the backpack that is currently empty, but I'm not sure how I'd get around to doing that. Any ideas?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trouble adding an object.

    Please post your code properly indented, like the first bit of code above. Having all lines left justified makes your code hard to read.

    Adding a Canteen object to your backpack might be as simple as

    theCanteen = new Canteen(); // now theCanteen is no longer null

    Then you can tell if a canteen has been added by checking if theCanteen is null.

    I would consider being able to add more than one canteen to a backpack up to some maximum so that there is an array or ArrayList of Canteen in Backpack. Then, adding a canteen instance to BackPack would involve creating a new Canteen instance and adding it to BackPack's Canteen collection.

    But, if you only want each backpack to hold one canteen, the way you're going is fine.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble adding an object.

    I see...but how would I put it in the backpack from the method? This is what I have so far:


    public class Backpack
    {
    private Canteen theCanteen;
    private Equipment[] theEquipment;
    Backpack()
    {
        this.theCanteen=null;
        this.theEquipment=new Equipment[0];
    }
    public void addCanteen(String Canteen)
    {
        theCanteen = new Canteen();
    }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trouble adding an object.

    Call the addCanteen() method on an instancde of Backpack:
    Backpack backpack = new Backpack();
    backpack.addCanteen();  // I didn't include the String parameter, because I don't know why you've included it

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble adding an object.

    I see...I'm also trying to list equipment, add equipment, and remove equipment. The equipment class looks like this:

    public class Equipment
    {
    private String name;
    private int weight;
    public Equipment(String name, int weight)
    {
    this.name=name;
    this.weight=weight;
    }
    }

    And here's an example of the Equipment I could use:

    Equipment Crowbar = new Equipment("Crowbar", 5);
    Equipment GrapplingHook = new Equipment("Grappling Hook", 4);
    Equipment Hammer = new Equipment("Hammer", 2);
    Equipment Lantern = new Equipment("Bullseye Lantern", 3);
    Equipment Manacles = new Equipment("Manacles", 2);
    Equipment Pickaxe = new Equipment("Pickaxe", 20);
    Equipment Sledge = new Equipment("Sledge", 10);
    Equipment Tent = new Equipment("Tent", 20);
    Equipment Torch = new Equipment("Torch", 1);
    Equipment Waterskin = new Equipment("Waterskin", 4);

    I could use any one of these and I've never created a method where I don't add ir remove something specific. I'm adding or removing only a single piece of equipment and it will be based on Index Number.

    I've also created a list method, that is supposed to list all the equipment currently in the backpack. Does this look right?

    public void listEquipment(String Equipment)
    {
        System.out.println(Equipment);
    }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trouble adding an object.

    For adding an Equipment object, create an addEquipment( Equipment equipment ) method that is called on the backpack instance:

    backpack.addEquipment( crowbar );

    The class Equipment name should be capitalized as you've done, but the instances of Equipment, the Equipment objects created, should have names that begin with lowercase letters:

    Equipment crowbar = new Equipment("Crowbar", 5);

    The object is named crowbar, the name of the Crowbar class is "Crowbar". It's important to keep these details straight and to be consistent.

    As for listing all equipment added to the pack, since theEquipment[] is an array of Equipment objects, you can simply iterate the array to print all of its elements using a for or enhanced for loop, similar to what you have but inside a loop and printing each element in turn.

  7. #7
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Trouble adding an object.

    I saw a similar problem on tutorialspoint.com/. Instance variables, For example, UHFootball myUHFootball = new UHFootball("file.jpg"); Can they find this if they ran it on their network? .com network that uses wordpress doing their html codes. I just wanted to add a picture in their comment box. I was reading about an MD5 hashed to my email, and seeing if this might work. This goes into the main method of the class called static. myUHFootball is referenced to object file.jpg. I hope it worked. It looked like it worked. They got it on html codes when I checked the <img src =" " section> I hope this simplified the concept a bit.

Similar Threads

  1. Trouble adding an object.
    By DarkestLord in forum Java Theory & Questions
    Replies: 1
    Last Post: May 21st, 2014, 02:21 AM
  2. Trouble adding objects to JComboBox
    By MarcusSt0ne in forum AWT / Java Swing
    Replies: 2
    Last Post: April 12th, 2013, 06:51 AM
  3. Adding Object to ArrayList
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 20th, 2013, 10:02 AM
  4. Trouble adding components to a JFrame
    By NcAdams in forum AWT / Java Swing
    Replies: 3
    Last Post: May 22nd, 2012, 10:46 PM
  5. Trouble adding exceptions to fixed queue class
    By Farmer in forum Exceptions
    Replies: 5
    Last Post: December 19th, 2011, 07:23 AM