Advertisement
evitanasevska

Marathon

Oct 21st, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. /* Marathon Problem 1 (1 / 9)
  2. Define Java interface IMarathon with the following methods:
  3.  
  4. Athlete bestTime() - that returns the winner of the marathon
  5.  
  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.  
  10. constructors (default and with parameters)
  11. set and get methods
  12. the method toString() in the following format: name / age / country of origin / time score (all of the parameters should be split with one space)
  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.  
  15. constructors (default and with parameters)
  16. set and get methods
  17. the method toString() in the following format: host country / year / attendees of the marathon (all of the parameters should be split with one space)
  18. bestTime()
  19. AthletesFrom(String s)
  20.  
  21. Sample input
  22. 3
  23. Ana Anovska
  24. zenski
  25. 17
  26. 1207
  27. Makedonija
  28. Simon Simonovski
  29. maski
  30. 27
  31. 1200
  32. Srbija
  33. Andrea Andreeska
  34. zenski
  35. 25
  36. 1305
  37. Makedonija
  38. Skopje
  39. 2015
  40. Makedonija
  41. Sample output
  42. Skopje
  43. 2015
  44. Ana Anovska
  45. 17
  46. Makedonija
  47. 1207.0
  48. Simon Simonovski
  49. 27
  50. Srbija
  51. 1200.0
  52. Andrea Andreeska
  53. 25
  54. Makedonija
  55. 1305.0
  56. Prvo mesto: Simon Simonovski
  57. 27
  58. Srbija
  59. 1200.0
  60.  
  61. Ima vkupno 2 atleticar/i od Makedonija  */
  62.  
  63. import java.util.Scanner;
  64.  
  65. interface IMaraton
  66. {
  67.     /**
  68.      *
  69.      * @return IMaraton е интерфејс кој има методи...
  70.      */
  71.     Athlete bestTime();
  72.     int AthletesFrom(String s);
  73. }
  74. class Athlete
  75. {
  76.     private String name;
  77.     private String gender;
  78.     private int age;
  79.     private double time;
  80.     public String state;
  81.  
  82.     public Athlete(String name, String gender, int age, double time, String state) {
  83.         this.name = name;
  84.         this.gender = gender;
  85.         this.age = age;
  86.         this.time = time;
  87.         this.state = state;
  88.     }
  89.  
  90.     public Athlete() {
  91.     }
  92.  
  93.     public String getName() {
  94.         return name;
  95.     }
  96.  
  97.     public String getGender() {
  98.         return gender;
  99.     }
  100.  
  101.     public int getAge() {
  102.         return age;
  103.     }
  104.  
  105.     public double getTime() {
  106.         return time;
  107.     }
  108.  
  109.     public String getState() {
  110.         return state;
  111.     }
  112.  
  113.     public void setName(String name) {
  114.         this.name = name;
  115.     }
  116.  
  117.     public void setGender(String gender) {
  118.         this.gender = gender;
  119.     }
  120.  
  121.     public void setAge(int age) {
  122.         this.age = age;
  123.     }
  124.  
  125.     public void setTime(double time) {
  126.         this.time = time;
  127.     }
  128.  
  129.     public void setState(String state) {
  130.         this.state = state;
  131.     }
  132.     @Override
  133.     public String toString()
  134.     {
  135.         return name+"\n"+age+"\n"+state+"\n"+time;
  136.     }
  137.    
  138. }
  139. class Marathon implements IMaraton
  140. {
  141.     private String host_country;
  142.     private int year;
  143.     private Athlete [] atleticari ;
  144.  
  145.     public Marathon(String host_country, int year, Athlete[] atleticari) {
  146.         this.host_country = host_country;
  147.         this.year = year;
  148.         this.atleticari = atleticari;
  149.     }
  150.  
  151.     public Marathon() {
  152.     }
  153.  
  154.     public String getHost_country() {
  155.         return host_country;
  156.     }
  157.  
  158.     public int getYear() {
  159.         return year;
  160.     }
  161.  
  162.     public Athlete[] getAtleticari() {
  163.         return atleticari;
  164.     }
  165.  
  166.     public void setHost_country(String host_country) {
  167.         this.host_country = host_country;
  168.     }
  169.  
  170.     public void setYear(int year) {
  171.         this.year = year;
  172.     }
  173.  
  174.     public void setAtleticari(Athlete[] atleticari) {
  175.         this.atleticari = atleticari;
  176.     }
  177.    
  178.     @Override
  179.     public Athlete bestTime() {
  180.         double min = atleticari[0].getTime();
  181.         int index= 0;
  182.         for (int i = 0; i < atleticari.length; i++) {
  183.             if(atleticari[i].getTime()<min)
  184.             {
  185.                 min = atleticari[i].getTime();
  186.                 index = i;
  187.             }
  188.         }
  189.         return atleticari[index];
  190.     }
  191.  
  192.     @Override
  193.     public int AthletesFrom(String s) {
  194.         int br=0;
  195.         for (int i = 0; i < atleticari.length; i++) {
  196.             if(atleticari[i].getState().equals(s))
  197.                 br++;
  198.         }
  199.         return br;
  200.     }
  201.  
  202.     @Override
  203.     public String toString() {
  204.         String s= new String();
  205.         s = s+ host_country + "\n" + year+"\n" ;
  206.         for (int i = 0; i < atleticari.length; i++) {
  207.             s = s+atleticari[i].toString() + "\n";
  208.         }
  209.         return s;
  210.    }
  211. }  
  212. public class Test {
  213.  
  214.    
  215.     public static void main(String[] args) {
  216.         Scanner input=new Scanner(System.in);
  217.         int n=input.nextInt();
  218.         Athlete[] atleticari = new Athlete[n];
  219.        
  220.         String ime;
  221.         String pol;
  222.         int vozrast;
  223.         double vreme;
  224.         String zemja;
  225.        
  226.         input.nextLine();
  227.            
  228.         for(int i=0;i<n;i++)
  229.         {
  230.             ime = input.nextLine();
  231.             pol = input.nextLine();
  232.             vozrast = input.nextInt();
  233.             vreme = input.nextDouble();
  234.             input.nextLine();
  235.             zemja = input.nextLine();
  236.             atleticari[i]=new Athlete(ime,pol,vozrast,vreme,zemja);
  237.         }
  238.        
  239.         String mesto;
  240.         int godina;
  241.         String zemjaP;
  242.         mesto = input.nextLine();
  243.         godina = input.nextInt();
  244.         input.nextLine();
  245.        
  246.         Marathon m1 = new Marathon(mesto, godina, atleticari);
  247.         System.out.print(m1.toString());
  248.        
  249.         zemjaP = input.nextLine();
  250.         System.out.println("Prvo mesto: " + m1.bestTime().toString()+"\n");
  251.         System.out.println("Ima vkupno " + m1.AthletesFrom(zemjaP) + " atleticar/i od " + zemjaP);
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement