I have two constructors that are pretty much exactly the same except that one also takes in a boolean value. I want the constructor with four parameters to the call the other one in order to get the the first three. Here is what I have so far.

public Tree(String species, String autumnColor, int price) {
        if (species == "") {
            throw new IllegalArgumentException("Please enter a tree species.");
        }
        if (!autumnColor.equals("red") && !autumnColor.equals("orange") && !autumnColor.equals("yellow")) {
            throw new IllegalArgumentException("Please enter the tree's Autumn color as yellow, orange, or red.");
        }
        if (autumnColor.equals("red")) {
            fallColor = Color.RED;
        }
        if (autumnColor.equals("orange")) {
            fallColor = Color.ORANGE;
        }
        if (autumnColor.equals("yellow")) {
            fallColor = Color.YELLOW;
        }
        if (price == 0) {
            throw new IllegalArgumentException("Please enter a price value greater than zero dollars.");
        }
        treeSpecies = species;
        treePrice = price;
        currentColor = Color.GREEN;
    }

    public Tree(String species, String autumnColor, int price, boolean autumnSwitch) {
        this.(Tree(String species, String autumnColor, int price));
        if(!autumnSwitch.equals(true) && !autumnSwitch.equals(false)) {
            throw new IllegalArgumentException("Please enter true or false.");
        }
    }

The second constructor gets an error message and I'm not sure what format I need in order to fix it. Thanx in advance.

Recommended Answers

All 3 Replies

Change line 26 to

this(species, autumnColor, price);

Also, there's no need for the check on lines 27-28, since a boolean can only be either true or false in Java. There is no other possible value it can be.

Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.