I have written out the assignment, which needs me to print seven different countries, its' color, and its' neighbors and then color them with 4 colors using a method. I figured out how to print, but cannot figure out how to color one of 4 colors - Blue, Yellow, Green, or Red. It should be random, like a method that checks for neighbors color and then assign one, so at the end none of the same colors touch each. I understand how to do it in my head, but don't have enough skills and knowledge about how to connect things, so I have no idea how to do it. Writing it in BlueJ. I would really appreciate some help, please. Thanks )
import java.util.*;
public class Solve
{
public ArrayList<Country>countries;
public Solve()
{
countries = new ArrayList<>();
countries.add(new Country("Germany", "NONE", "Poland", "Czech Republic", "Austria", "Switzerland", "NONE", "NONE", "NONE"));
countries.add(new Country("Poland", "NONE", "Germany", "Czech Republic", "Slovakia", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Czech Republic", "NONE", "Poland", "Germany", "Austria", "Slovakia", "NONE", "NONE", "NONE"));
countries.add(new Country("Slovakia", "NONE", "Czech Republic", "Hungary", "Austria", "Poland", "NONE", "NONE", "NONE"));
countries.add(new Country("Hungary", "NONE", "Slovenia", "Slovakia", "Austria", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Slovenia", "NONE", "Italy", "Hungary", "Austria", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Italy", "NONE", "Slovenia", "Switzerland", "Austria", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Austria", "NONE", "Italy", "Slovenia", "Hungary", "Slovakia", "Czech Republic", "Germany", "Switzerland"));
countries.add(new Country("Switzerland", "NONE", "Italy", "Austria", "Germany", "NONE", "NONE", "NONE", "NONE"));
}
public void printData()
{
String first = "NONE";
String second = "NONE";
String third = "NONE";
String fourth = "NONE";
String fifth = "NONE";
String sixth = "NONE";
String seventh = "NONE";
String color = "NONE";
for(Country country:countries)
{
color = country.getColor().equals("NONE") ? "" : (country.getColor() + ", ");
first = country.getFirstNeighbor().equals("NONE") ? "" : (country.getFirstNeighbor() + ", ");
second = country.getSecondNeighbor().equals("NONE") ? "" : (country.getSecondNeighbor() + ", ");
third = country.getThirdNeighbor().equals("NONE") ? "" : (country.getThirdNeighbor() + ", ");
fourth= country.getFourthNeighbor().equals("NONE") ? "" : (country.getFourthNeighbor() + ", ");
fifth = country.getFifthNeighbor().equals("NONE") ? "" : (country.getFifthNeighbor() + ", ");
sixth = country.getSixthNeighbor().equals("NONE") ? "" : (country.getSixthNeighbor() + ", ");
seventh = country.getSeventhNeighbor().equals("NONE") ? "" : (country.getSeventhNeighbor() + ", ");
System.out.println("COUNTRY - " + country.getName() + " | COLOR - " + color + " | NEIGHBORS - " + first + second + third + fourth + fifth + sixth + seventh);
}
}
public void printNew()
{
......
}
}
import java.util.*;
public class Country
{
private String name;
private String currentColor;
private String country1;
private String country2;
private String country3;
private String country4;
private String country5;
private String country6;
private String country7;
public Country(String nameIn, String originalColor, String firstCountry, String secondCountry, String thirdCountry, String fourthCountry, String fifthCountry, String sixthCountry, String seventhCountry)
{
name = nameIn;
currentColor = originalColor;
country1 = firstCountry;
country2 = secondCountry;
country3 = thirdCountry;
country4 = fourthCountry;
country5 = fifthCountry;
country6 = sixthCountry;
country7 = seventhCountry;
}
public String getName()
{
return name;
}
public String getColor()
{
return currentColor;
}
public String getFirstNeighbor()
{
return country1;
}
public String getSecondNeighbor()
{
return country2;
}
public String getThirdNeighbor()
{
return country3;
}
public String getFourthNeighbor()
{
return country4;
}
public String getFifthNeighbor()
{
return country5;
}
public String getSixthNeighbor()
{
return country6;
}
public String getSeventhNeighbor()
{
return country7;
}
}
Aucun commentaire:
Enregistrer un commentaire