Created
September 30, 2016 23:42
-
-
Save tipochka/4ce7a25e57ffb32282c9ea6d16900baf to your computer and use it in GitHub Desktop.
Блинов. Глава 4. Вариант А. 5 (*). Создать объект класса Планета, используя классы Материк, Океан, Остров. Методы: вывести на консоль название материка, планеты, количество материков.
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 Continent { | |
private String name; | |
public Continent(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
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 Island { | |
private String name; | |
public Island(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
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 Ocean { | |
private String name; | |
public Ocean(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
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 Planet { | |
private String name; | |
private List<Ocean> oceanList = new ArrayList<>(); | |
private List<Continent> continentList = new ArrayList<>(); | |
private List<Island> islandList = new ArrayList<>(); | |
public Planet(String name) { | |
this.name = name; | |
} | |
public void addOcean(Ocean ocean) { | |
oceanList.add(ocean); | |
} | |
public void addContinent(Continent continent) { | |
continentList.add(continent); | |
} | |
public void addIsland(Island island) { | |
islandList.add(island); | |
} | |
public String getName() { | |
return name; | |
} | |
public List<Ocean> getOceanList() { | |
return oceanList; | |
} | |
public List<Continent> getContinentList() { | |
return continentList; | |
} | |
public List<Island> getIslandList() { | |
return islandList; | |
} | |
} |
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 PlanetRunner { | |
public static void main(String[] args) { | |
Island island = new Island("Barbados"); | |
Planet planet = new Planet("Earth"); | |
planet.addContinent(new Continent("Eurasia")); | |
planet.addContinent(new Continent("Africa")); | |
planet.addOcean(new Ocean("Atlantic")); | |
planet.addOcean(new Ocean("Pacific")); | |
planet.addIsland(new Island("Barbados")); | |
System.out.println("Planet name: "+planet.getName()); | |
System.out.println("Continent name: "+getFirstContinentName(planet.getContinentList())); | |
System.out.println("Count continents:" + planet.getContinentList().size()); | |
} | |
public static String getFirstContinentName(List<Continent> continentList) { | |
String result = null; | |
for (Continent continent: continentList) { | |
result = continent.getName(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment