In: Computer Science
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);
}
}
// 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: