Consider the association between a Country class and a Sportsperson class. Country class is defined with a name and other attributes like size, population, capital, etc, and a list of all the Sportspersons that come from it. A Sportsperson class is defined with a name and other attributes like age, height, weight, match scores (int array), number of matches played etc. Provide appropriate getters/setters and toString() method. In a real-world context, we can infer association between country and sports person that hails from that country. A Country object has-a list of Sportsperson objects that are related to it.Note that a sportsperson object can exist with his own attributes and methods, alone without the association with the country object. Similarly, a country object can exist independently without any association to a sportsperson object. In the main method make objects and print list of sportsman of the country, average scores of all sportsperson and the sportsperson who have highest scores in a match.
import java.util.ArrayList;
import java.util.List;
public class Country {
private String name;
private float size;
private String capital;
private int population;
private List<Sportsperson> sportsPersons = new ArrayList<>();
public Country(String name, float size, String capital, int population) {
this.name = name;
this.size = size;
this.capital = capital;
this.population = population;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public List<Sportsperson> getSportsPersons() {
return sportsPersons;
}
public void setSportsPersons(List<Sportsperson> sportsPersons) {
this.sportsPersons = sportsPersons;
}
@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
", size=" + size +
", capital='" + capital + '\'' +
", population=" + population +
", sportsPersons=" + sportsPersons +
'}';
}
}
import java.util.Arrays;
import java.util.Objects;
public class Sportsperson {
private String name;
private short age;
private float weight;
private float height;
private int[] matchScores;
private int matchPlayed = 0;
private Country country;
public Sportsperson() {
}
public Sportsperson(String name, short age, float weight, float height) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
public double getAverageScore() {
int sum = 0;
for(int i = 0; i < matchScores.length; i++) {
sum+=matchScores[i];
}
return sum / (double) matchScores.length;
}
public int getTheMostEfficientMatch() {
int highest = 0;
for(int i = 0; i < matchScores.length; i++) {
if(highest < matchScores[i]) {
highest = matchScores[i];
}
}
return highest;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getAge() {
return age;
}
public void setAge(short age) {
this.age = age;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public int[] getMatchScores() {
return matchScores;
}
public void setMatchScores(int[] matchScores) {
this.matchScores = matchScores;
this.matchPlayed = matchScores.length;
}
public int getMatchPlayed() {
return matchPlayed;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
if(!country.getSportsPersons().contains(this)) {
country.getSportsPersons().add(this);
}
}
@Override
public String toString() {
return "Sportsperson{" +
"name='" + name + '\'' +
", age=" + age +
", weight=" + weight + " kg" +
", height=" + height + " cm" +
", matchScores=" + Arrays.toString(matchScores) +
", matchPlayed=" + matchPlayed +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Sportsperson that = (Sportsperson) o;
return getAge() == that.getAge() && Float.compare(that.getWeight(), getWeight()) == 0 && Float.compare(that.getHeight(), getHeight()) == 0 && Objects.equals(getName(), that.getName());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAge(), getWeight(), getHeight());
}
}
public class Main {
public static void main(String[] args) {
Country country = new Country("USA", 9834000f,"Washington", 333449281);
Sportsperson sportsperson = new Sportsperson("Jayson Tatum", (short) 23, 95, 203);
sportsperson.setMatchScores(new int[]{23, 54, 13, 4});
sportsperson.setCountry(country);
Sportsperson sportsperson1 = new Sportsperson("Jaylen Brown", (short) 25, 101, 198);
sportsperson1.setMatchScores(new int[]{41, 15, 13});
sportsperson1.setCountry(country);
int highestScore = 0;
Sportsperson highestScorePerson = null;
for(Sportsperson sp : country.getSportsPersons()) {
System.out.println(sp);
System.out.println("Average score: " + sp.getAverageScore());
if(highestScore < sp.getTheMostEfficientMatch()) {
highestScore = sp.getTheMostEfficientMatch();
highestScorePerson = sp;
}
}
System.out.println("The highest scores " + highestScore + " in a match has " + highestScorePerson.getName());
}
}
Comments
Leave a comment