Invalid boolean field output in Java

I am starting a Java tutorial and am trying to get boolean fields lovesCatnip

and isGuidedog

to print as true

in my print release. My code runs and I am currently getting the following output.

Name: Animal Weight: 100 Sex: M
Name: Cat Weight: 50 Sex: F Loves Catnip: false
Name: Dog Weight: 60 Sex: M Is Guide Dog: false

      

Below is my code to achieve this result. There are two classes. Below is:

public class Animal {

    protected int weight; 
    protected String sex;
    protected boolean extra;

    public class Dog extends Animal{

        public Dog(int weight, String sex, boolean extra) {
            super(weight, sex, extra);
            this.extra = isGuideDog;
        }
        boolean isGuideDog;
    }

    public class Cat extends Animal{

            public Cat(int weight, String sex,boolean extra) {
            super(weight, sex, extra);
            this.extra = lovesCatnip;
        }
            boolean lovesCatnip;        
    }

    public Animal (int weight, String sex, boolean extra){
    this.weight =  weight;
    this.sex = sex;
    this.extra = extra;
    }
}

      

And this one

import apollo.exercises.ch07_inheritance.Animal.Cat;
import apollo.exercises.ch07_inheritance.Animal.Dog;

public class AnimalRunner {
    public static void main(String[] animals) {

        Animal animal = new Animal(100, "M", false);

        Cat cat = animal.new Cat (50, "F", true);

        Dog dog = animal.new Dog (60, "M", true);

        System.out.println("Name: Animal Weight: " + animal.weight + " Sex: " + animal.sex);
        System.out.println("Name: Cat Weight: " + cat.weight + " Sex: " + cat.sex + " Loves Catnip: " + cat.lovesCatnip);
        System.out.println("Name: Dog Weight: " + dog.weight + " Sex: " + dog.sex + " Is Guide Dog: " + dog.isGuideDog);
    }
}

      

Does anyone have any advice on what I did wrong?

+3


source to share


3 answers


For a class, Cat

it is because of this line:

this.extra = lovesCatnip;

      

No, if you create a Cat

value extra

, you always assign a value to it lovesCatnip

, and since you never initialize it, it will false

.



Now there is another problem and with using your attributes, why do you have an attribute extra

in your superclass if you create lovesCatnip

in a subclass and think they are the same?

It looks like you don't need extra

a superclass at all.

+1


source


It happens that the default value for boolean

in Java is false

and you never assign a value isGuideDog

or lovesCatnip

:

boolean lovesCatnip; // default: false

boolean isGuideDog;  // default: false  

      



I think you would like to do something like this in your constructors:

boolean lovesCatnip;
public Cat(int weight, String sex,boolean extra) {
    super(weight, sex, extra);
    this.extra = extra;       // <-
    this.lovesCatnip = extra; // <- I'm not 100% sure if this is the desired
}

      

+2


source


I believe

this.extra = lovesCatnip;

      

it should be

this.lovesCatnip = extra;

      

and

this.isGuideDog = extra;

      

You are modifying the passed parameter (without assigning it to the field).

0


source







All Articles