Java – Trying to pass both no value and boolean, but returns null

Trying to pass no value and a boolean value of true over two different objects set for the same class.

According to the TestDriver: The following should pass true as boolean via public Iron(boolean val)

Metal m1 = new Iron(true);
System.out.println("Should be Iron->"+m1);
System.out.println("Should be Enchanted (True)->"+m1.isEnchanted());`

However, passes as null, as though it is not reaching this constructor. Tell me it’s not trying to look at the Metal class instead with a constructor for Iron?

The second piece of code submits no value, but also returns null. I have tried using Boolean.parseBoolean() in order to try and convert a string to boolean and vice versa.

I have tried a pelthora of different ways to get it to work, however cannot see to get anywhere, if I am missing something obvious, please do tell.

Cheers.

public class TestDriver {

    public static void main(String[] args) {

        Metal m1 = new Iron(true);
        System.out.println("Should be Iron->"+m1);
        System.out.println("Should be Enchanted (True)->"+m1.isEnchanted());
        Iron i1 = new Iron();
        System.out.println("Should not be Enchanted (False)->"+i1.isEnchanted());
    }

}

public class Iron extends Metal {

    public boolean isEnchanted(boolean val) {       

        boolean res = false;

        if(val == true) {
            res = true;
        } else {
            res = false;
        }
        return (res);

    }

    public Iron() {

    }

    public Iron(boolean val) {

        boolean res = false;

        if(val == true) {
            res = true;
        } else {
            res = false;
        }
        //return (res);

    }
}

Expected true when passing true value and expecting false when passing no value, however when calling isEnchanted – should be called somehow.

Answer

You have not included your Metal class, but I will put a working example of what you specified here to get you started.

Below is the Metal class I made to meet the requirements you want:

public class Metal {

    private boolean res;
    private String name;

    public Metal() {
        this.res = false;
    }

    public Metal (boolean res, String name) {
        this.res = res;
        this.name = name;
    }

    public boolean isEnchanted() {
        return res;       
    }

    @Override
    public String toString() {
        return name;       
    }
}

I added a name class field to be used when you want to override toString since you want to print the name when the field is used in System.out.println. toString() can still be modified to print whatever you want in addition to the name.

Also take note of the changes to IsEnchanted(). This should be only a return of the class variable res so you can see what the value is set to, you should not put in logic that changes the boolean here.

Also you attempted to use many local variables which is not the correct way to take advantage of using an Object. See how the variable res is on the class level now and you no longer need to pass it around since you will be able to access it whenever you want to after setting it from the constructor the first time.

Here is the Iron class now after the modifications to the Metal class. Notice how little is needed to be done inside of Iron at this point. Most of the logic I assumed was to be used by other Metals as well, so you can now extend Metal easily and get functionality in, for example, a Copper now:

public class Iron extends Metal {

    public Iron() {
    }

    public Iron(boolean val) {
        super(val, "Iron");
    }
}

The most important thing to note here is the call to super(val, "Iron") which will pass the res parameter to the super class’s constructor and define the name to be "Iron". The super class in this case is Metal.

Now you can use your test code and it works as expected:

public static void main(String args[]){
    Metal m1 = new Iron(true);
    System.out.println("Should be Iron->"+ m1);
    System.out.println("Should be Enchanted (True)->" + m1.isEnchanted());
    Metal i1 = new Iron();
    System.out.println("Should not be Enchanted (False)->"+i1.isEnchanted());
}

Output:

Should be Iron->Iron

Should be Enchanted (True)->true

Should not be Enchanted (False)->false

Leave a Reply

Your email address will not be published. Required fields are marked *