Question

In: Mechanical Engineering

An animal in our JunglePark application needs to check whether another animal is in its neighborhood...

An animal in our JunglePark application needs to check whether another animal is in its neighborhood (close to it). To implement this behavior, add isClose() method to the Animal base class and implement it. This method must have the following signature.

public boolean isClose(Animal otherAnimal, int range) { }

The isClose() method returns TRUE if otherAnimal is located within a range distance [0 .. range] around the current animal and FALSE otherwise.

Solutions

Expert Solution

Code:

package test;

public class Animal {

   public String Animal;
   //position of the animal in x and co-ordinates
   public Double xDistance;
   public Double yDistance;

   public Animal(String animal, Double xDistance, Double yDistance) {
       super();
       Animal = animal;
       this.xDistance = xDistance;
       this.yDistance = yDistance;
   }

   public Animal() {
   }

   public String getAnimal() {
       return Animal;
   }

   public void setAnimal(String animal) {
       Animal = animal;
   }

   public Double getxDistance() {
       return xDistance;
   }

   public void setxDistance(Double xDistance) {
       this.xDistance = xDistance;
   }

   public Double getyDistance() {
       return yDistance;
   }

   public void setyDistance(Double yDistance) {
       this.yDistance = yDistance;
   }

   public boolean isClose(Animal otherAnimal, int range) {

       Double x1 = this.xDistance;
       Double y1 = this.yDistance;
       Double x2 = otherAnimal.getxDistance();
       Double y2 = otherAnimal.getyDistance();
      
      
       //Distance between the animals
       double distanceBetweenAnimals = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
       System.out.println("Distance betweeen them:" + distanceBetweenAnimals);
       System.out.println("Allowed Range is:" + range);

       if (distanceBetweenAnimals < range)
           return true;
       else
           return false;

   }

   public static void main(String[] args) {

       Animal firstAnimal = new Animal("Dog", 1.0, 1.0);
       Animal secondAnimal = new Animal("Cat", 9.0, 5.0);
       System.out.println(
               "Position of first animal:(" + firstAnimal.getxDistance() + "," + firstAnimal.getyDistance() + ")");
       System.out.println(
               "Position of first animal:(" + secondAnimal.getxDistance() + "," + secondAnimal.getyDistance() + ")");
       boolean check = firstAnimal.isClose(secondAnimal, 10);
       if (check == true)
           System.out.println("The animal is it its neighbourhood");
       else
           System.out.println("The animal is not in its neighbourhood");

   }

}

Screenshot:


Related Solutions

One application of our study of competitive markets is the decision of whether or not some...
One application of our study of competitive markets is the decision of whether or not some government services (such as garbage collection or prison operation) should be privatized. According to our discussion, the main argument for privatizing these services is that this might improve: a. Profits b. Oversight of operations c. Efficiency d. Wages of employees providing these services 2. When firms in a competitive market are experiencing positive economic profits, we expect some firms to ________ the market, which...
Check whether the indicated application is best represented by an open-loop or closed loop control system...
Check whether the indicated application is best represented by an open-loop or closed loop control system model.  If you think there is some ambiguity, discuss as necessary. APPLICATION (2 Points each) OPEN LOOP CLOSED LOOP Standard LED flashlight Antilock Braking System (ABS) in a car Distance traveled by fireworks, i.e. bottle rocket DVD player disc rotation speed Cruise control in a car Turning on a faucet in a sink Automatic flash used in your smart phone camera Accelerator pedal in a...
State the various electromagnetic waves and explain its application in our daily lives.
State the various electromagnetic waves and explain its application in our daily lives.
Application of the A&P of the Animal & Plant Cell: Relate the structures of the cell...
Application of the A&P of the Animal & Plant Cell: Relate the structures of the cell (animal or plant) to parts of some other concrete or abstract structure and explain how each structure connects. Example: Gas to a car is to the mitochondria of the cell. Explanation: Without gas a car would have no means of energy for power to move. Just as the mitochondria "powerhouse" provides energy for the cell. * Whatever structure chosen to compare the cell to...
How has/is our lack of knowledge surrounding animal diversity limiting our ability to identify the source...
How has/is our lack of knowledge surrounding animal diversity limiting our ability to identify the source of these pathogens?
Create a Web application that display an animal classification. For the classification uses the given csv...
Create a Web application that display an animal classification. For the classification uses the given csv file: test1.csv (open up the csv file in any no-code insertion text editor like: notepad, wordpad (windows); or vi, emacs, nano, pico (Macs) – just plain text editor NOT word processor like Word, Pages, etc). The csv file contains information on the types, traits, and examples of animal classification. Give the user the following options (it can be in radio buttons) to see the...
Avoparcin, a glycopeptide antibiotic, was widely used in animal feed in Denmark. Vancomycin is another glycopeptide....
Avoparcin, a glycopeptide antibiotic, was widely used in animal feed in Denmark. Vancomycin is another glycopeptide. In 1995, the percent of vancomycin-resistance Enterococcus faecalis (VRE) isolated from pigs was 100. The use of avoparcin was banned in Denmark in 1995. In 2002, Danish scientists reported that 20% of their E. faecalis isolates was vancomycin resistant. Explain why the percent of VRE decreased and why VRE in pigs is important to humans.
PYTHON: Our glass installation business needs a program that will assist our agents in calculating the...
PYTHON: Our glass installation business needs a program that will assist our agents in calculating the installed cost of shower enclosure. This is in particular for 90 degree glass shower enclosures. The program will get the depth (B), width (A) and height (C) of the shower base as shown on the diagram on the right. Program also needs to get the thickness of the glass as that affects the price. All input are in inches. For a 90-degree shower enclosure,...
1. When an individual receives antibodies that were produced by another person, or animal, providing short...
1. When an individual receives antibodies that were produced by another person, or animal, providing short lived or temporary immunity, that is referred as: active immunity humoral immunity immunosuppression passive acquired immunity immunocompetence 2.  A method to artificially induce an immune response to an infectious disease is: immunity resistance antitoxin vaccination conjugation 3. It is sometimes difficult to produce an immunization against certain pathogens because   they frequently change their molecular protein coat.  This is called: protective antibodies antigenic variation attenuation hypergammaglobulinemia artificial...
Kimmy needs to select 3 students to take part in a survey from our class. (our...
Kimmy needs to select 3 students to take part in a survey from our class. (our class has 25 students total-20 female, 5 male) a. how many ways can Kimmy select all male b. how many ways can Kimmy select 3 males and 2 females?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT