write a cooments of every single line of code of the following: public class MyBean{ private String name; private String city; private String country; private static final String[] VALID_COUNTRIES = {"GB","US","DE"}; private static final String[] VALID_CITIES = { {"London","Oxford","Leeds"}, {"New York","Los Angeles","Miami"}, {"Berlin","Frankfurt","Baden-Baden"} }; private static final String[] DEFAULT_CITIES = {"Leeds","Miami","Berlin"}; private static final String[] DEFAULT_COUNTRY = "GB"; private static final String[] DEFAULT_CITY = "Leeds"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = validateCity(city); } public String getCountry() { return country; } public void setCountry(String country) { this.country = validateCountry(country); this.city = validateCity(this.city); } private String validateCountry(String country) { if (country == null || country.isEmpty()) { return DEFAULT_COUNTRY; } For (String validCountry : VALID_COUNTRIES) { if (validCountry.equals(country)) { return country; } } return DEFAULT_COUNTRY; } Private String validateCity(String city) { if (city == null || city.isEmpty()) { return DEFAULT_CITY; } int CountryIndex = -1; for (int i = 0; i < VALID_COUNTRIES.length; i++) { if ( VALID_COUNTRIES[i].equals(this.country)) { countryIndex = i; break; } } if (countryIndex == -1) { return DEFAULT_CITY; } for (String validCity : VALID_CITIES[countryIndex]) { if (validCity.equals(city)) { return city; } } } return DEFAULT_CITIES[countryIndex]; } }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
write a cooments of every single line of code of the following:
public class MyBean{
private String name;
private String city;
private String country;
private static final String[] VALID_COUNTRIES = {"GB","US","DE"};
private static final String[] VALID_CITIES = {
{"London","Oxford","Leeds"},
{"New York","Los Angeles","Miami"},
{"Berlin","Frankfurt","Baden-Baden"}
};
private static final String[] DEFAULT_CITIES = {"Leeds","Miami","Berlin"};
private static final String[] DEFAULT_COUNTRY = "GB";
private static final String[] DEFAULT_CITY = "Leeds";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = validateCity(city);
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = validateCountry(country);
this.city = validateCity(this.city);
}
private String validateCountry(String country) {
if (country == null || country.isEmpty()) {
return DEFAULT_COUNTRY;
}
For (String validCountry : VALID_COUNTRIES) {
if (validCountry.equals(country)) {
return country;
}
}
return DEFAULT_COUNTRY;
}
Private String validateCity(String city) {
if (city == null || city.isEmpty()) {
return DEFAULT_CITY;
}
int CountryIndex = -1;
for (int i = 0; i < VALID_COUNTRIES.length; i++) {
if ( VALID_COUNTRIES[i].equals(this.country)) {
countryIndex = i;
break;
}
}
if (countryIndex == -1) {
return DEFAULT_CITY;
}
for (String validCity : VALID_CITIES[countryIndex]) {
if (validCity.equals(city)) {
return city;
}
}
}
return DEFAULT_CITIES[countryIndex];
}
}
Step by step
Solved in 3 steps