In: Computer Science
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value.
import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return aName; } public void editTag(String pTag, String pValue) { aTags.put(pTag, pValue); } }
import java.util.HashMap;
import java.util.Map;
class Student {
private final String aName;
private String aGender;
private int aAge;
private Country aCountry;
private Map<String, String> aTags = new HashMap<>();
// constructor to create Student object only by specifying name
Student(String name) {
this.aName = name;
// all other values are absent
this.aGender = "";
this.aAge = 0;
this.aCountry = new Country();
}
public Song(String pName)
{
aName = pName;
}
public String getName() {
return aName;
}
public void editTag(String pTag, String pValue) {
aTags.put(pTag, pValue);
}
// tag without pvalue
public void editTag(String pTag) {
aTags.put(pTag, "");
}
// setters
void setName(String name) {
this.aName = name;
}
void setAge(int age) {
this.aAge = age;
}
void setCountry(Country country) {
this.aCountry = country;
}
// getters
String getName() {
return this.aName;
}
int getAge() {
return this.aAge;
}
Country getCountry() {
return this.aCountry;
}
}
.
If you want exact output for some code in main, please provide main() and Country class code.