Advertisement
StefanTodorovski

[ADS/АПС] Intro 1: Marathon / Маратон

Oct 23rd, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. /*
  2. Marathon Problem 1 (1 / 3)
  3.  
  4. Define Java interface IMarathon with the following methods:
  5. Athlete bestTime() - that returns the winner of the marathon
  6. int AthletesFrom(String s) - that returns the number of athletes from s country.
  7.  
  8. 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:
  9. - constructors (default and with parameters)
  10. - set and get methods
  11. - the method toString() in the following format: name / age / country of origin / time score (all of the parameters should be split with one space)
  12.  
  13. 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:
  14. - constructors (default and with parameters)
  15. - set and get methods
  16. - the method toString() in the following format: host country / year / attendees of the marathon (all of the parameters should be split with one space)
  17. - bestTime()
  18. - AthletesFrom(String s)
  19. */
  20.  
  21. import java.util.Scanner;
  22.  
  23. interface IMarathon {
  24.     Athlete bestTime();
  25.     int AthletesFrom(String s);
  26. }
  27.  
  28. class Athlete {
  29.     private String name, gender, country;
  30.     private int age;
  31.     private double score;
  32.    
  33.     public Athlete() {  }
  34.     public Athlete(String name, String gender, int age, double score, String country) {
  35.         this.name = name;
  36.         this.gender = gender;
  37.         this.age = age;
  38.         this.country = country;
  39.         this.score = score;
  40.     }
  41.    
  42.     public String getName() {
  43.         return name;
  44.     }
  45.    
  46.     public void setName(String name) {
  47.         this.name = name;
  48.     }
  49.    
  50.     public String getGender() {
  51.         return gender;
  52.     }
  53.    
  54.     public void setGender(String gender) {
  55.         this.gender = gender;
  56.     }
  57.    
  58.     public String getCountry() {
  59.         return country;
  60.     }
  61.    
  62.     public void setCountry(String country) {
  63.         this.country = country;
  64.     }
  65.    
  66.     public int getAge() {
  67.         return age;
  68.     }
  69.    
  70.     public void setAge(int age) {
  71.         this.age = age;
  72.     }
  73.    
  74.     public double getScore() {
  75.         return score;
  76.     }
  77.    
  78.     public void setScore(double score) {
  79.         this.score = score;
  80.     }
  81.    
  82.     @Override
  83.     public String toString() {
  84.         return name + "\n" + age + "\n" + country + "\n" + score + "\n";
  85.     }
  86.    
  87. }
  88.  
  89. class Marathon implements IMarathon {
  90.     private String hostCountry;
  91.     private int year;
  92.     private Athlete[] athletes;
  93.    
  94.     public Marathon() { }
  95.    
  96.     public Marathon(String hostCountry, int year, Athlete[] athletes) {
  97.         this.hostCountry = hostCountry;
  98.         this.year = year;
  99.         this.athletes = athletes;
  100.     }
  101.  
  102.     public String getHostCountry() {
  103.         return hostCountry;
  104.     }
  105.  
  106.     public void setHostCountry(String hostCountry) {
  107.         this.hostCountry = hostCountry;
  108.     }
  109.  
  110.     public int getYear() {
  111.         return year;
  112.     }
  113.  
  114.     public void setYear(int year) {
  115.         this.year = year;
  116.     }
  117.  
  118.     public Athlete[] getAthletes() {
  119.         return athletes;
  120.     }
  121.  
  122.     public void setAthletes(Athlete[] athletes) {
  123.         this.athletes = athletes;
  124.     }
  125.    
  126.     @Override
  127.     public String toString() {
  128.         String toReturn;
  129.         toReturn = hostCountry + "\n" + year + "\n";
  130.         for(Athlete a : athletes) toReturn += a.toString();
  131.         return toReturn;
  132.     }
  133.  
  134.     public Athlete bestTime() {
  135.         Athlete winner = athletes[0];
  136.         for(Athlete w : athletes) {
  137.             if(w.getScore() < winner.getScore())
  138.                 winner = w;
  139.         }
  140.         return winner;
  141.     }
  142.    
  143.     public int AthletesFrom(String s) {
  144.         int counter = 0;
  145.         for(Athlete at : athletes) {
  146.             if(at.getCountry().equals(s)) counter++;
  147.         }
  148.         return counter;
  149.     }
  150. }
  151.  
  152. public class Test {
  153.  
  154.     public static void main(String[] args) {
  155.         Scanner input=new Scanner(System.in);
  156.         int n=input.nextInt();
  157.         Athlete[] atleticari = new Athlete[n];
  158.        
  159.         String ime;
  160.         String pol;
  161.         int vozrast;
  162.         double vreme;
  163.         String zemja;
  164.        
  165.         input.nextLine();
  166.            
  167.         for(int i=0;i<n;i++)
  168.         {
  169.             ime = input.nextLine();
  170.             pol = input.nextLine();
  171.             vozrast = input.nextInt();
  172.             vreme = input.nextDouble();
  173.             input.nextLine();
  174.             zemja = input.nextLine();
  175.             atleticari[i]=new Athlete(ime,pol,vozrast,vreme,zemja);
  176.         }
  177.        
  178.         String mesto;
  179.         int godina;
  180.         String zemjaP;
  181.         mesto = input.nextLine();
  182.         godina = input.nextInt();
  183.         input.nextLine();
  184.        
  185.         Marathon m1 = new Marathon(mesto, godina, atleticari);
  186.         System.out.print(m1.toString());
  187.        
  188.         zemjaP = input.nextLine();
  189.         System.out.println("Prvo mesto: " + m1.bestTime().toString());
  190.         System.out.println("Ima vkupno " + m1.AthletesFrom(zemjaP) + " atleticar/i od " + zemjaP);
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement