Victor A Gonzalez
Victor A Gonzalez

Reputation: 3

Issue with Varargs with multiple classes in Java

So I'm having an issue when I create Variable Arguments with multiple classes. As you can see on the code below, I'm just trying to take in multiple cities and districts in a random order and try to print out the population of the entire state. Except the issue is I have no idea how to iterate through my CAndD array in order to add all of the populations together.

Code:

public class Main {
    public static void main(String args[]) {
        State S = new State("Florida", new District("Miami-Dade", 2752000),
                new City("Miami", 463347),
                new City("Tampa", 385430),
                new District("Broward", 1936000));
        System.out.println("The Population is: " + S.getPopulation());
    }
}

class CitiesAndDistricts {

}

class City extends CitiesAndDistricts{
    String name;
    int population;

    public City(String name, int population) {
        this.name = name;
        this.population = population;
    }
}

class District extends CitiesAndDistricts{
    String name;
    int population;

    public District(String name, int population) {
        this.name = name;
        this.population = population;
    }
}

class State {
    String name;
    int population;
    CitiesAndDistricts[] CAndD;

    public State(String name, CitiesAndDistricts ... entities) {
        this.name = name;
        CAndD = entities;
        for(int i = 0; i < CAndD.length; i++) {
            this.population += CAndD[i].population;
        }
    }

    public int getPopulation() {
        return population;
    }
}

If anyone can help me solve this problem that'd be great!

Upvotes: 0

Views: 48

Answers (2)

VienNguyen
VienNguyen

Reputation: 12

Designing an empty parent class seems not good. In your code, the class City and District are the same. I assume that you want to know the type of place (City or District). The better solution is moving all the properties of the child class to the parent class. The class CitiesAndDistricts would be:

class CitiesAndDistricts {
    String name;
    int population;
    public CitiesAndDistricts(String name, int population} {
        this.name = name;
        this.population = population;
    }
}

From this parent class, you can extend to class City and District as below:

class City extends CitiesAndDistricts{
    public City(String name, int population) {
        super(name, population);
    }
}

class District extends CitiesAndDistricts{
    public District(String name, int population) {
        super(name, population);
    }
}

Upvotes: 0

Shrirang Kumbhar
Shrirang Kumbhar

Reputation: 363

Please Have a look at below code, I have moved population variable from City and District to parent class:

public class Main {
    public static void main(String args[]) {
        State S = new State("Florida", new District("Miami-Dade", 2752000),
                new City("Miami", 463347),
                new City("Tampa", 385430),
                new District("Broward", 1936000));
        System.out.println("The Population is: " + S.getPopulation());
    }
}

class CitiesAndDistricts {
    int population;
}

class City extends CitiesAndDistricts{
    String name;


    public City(String name, int population) {
        this.name = name;
        this.population = population;
    }
}

class District extends CitiesAndDistricts{
    String name;

    public District(String name, int population) {
        this.name = name;
        this.population = population;
    }
}

class State {
    String name;
    int population;
    CitiesAndDistricts[] CAndD;

    public State(String name, CitiesAndDistricts ... entities) {
        this.name = name;
        CAndD = entities;
        for(int i = 0; i < CAndD.length; i++) {
            this.population += CAndD[i].population;
        }
    }

    public int getPopulation() {
        return population;
    }
}

Upvotes: 1

Related Questions