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: Very confused

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

    Default Very confused

    Hi, i have an issue where my programs output is returning false in comparison to the benchmark output for an assignment I'm doing (can be seen in attachment below), I was wondering if anyone knows what I did wrong?, I put a comment with // below on what I think went wrong but I'm still unsure why the method isChestUnlocked returning false, maybe instead of referring to jar1 in class ground, I have to refer to jar1, jar2 and 3 in class Chest instead?, if so how do I do this since the variables have to be private.

    public class Stone
    {
        private String name;
        private int weight;
     
        public Stone()[ATTACH=CONFIG]3655[/ATTACH][ATTACH=CONFIG]3655[/ATTACH]
        {
            System.out.print("Enter stone name: ");
            name = Global.keyboard.nextLine();
     
            System.out.print("Enter stone weight: ");
            weight = Global.keyboard.nextInt();
            Global.keyboard.nextLine();
        }
     
    	public int getWeight()
    	{
         	return weight;
    	}
    }
     
    public class Jar
    {
     
        private int position;
        private Stone stone;
     
     
        public Jar()
        {
            position = 0;
            stone = null;
        }
     
        public Jar(int initPos, Stone stone)
        {
            position = initPos;
            this.stone = stone;
        }
     
         public void move(int distance)
        {
           position = position + distance;
        }
     
        public void moveTo(Jar jar)
        {
            jar.stone = stone;
            stone = null;
        }
     
        public int getWeight() {
        if (stone == null) {
            return 0;
        }
        return stone.getWeight();
        }
     
    public class Ground
    {
        private Jar jar1;
        private Jar jar2;
        private Jar jar3;
        private Player player;
        private Chest chest;
        private Stone stone1;
        private Stone stone2;
        private Stone stone3;
     
        public Ground()
        {
          player = new Player();
     
          jar1 = new Jar(1, new Stone());
          jar2 = new Jar(2, new Stone());
          jar3 = new Jar(3, new Stone());
     
          chest = new Chest();
        }
     
       public boolean isChestUnlocked(){
        if (jar1.getWeight() + jar2.getWeight() + jar3.getWeight() == chest.combination) // I think this is the main problem
            return true;
        else
            return false;
     }
    }
     
    public class Chest
    {
     
        public int combination;
        private int position = 4;
        private Jar jar1;
        private Jar jar2;
        private Jar jar3;
     
     
     
        public Chest()
        {
            System.out.print("Enter chest combination (5-10): ");
            combination = Global.keyboard.nextInt();
            Global.keyboard.nextLine();
     
     
            jar1 = new Jar(4, null);
            jar2 = new Jar(4, null);
            jar3 = new Jar(4, null);
     
        }
    }
    Webp.net-resizeimage.png

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Very confused

    why the method isChestUnlocked returning false,
    How are you trying to debug the code?
    Try adding some print statements that print out all the values (looks like 4) used in the if statement that controls what that method returns. The printed values should show you what is happening when the statement is executed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I am confused
    By Rutherford in forum The Cafe
    Replies: 0
    Last Post: January 23rd, 2018, 11:42 AM
  2. I'm confused
    By Wonderlandslost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2012, 01:33 PM
  3. help me , im confused!!!!
    By sephskie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 10:52 AM
  4. Confused..
    By jadowers in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 5th, 2011, 12:56 PM
  5. [SOLVED] confused
    By joon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 12th, 2011, 11:40 AM

Tags for this Thread