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

Thread: How to put other backpack to main backpack?

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to put other backpack to main backpack?

    I tryed to put some items to the backpack.
    I can add and remove some items, but I can't put another backpack in main backpack.

    After running the program I can see:
    backpack and some items inside

    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1.0 kg, 3.0 l)
    2: Torch (0.5 kg, 1.0 l)
    3: Box1 (5.0 kg, 25.0 l)
    Total: 6.6, 29.5

    backpack with removed second item

    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1.0 kg, 3.0 l)
    2: Box1 (5.0 kg, 25.0 l)
    Total: 6.1, 28.5

    backpack with other backpack

    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1.0 kg, 3.0 l)
    2: Box1 (5.0 kg, 25.0 l)
    3: Backpack (0.01 kg, 0.1 l)
    Total: 6.109999999999999, 28.6

    but I can't see other backpack with its items...
    I ought to see:
    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1 kg, 3 l)
    2: Torch (0.5 kg, 1 l)
    3: Box1 (5 kg, 25 l)
    Total: 5.6 kg 29.5 l
    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1 kg, 3 l)
    2: Box1 (5 kg, 25 l)
    Total: 5.1 kg 28.5 l

    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1 kg, 3 l)
    2: Box1 (5 kg, 25 l)
    3: Backpack (0.01 kg, 0.1 l) with
    1: Box2 (3 g, 8 l)
    Total: 3.01 kg, 8.1 l
    Total: 5.1 kg 28.5 l

    Maybe someone has any suggestions?
    My code is here:
    import java.util.ArrayList;
     
    class TableSafe {
        ArrayList<Thing> item = new ArrayList<>();
    }
     
    class Thing {
        protected double weight;
        protected double volume;
        protected String name;
     
        public Thing(String name, double weight, double volume) {
            if (name != null)
                this.name = name;
            else
                this.name = "";
            this.weight = Math.max(weight, 0.0);
            this.volume = Math.max(volume, 0.0);
        }
        public double getWeight() {
            return weight;
        }
        public double getVolume() {
            return volume;
        }
        public String getName() {
            return name;
        }
        @Override
        public String toString() {
            return name + " (" + weight + " kg, " + volume + " l)";
        }
    }
    class Backpack {
        double weight;
        double volume;
        double maxWeight;
        double maxVolume;
        String name;
        TableSafe element = new TableSafe();
     
        public Backpack(double weight, double volume, double maxWeight, double maxVolume) {
            this.weight = weight;
            this.volume = volume;
            this.maxWeight = maxWeight;
            this.maxVolume = maxVolume;
     
        }
        void add(Thing thing) {
            element.item.add(thing);
     
        }
        void add(Backpack small) {
            add(new Thing(Backpack.class.getSimpleName(), small.weight, small.volume));
        }
        public void remove(int index) {
            element.item.remove(index - 1);
        }
        public void printContent() {
            System.out.println(new Thing(Backpack.class.getSimpleName(), this.weight, this.volume) + " with");
            double totalWeight = 0.0;
            double totalVolume = 0.0;
            for (int i = 0; i < element.item.size(); i++) {
                System.out.print((i + 1) + ": ");
                Thing thing = element.item.get(i);
                System.out.println(thing);
                totalWeight += thing.weight;
                totalVolume += thing.volume;
     
            }
     
            System.out.println("Total: " + (totalWeight + this.weight) + ", " + (totalVolume + this.volume));
            System.out.println();
        }
        public double getWeight() {
            double total = 0.0;
            for (int i = 0; i < element.item.size(); i++) {
                total += element.item.get(i).getWeight();
            }
            return total + this.weight;
        }
        public double getVolume() {
            double total = 0.0;
            for (int i = 0; i < element.item.size(); i++) {
                total += element.item.get(i).getVolume();
            }
            return total + this.volume;
        }
        public String getName() {
            for (int i = 0; i < element.item.size(); i++) {
                String name = Backpack.class.getSimpleName();
            }
            return name;
        }
    }
     
    class Box extends Thing {
        protected double height;
        protected double width;
        protected double depth;
        protected double volume;
     
        public Box(String name, double weight,
                   double height, double width, double depth) {
            super(name, weight, height * width * depth * 1000);
            this.height = height;
            this.width = width;
            this.depth = depth;
            volume = height * width * depth * 1000;
        }
        public double getHeight() {
            return height;
        }
     
        public double getWidht() {
            return width;
        }
     
        public double getDepth() {
            return depth;
        }
        @Override
        public String getName() {
            return super.getName();
        }
    }
    public class Safe {
        public static void main(String[] args) {
            Backpack b = new Backpack(0.100, 0.5, 40, 50);
            b.add(new Thing("Ball", 1, 3));
            b.add(new Thing("Torch", 0.5, 1));
            b.add(new Box("Box1", 5, 0.1, 0.5, 0.5));
            b.printContent();
            b.remove(2);
            b.printContent();
            System.out.println(b.getWeight());
            System.out.println(b.getVolume());
            System.out.println();
     
            Backpack small = new Backpack(0.01, 0.1, 5, 13);
           // b = small;
            small.add(new Box("Box2", 3, 0.2, 0.2, 0.2));
            small.add(new Box("Box3", 1, 0.1, 0.2, 0.2));
            b.add(small);
     
            b.printContent();
     
        }
    }
    Thank's for every suggestions!
    Last edited by geoinfoguide; December 29th, 2018 at 11:45 AM.

  2. #2
    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: How to put other backpack to main backpack?

    Please post the code here on the forum (wrapped in code tags) that show what you are talking about. No links.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    I made a constructor in class Thing:
        public Thing(Backpack small) {
            this.small = small;
        }

    and a method in class Backpack
    void add(Backpack small) {
                    element.item.add(new Thing(small));
        }
    but this returns null...

  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: How to put other backpack to main backpack?

    this returns null.
    The add method is defined as void which means it does not return anything.

    Where is the null value you talk about?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    I've translated it wrong, sorry. The point is that the program returns the following response:

    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1.0 kg, 3.0 l)
    2: Box1 (5.0 kg, 25.0 l)
    3: null (0.0 kg, 0.0 l) //Here is my problem
    Total: 6.1, 28.5

  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: How to put other backpack to main backpack?

    the program returns the following response:
    Ok. Try debugging the code to see where that null value comes from. Add some print statements that print out the values of variables as the code executes to show what the computer sees when the code is executed.

    I do not get a null when I execute the posted code. Is the code you are executing different from the code that you posted? Please post the current code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    I'm still changing something in the code so I'd better paste it again. This is the last version.
    package Safe;
     
    import java.util.ArrayList;
     
    class TableSafe {
     
        ArrayList<Thing> item = new ArrayList<>();
    }
     
    class Thing {
        protected double weight;
        protected double volume;
        protected String name;
        Backpack small;
     
        public Thing(String name, double weight, double volume) {
            if (name != null)
                this.name = name;
            else
                this.name = "";
            this.weight = Math.max(weight, 0.0);
            this.volume = Math.max(volume, 0.0);
        }
     
        public Thing(Backpack small) {
            this.small = small;
        }
     
        public double getWeight() {
            return weight;
        }
     
        public double getVolume() {
            return volume;
        }
     
        public String getName() {
            return name;
        }
     
        @Override
        public String toString() {
            return name + " (" + weight + " kg, " + volume + " l)";
        }
    }
     
    class Backpack {
        double weight;
        double volume;
        double maxWeight;
        double maxVolume;
        String name;
        TableSafe element = new TableSafe();
     
     
        public Backpack(double weight, double volume, double maxWeight, double maxVolume) {
            this.weight = weight;
            this.volume = volume;
            this.maxWeight = maxWeight;
            this.maxVolume = maxVolume;
        }
     
        public void add(Thing thing) {
            element.item.add(thing);
        }
     
     
        public void remove(int index) {
            element.item.remove(index - 1);
        }
     
        boolean add(Backpack small) {
     
    //        element.item.add(new Thing(Backpack.class.getSimpleName(), small.weight, small.volume));
            return element.item.add(new Thing(small));
     
        }
     
     
        public void printContent() {
            System.out.println(new Thing(Backpack.class.getSimpleName(), this.weight, this.volume) + " with");
            double totalWeight = 0.0;
            double totalVolume = 0.0;
            for (int i = 0; i < element.item.size(); i++) {
                System.out.print((i + 1) + ": ");
                Thing thing = element.item.get(i);
                System.out.println(thing);
                totalWeight += thing.weight;
                totalVolume += thing.volume;
     
            }
            System.out.println("Total: " + (totalWeight + this.weight) + ", " + (totalVolume + this.volume));
            System.out.println();
        }
     
        public double getWeight() {
            double total = 0.0;
            for (int i = 0; i < element.item.size(); i++) {
                total += element.item.get(i).getWeight();
            }
            return total + this.weight;
        }
     
        public double getVolume() {
            double total = 0.0;
            for (int i = 0; i < element.item.size(); i++) {
                total += element.item.get(i).getVolume();
            }
            return total + this.volume;
        }
     
        public String getName() {
            for (int i = 0; i < element.item.size(); i++) {
                String name = Backpack.class.getSimpleName();
            }
            return name;
        }
    }
     
    class Box extends Thing {
        protected double height;
        protected double width;
        protected double depth;
        protected double volume;
     
        public Box(String name, double weight,
                   double height, double width, double depth) {
            super(name, weight, height * width * depth * 1000);
            this.height = height;
            this.width = width;
            this.depth = depth;
            volume = height * width * depth * 1000;
        }
     
        public double getHeight() {
            return height;
        }
     
        public double getWidht() {
            return width;
        }
     
        public double getDepth() {
            return depth;
        }
     
        @Override
        public String getName() {
            return super.getName();
        }
    }
     
    public class Safe {
        public static void main(String[] args) {
            Backpack b = new Backpack(0.100, 0.5, 40, 50);
            b.add(new Thing("Ball", 1, 3));
            b.add(new Thing("Torch", 0.5, 1));
            b.add(new Box("Box1", 5, 0.1, 0.5, 0.5));
            b.printContent();
            b.remove(2);
            b.printContent();
            System.out.println(b.getWeight());
            System.out.println(b.getVolume());
            System.out.println();
     
            Backpack small = new Backpack(0.01, 0.1, 5, 13);
            small.add(new Box("Box2", 3, 0.2, 0.2, 0.2));
            small.add(new Box("Box3", 1, 0.1, 0.2, 0.2));
            b.add(small);
     
            small.printContent();
            b.printContent();
        }
    }

  8. #8
    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: How to put other backpack to main backpack?

    Did you try using some print statements to find where the null value comes from?


    3: null (0.0 kg, 0.0 l) //Here is my problem
    What is the expected output? What should be printed instead of null?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1 kg, 3 l)
    2: Box1 (5 kg, 25 l)
    3: Backpack (0.01 kg, 0.1 l) with
    1: Box2 (3 g, 8 l)
    2: Box3 (1 g, 4 l
    Total: 4.01 kg, 12.1 l
    Total: 10.1 kg 40.1 l

  10. #10
    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: How to put other backpack to main backpack?

    I asked:
    What should be printed instead of null?
    That is a lot more than the one line with the null value.

    What does the code need to differently to get that output?
    Where are the extra lines (instead of null) supposed to come from?

    Did you try using some print statements to find where the null value comes from?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    I know how to put to the backpack some things,
    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1.0 kg, 3.0 l)
    2: Torch (0.5 kg, 1.0 l)
    3: Box1 (5.0 kg, 25.0 l)
    Total: 6.6, 29.5

    I can create second backpack with other (small) things.
    Backpack (0.01 kg, 0.1 l) with
    1: Box2 (3.0 kg, 8.000000000000002 l)
    2: Box3 (1.0 kg, 4.000000000000001 l)
    Total: 4.01, 12.100000000000003

    But I can,t know how to put second backpack to the first backpack.
    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1 kg, 3 l)
    2: Box1 (5 kg, 25 l)
    3: Backpack (0.01 kg, 0.1 l) with //This item ought to be in the first backpack
    1: Box2 (3 g, 8 l) //but I can't do this
    2: Box3 (1 g, 4 l
    Total: 4.01 kg, 12.1 l
    Total: 10.1 kg 40.1 l






    I'm sorry I can not explain it better, but I'm just starting my enjoying with java.
    I still have to learn a lot.

  12. #12
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How to put other backpack to main backpack?

    Do you want to place the backpack in another backpack or just transfer the items from one backpack to the other?

    Regards,
    Jim

  13. #13
    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: How to put other backpack to main backpack?

    how to put second backpack to the first backpack.
    Can you describe what that means?
    After adding, Should the original backpack now contain a backpack or should the contents be removed from the second backpack and merged with the contents of the original backpack?
    What if a second backpack is added?
    Can a backpack that contains a backpack be added to another backpack?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    Thanks for interesting;
    I'd like to place the backpack in another backpack...

    --- Update ---

    Yes, I'd like to only have a second backpack in the original backpack.
    That's all.

  15. #15
    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: How to put other backpack to main backpack?

    Can a backpack can 3 different contents: some stuff or a backpack or both?

    But the backpack that is in another backpack can not contain a third backpack. Can only nest one deep.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    Yes, I'd like to have only nest one deep.

  17. #17
    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: How to put other backpack to main backpack?

    have only nest one deep.
    Does that mean that the add method for adding a backpack needs to check if the backpack that is being added contains a backpack?
    Also does the add method need to check if a backpack has already been added (to allow only 1 backpack to be added)?

    In other words, should the add method throw an exception when either of the above happens?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    It would be good, but it is not necessary. Now I just need advice on how to add one backpack to another

  19. #19
    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: How to put other backpack to main backpack?

    how to add one backpack to another
    What is the problem with the current code?

    What happens when a backpack is added now? Where is the reference to it saved?

    My testing shows that the backpack is added successfully.
            small.name = "a Small BP";                                        //<<<<<<< give backpack a name
           System.out.println(b.element.item);    // [Ball (1.0 kg, 3.0 l), Box1 (5.0 kg, 25.0 l), Backpack >a Small BP (0.01 kg, 0.1 l) ]
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put other backpack to main backpack?

    Quote Originally Posted by Norm View Post
    What is the problem with the current code?
    My testing shows that the backpack is added successfully.
            small.name = "a Small BP";                                        //<<<<<<< give backpack a name
           System.out.println(b.element.item);    // [Ball (1.0 kg, 3.0 l), Box1 (5.0 kg, 25.0 l), Backpack >a Small BP (0.01 kg, 0.1 l) ]
    After entering the above code, I have:
    [Ball (1.0 kg, 3.0 l), Box1 (5.0 kg, 25.0 l), null (0.0 kg, 0.0 l)]

    and I should have:
    Backpack (0.1 kg, 0.5 l) with
    1: Ball (1 kg, 3 l)
    2: Box1 (5 kg, 25 l)
    3: Backpack (0.01 kg, 0.1 l) with
    1: Box2 (3 g, 8 l)
    Total: 3.01 kg, 8.1 l
    Total: 5.1 kg 28.5 l

  21. #21
    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: How to put other backpack to main backpack?

    My test code was to show that the values were added to the list. It was not supposed to format the output the way you show.
    The code that makes the print out needs to be changed to create the desired output.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. "Could not find the main class: main. program will exit" - Wierd case.
    By AvivC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2014, 07:44 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. Replies: 1
    Last Post: November 29th, 2010, 08:28 AM