Advertisement
Josephvelilla

Untitled

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