Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Marathon Problem 1 (1 / 3)
- Define Java interface IMarathon with the following methods:
- Athlete bestTime() - that returns the winner of the marathon
- int AthletesFrom(String s) - that returns the number of athletes from s country.
- Define Java class Athlete. For every athlete the following information is stored: name (String), gender (String), age (int), time score in seconds (double) and country of origin (String). For this class implement:
- - constructors (default and with parameters)
- - set and get methods
- - the method toString() in the following format: name / age / country of origin / time score (all of the parameters should be split with one space)
- Define Java class Marathon that implements the interface IMarathon. For every marathon the following information is stored: host country (String), year (int), an array of Athletes Athlete[]. For this class implement:
- - constructors (default and with parameters)
- - set and get methods
- - the method toString() in the following format: host country / year / attendees of the marathon (all of the parameters should be split with one space)
- - bestTime()
- - AthletesFrom(String s)
- */
- import java.util.Scanner;
- interface IMarathon {
- Athlete bestTime();
- int AthletesFrom(String s);
- }
- class Athlete {
- private String name, gender, country;
- private int age;
- private double score;
- public Athlete() { }
- public Athlete(String name, String gender, int age, double score, String country) {
- this.name = name;
- this.gender = gender;
- this.age = age;
- this.country = country;
- this.score = score;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public double getScore() {
- return score;
- }
- public void setScore(double score) {
- this.score = score;
- }
- @Override
- public String toString() {
- return name + "\n" + age + "\n" + country + "\n" + score + "\n";
- }
- }
- class Marathon implements IMarathon {
- private String hostCountry;
- private int year;
- private Athlete[] athletes;
- public Marathon() { }
- public Marathon(String hostCountry, int year, Athlete[] athletes) {
- this.hostCountry = hostCountry;
- this.year = year;
- this.athletes = athletes;
- }
- public String getHostCountry() {
- return hostCountry;
- }
- public void setHostCountry(String hostCountry) {
- this.hostCountry = hostCountry;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public Athlete[] getAthletes() {
- return athletes;
- }
- public void setAthletes(Athlete[] athletes) {
- this.athletes = athletes;
- }
- @Override
- public String toString() {
- String toReturn;
- toReturn = hostCountry + "\n" + year + "\n";
- for(Athlete a : athletes) toReturn += a.toString();
- return toReturn;
- }
- public Athlete bestTime() {
- Athlete winner = athletes[0];
- for(Athlete w : athletes) {
- if(w.getScore() < winner.getScore())
- winner = w;
- }
- return winner;
- }
- public int AthletesFrom(String s) {
- int counter = 0;
- for(Athlete at : athletes) {
- if(at.getCountry().equals(s)) counter++;
- }
- return counter;
- }
- }
- public class Test {
- public static void main(String[] args) {
- Scanner input=new Scanner(System.in);
- int n=input.nextInt();
- Athlete[] atleticari = new Athlete[n];
- String ime;
- String pol;
- int vozrast;
- double vreme;
- String zemja;
- input.nextLine();
- for(int i=0;i<n;i++)
- {
- ime = input.nextLine();
- pol = input.nextLine();
- vozrast = input.nextInt();
- vreme = input.nextDouble();
- input.nextLine();
- zemja = input.nextLine();
- atleticari[i]=new Athlete(ime,pol,vozrast,vreme,zemja);
- }
- String mesto;
- int godina;
- String zemjaP;
- mesto = input.nextLine();
- godina = input.nextInt();
- input.nextLine();
- Marathon m1 = new Marathon(mesto, godina, atleticari);
- System.out.print(m1.toString());
- zemjaP = input.nextLine();
- System.out.println("Prvo mesto: " + m1.bestTime().toString());
- System.out.println("Ima vkupno " + m1.AthletesFrom(zemjaP) + " atleticar/i od " + zemjaP);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement