Created
January 8, 2016 04:17
-
-
Save SIRHAMY/673336e968d1fbfd238f to your computer and use it in GitHub Desktop.
My WorldProblem solution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class City implements Municipality{ | |
private int population; | |
private String name; | |
public City(String name, int population) { | |
this.name = name; | |
this.population = population; | |
} | |
@Override | |
public int getPopulation() { | |
return population; | |
} | |
@Override | |
public String getString() { | |
return name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Country implements Municipality{ | |
private String name; | |
private Municipality[] municipalities; | |
public Country(String name, Municipality... entities) { | |
this.name = name; | |
municipalities = entities; | |
} | |
@Override | |
public int getPopulation() { | |
int result = 0; | |
for(Municipality mun: municipalities) { | |
result += mun.getPopulation(); | |
} | |
return result; | |
} | |
@Override | |
public String getString() { | |
StringBuffer result = new StringBuffer(); | |
result.append(name); | |
for(Municipality mun: municipalities) { | |
result.append(" " + mun.getString()); | |
} | |
return result.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class District implements Municipality{ | |
private int population; | |
private String name; | |
public District(String name, int population) { | |
this.name = name; | |
this.population = population; | |
} | |
@Override | |
public int getPopulation() { | |
return population; | |
} | |
@Override | |
public String getString() { | |
return name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
World myWorld = new World( | |
new Country("USA", | |
new State("NY", | |
new City("New York", 8143197)), | |
new City("LA", 3844829), | |
new City("Chicago", 2842518), | |
new District("Washington D.C.", 658893)), | |
new Country("Germany", | |
new City("Berlin", 3336026), | |
new City("Hamburg", 1605606), | |
new City("Munich", 831937)), | |
new Country("Tamriel", | |
new State("Skyrim", | |
new City("Dragonsreach", 580), | |
new City("Falkreath", 390), | |
new District("Sheogorath's Realm", 666)), | |
new State("Morrowind", | |
new City("Seyda Neen", 46)))); | |
myWorld.printPopulation(); | |
System.out.println(); | |
myWorld.printWorld(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Municipality { | |
public String getString(); | |
public int getPopulation(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class State implements Municipality{ | |
private String name; | |
private Municipality[] municipalities; | |
public State(String name, Municipality... entities) { | |
this.name = name; | |
municipalities = entities; | |
} | |
@Override | |
public int getPopulation() { | |
int result = 0; | |
for(Municipality mun: municipalities) { | |
result += mun.getPopulation(); | |
} | |
return result; | |
} | |
@Override | |
public String getString() { | |
StringBuffer result = new StringBuffer(); | |
result.append(name); | |
for(Municipality mun: municipalities) { | |
result.append(" " + mun.getString()); | |
} | |
return result.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class World { | |
private Country[] countries; | |
public World(Country... entities) { | |
countries = entities; | |
} | |
public void printPopulation() { | |
int result = 0; | |
for(Country c: countries) { | |
result += c.getPopulation(); | |
} | |
System.out.format("World Population: %d", result); | |
} | |
public void printWorld() { | |
StringBuffer result = new StringBuffer(); | |
for(Country c: countries) { | |
result.append(c.getString() + " "); | |
} | |
System.out.println("World to String: " + result.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment