Question

In: Computer Science

A zoo receives monkeys in batches from time to time. Whenever a new batch of monkeys...

A zoo receives monkeys in batches from time to time. Whenever a new batch of monkeys arrives, the zookeeper will try to accommodate them in cages of 4 different sizes, i.e., 10-monkey cage, 5-monkey cage, 2-monkey cage, and a single-monkey cage. The zookeeper is supposed to use as few cages as possible and each cage must be full. For example, a 5-monkey cage is not allowed to accommodate only 2 monkeys. The strategy is to first pack the monkeys into 10-monkey cages. Then the leftover monkeys into 5-monkey cages, and then into 2-monkey cages, and finally the remaining monkey (if any) into a single-monkey cage. You are going to write a program to enable the zookeeper to manage the allocation of the monkeys to the cages.

a. Complete the following class according to the comments. There are five places to be completed in this class.

b. When test() is executed, what is the output?

public class Monkeys {
   static int totalMonkey = 0; // total monkeys in the zoo
   int numMonkey = 0;
  
   public Monkeys(int nMonkey, int nCage){
       // nMonkey: the number of new monkeys in the batch
       // nCage: the maximum number of cages can be used to
       // accommodate the new monkeys

       numMonkey = nMonkey;      
       totalMonkey = totalMonkey + nMonkey;
      
       // tenMonkeyCage: the number of 10-monkey cages should be used
       // to accommodate the new monkeys, etc.
       int tenMonkeyCage;
       int fiveMonkeyCage;
       int twoMonkeyCage;
       int oneMonkeyCage;

       // You are required to fill the code here:
       tenMonkeyCage = ____________________
       fiveMonkeyCage = ____________________
       twoMonkeyCage = ____________________
       oneMonkeyCage = ____________________
      
       int numCage = tenMonkeyCage + fiveMonkeyCage + twoMonkeyCage + oneMonkeyCage;
      
       // You are required to fill the code here:
       if (____________________) {
           System.out.println("Need More Cages!");
           return;          
       }
       if (numCage > 10) {
           CrazyMonkey();
       }
       if (numCage < 10) {
           LazyMonkey(numMonkey);
           System.out.println("numMonkey:" + numMonkey);   
       }      
   }
  
   public void CrazyMonkey() { // some crazy calculations
       numMonkey = numMonkey + 10;
       int totalMonkey = 10;
       {
           int numMonkey = 100;
           numMonkey = numMonkey + 10;
           totalMonkey = totalMonkey + numMonkey;
       }
       numMonkey++;
       System.out.println("numMonkey:" + numMonkey);
       System.out.println("totalMonkey:" + totalMonkey);
   }
  
   public void LazyMonkey(int numMonkey) {
       numMonkey = totalMonkey - numMonkey;
       System.out.println("numMonkey:" + numMonkey);
   }      
   public static void test(){
       Monkeys A= new Monkeys(15,1);
       Monkeys B= new Monkeys(88,20);
       Monkeys C= new Monkeys(23,7);
   }
}

Solutions

Expert Solution

// Monkeys.java

public class Monkeys {
  
   static int totalMonkey = 0; // total monkeys in the zoo
   int numMonkey = 0;
  
   public Monkeys(int nMonkey, int nCage){
   // nMonkey: the number of new monkeys in the batch
   // nCage: the maximum number of cages can be used to
   // accommodate the new monkeys

   numMonkey = nMonkey;
   totalMonkey = totalMonkey + nMonkey;
  
   // tenMonkeyCage: the number of 10-monkey cages should be used
   // to accommodate the new monkeys, etc.
   int tenMonkeyCage;
   int fiveMonkeyCage;
   int twoMonkeyCage;
   int oneMonkeyCage;

   // You are required to fill the code here:
   tenMonkeyCage = nMonkey/10; // get the number of ten monkey cage by dividing the number of monkeys by 10

   // check number of leftover monkeys > 0,
   // then divide the left over monkeys by 5 to get the number of cages in five monkey cage
   fiveMonkeyCage = (nMonkey - tenMonkeyCage*10) > 0 ? (nMonkey - tenMonkeyCage*10)/5 : 0;
     
   // check number of leftover monkeys > 0,
   // then divide the left over monkeys by 2 to get the number of cages in five monkey cage
   twoMonkeyCage = (nMonkey - tenMonkeyCage*10 - fiveMonkeyCage*5) > 0 ? (nMonkey - tenMonkeyCage*10 - fiveMonkeyCage*5)/2 : 0;
     
   // check number of leftover monkeys > 0,
   // then assign the number of one monkey case to left over monkeys
   oneMonkeyCage = (nMonkey - tenMonkeyCage*10 - fiveMonkeyCage*5 - twoMonkeyCage*2) > 0 ? (nMonkey - tenMonkeyCage*10 - fiveMonkeyCage*5 - twoMonkeyCage*2) : 0;
  
   int numCage = tenMonkeyCage + fiveMonkeyCage + twoMonkeyCage + oneMonkeyCage;
  
   // number of cages required > number of cages available
   if (numCage > nCage)
   {
   System.out.println("Need More Cages!");
   return;
   }
   if (numCage > 10) {
   CrazyMonkey();
   }
   if (numCage < 10) {
   LazyMonkey(numMonkey);
   System.out.println("numMonkey:" + numMonkey);   
   }
   }
     
   public void CrazyMonkey() { // some crazy calculations
   numMonkey = numMonkey + 10;
   int totalMonkey = 10;
   {
   int numMonkey = 100;
   numMonkey = numMonkey + 10;
   totalMonkey = totalMonkey + numMonkey;
   }
   numMonkey++;
   System.out.println("numMonkey:" + numMonkey);
   System.out.println("totalMonkey:" + totalMonkey);
   }
  
   public void LazyMonkey(int numMonkey) {
   numMonkey = totalMonkey - numMonkey;
   System.out.println("numMonkey:" + numMonkey);
   }
     
   public static void test(){
   Monkeys A = new Monkeys(15,1);
   Monkeys B = new Monkeys(88,20);
   Monkeys C = new Monkeys(23,7);
   }
}

//end of Monkeys.java

// MonkeysTester.java

public class MonkeysTester {
  
   public static void main(String[] args) {
      
       Monkeys.test();
   }

}

//end of MonkeysTester.java

Output:


Related Solutions

New world monkeys and old world monkeys
What are the major differences between Platyrrhini and Catarrhini?
1. The percent yield (Y) of product from a batch chemical process for 20 batches is...
1. The percent yield (Y) of product from a batch chemical process for 20 batches is given below: Y = { 48.7 48.8 49.0 49.2 49.5 49.7 49.7 49.8 50.0 50.3 50.5 50.6 50.7 50.9 51.4 51.7 52.1 52.5 53.4 62.0} Draw a clearly labelled box plot for this data, showing how you calculate the various values. Comment on the distribution of the data presented with relevant justification.
1. A city bus service claims that the time to complete the route from the zoo...
1. A city bus service claims that the time to complete the route from the zoo to City Hall never varies by more than 2.0 minutes. The times for 18 randomly chosen trips from the zoo to City Hall were recorded and gave a sample mean of 26.6 minutes with a sample standard deviation of 2.25 minutes. Find and interpret a 98% confidence interval for the population standard deviation of travel times. (State any assumptions that you need to make.)
Sweet Tooth Temptations makes cupcakes in batches. To manufacture a batch of cupcakes, Brenda, the master...
Sweet Tooth Temptations makes cupcakes in batches. To manufacture a batch of cupcakes, Brenda, the master baker, must setup the measuring and mixing machines. Setup costs are batch-level costs because they are associated with batches rather than individual units of products. A separate Setup Department is responsible for setting up measuring and mixing machines for different flavours of cupcakes. Setup overhead costs consist of some costs that are variable and some costs that are fixed with respect to the number...
Stinehelfer Beet Processors, Inc., processes sugar beets in batches. A batch of sugar beets costs $60...
Stinehelfer Beet Processors, Inc., processes sugar beets in batches. A batch of sugar beets costs $60 to buy from farmers and $22 to crush in the company's plant. Two intermediate products, beet fiber and beet juice, emerge from the crushing process. The beet fiber can be sold as is for $36 or processed further for $28 to make the end product industrial fiber that is sold for $48. The beet juice can be sold as is for $56 or processed...
It has been said that enough monkeys, pounding on enough keyboards for enough time, will eventually...
It has been said that enough monkeys, pounding on enough keyboards for enough time, will eventually produce the complete works of William Shakespeare. Let’s examine this. Suppose we have 10 billion galaxies, each containing 10 billion planets populated by 10 billion immortal monkeys with extreme insomnia. We provide each with a keyboard consisting of 26 keys (we won’t insist on capitalization, punctuation, or even spaces). The monkeys proceed to randomly strike 10 keys per second for the age of the...
The Fresno Chaffee Zoo is designing a new Hippo River exhibit. They are interested in the...
The Fresno Chaffee Zoo is designing a new Hippo River exhibit. They are interested in the possibility of using wetlands areas to provide natural biological filtration (conversion of nitrogenous waste into nitrate and the removeal of nitrate from the system). In this system wetlands grasses would use nitrate from the river water as food, efectively removing it from the river. Research Question: Does sweetgrass or sedge (two wetland grasses) achieve a lower concentration of nitrate in the river water? Classify...
A batch production operation has a machine setup time of 4.0 hr and a processing time...
A batch production operation has a machine setup time of 4.0 hr and a processing time of 1.50 min per cycle. Three parts are produced each cycle. No tool handling time is included in the cycle. Part handling time each cycle is 30 sec. It consists of the worker obtaining three starting work units from a parts tray, loading them into the machine, and then after processing, unloading the completed units and placing them into the same tray. Each tray...
A project manager receives a change request from the customer. The change requires adding a new...
A project manager receives a change request from the customer. The change requires adding a new filtration system to the project in place of a cleaner bag. When the project manager evaluates the change, it is most important that she consider the impact on the: Sponsor Project constraints Communications management plan Subsequent work packages Conduct Procurements is a part of which process group? Monitoring and controlling Planning Initiating Executing A project manager is completing the WBS with the project team,...
The zoo is building a new polar bear exhibit, and wants to put a semi-circular window...
The zoo is building a new polar bear exhibit, and wants to put a semi-circular window in the concrete wall of the swimming tank. (Note: picture is not to scale) If the semi-circle has diameter 80 centimeters, and the bottom of the window is at a depth of 3 meters, find the hydrostatic force on the window.    Newtons
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT