Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.awt.Rectangle;
  3. import java.util.Random;
  4. // import java.util.Random;
  5.  
  6. /*
  7.  * A soldier needs our help!
  8.  * Find the methods below and implement them in order to support him.
  9.  */
  10.  
  11. public class Soldier {
  12.     private String firstName;
  13.     private String lastName;
  14.     private String tag;
  15.     private String rank;
  16.     private Point position;
  17.  
  18.     public Soldier(String firstName, String lastName, String tag, String rank, Point position) {
  19.         this.firstName = firstName;
  20.         this.lastName = lastName;
  21.         this.tag = tag;
  22.         this.rank = rank;
  23.         this.position = position;
  24.     }
  25.  
  26.     public String getFirstName() {
  27.         return firstName;
  28.     }
  29.  
  30.     public String getLastName() {
  31.         return lastName;
  32.     }
  33.  
  34.     public String getTag() {
  35.         return tag;
  36.     }
  37.  
  38.     public String getRank() {
  39.         return rank;
  40.     }
  41.  
  42.     public Point getPosition() {
  43.         return position;
  44.     }
  45.    
  46.     public void setFirstName(String firstName) {
  47.         this.firstName = firstName;
  48.     }
  49.  
  50.     public void setLastName(String lastName) {
  51.         this.lastName = lastName;
  52.     }
  53.  
  54.     public void setNumberTag(String tag) {
  55.         this.tag = tag;
  56.     }
  57.  
  58.     public void setRank(String rank) {
  59.         this.rank = rank;
  60.     }
  61.    
  62.     public void setPosition(Point position) {
  63.         this.position = position;
  64.     }
  65.  
  66.     /* You must return the distance of the target soldier to the point battle given as a parameter.
  67.      * the formula of distance of two points P1(X1,Y1) and P2(X2,Y2) is ((Y2 - Y1)^2 + (X2 - X1)^2) ^1/2
  68.      * Hint: Look for the Point class in the Java 8 API and observe the predefined methods.
  69.      */
  70.  
  71.     public double distanceToBattle(Point battle) {
  72.        
  73.         // WRITE YOUR CODE HERE
  74.        
  75. //      double differenceX, differenceY;
  76. //     
  77. //      differenceX = Math.pow(this.position.getX() - battle.getX(), 2);
  78. //      differenceY = Math.pow(this.position.getY() - battle.getY(), 2);
  79. //     
  80. //      double distance = Math.pow((differenceY + differenceX), 1/2);
  81.        
  82.         double distanceToBattle = Math.pow(((Math.pow(battle.getY() - this.position.y , 2)) + (Math.pow(battle.getX() - this.position.x, 2))), 1/2);
  83.        
  84.         return distanceToBattle;
  85.     }
  86.  
  87.     /* Using the enemyZone parameter and the soldiers position determine if the soldier is inside
  88.      * or in the edge of the danger zone.
  89.      * Hint: Look for the Rectangle class in the Java 8 API and observe the predefined methods.
  90.      */
  91.     public boolean inDanger(Rectangle enemyZone) {
  92.         // WRITE YOUR CODE HERE
  93.         return false; // Dummy return.
  94.     }
  95.  
  96.     /*
  97.      * The soldier has setup the radio to communicate with his base, but after a while the
  98.      * radio stopped working. He suspects that he needs to change the frequency in order to make
  99.      * it work again. Help him find the right frequency. Remember that the formula for sinusoidal
  100.      * functions is y(t) = A*sin(2*PI*f*t + theta).
  101.      * Hint #1: Look for the Math class in the Java 8 API and observe the predefined methods and fields.
  102.      * Hint #2: Solve for f on the previously mentioned sinusoidal function.
  103.      * Hint #3: sin(asin(t)) == t
  104.      */
  105.     public double getRadioFrequency(double yOfT, double A, double t, double theta) {
  106.         // WRITE YOUR CODE HERE
  107.         return 0.0; // Dummy return.
  108.     }
  109.  
  110.     /*
  111.      * The Soldier lost his tag in battle and he needs to get a new one.
  112.      * All tags are numbers of 6 digits. Generate a new tag changing the last
  113.      * 3 digits with random numbers.
  114.      * Hint: Look for the Random class in the Java 8 API and observe the predefined methods.
  115.      */
  116.    
  117.     public void newTag(){
  118.         // WRITE YOUR CODE HERE
  119.        
  120.         Random rand = new Random();
  121.         int randNum = 456;
  122.        
  123.         while(randNum == 456) {
  124.             randNum = 100 + rand.nextInt(900);
  125.         }
  126.        
  127.         this.tag = tag.substring(0, 3).concat(String.valueOf(randNum));
  128.        
  129.     }
  130.  
  131.     /*
  132.      * A newly formed squad is looking for a leader. Decide who has more experience
  133.      * on their hands between the target and parameter soldier, then return their last
  134.      * name so the new squad know who he is. The name parameter has the following format: S.Rodriguez.
  135.      * Where S is the initial of the first name and Rodriguez is the last name. Also, for the sake
  136.      * of this exercise we will assume that there are three ranks: Captain, Major and Sergeant.
  137.      * Captain is the highest rank, then Major and then Sergeant.
  138.      * Hint: Ranks are in lexicographical order.
  139.      */
  140.     public String teamLeader(String rank, String name) {
  141.         // WRITE YOUR CODE HERE
  142.         return null; // Dummy return.
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement