James decides to visit a small tropical island during his spring break. On the i
ID: 3680868 • Letter: J
Question
James decides to visit a small tropical island during his spring break. On the island, James find there lives a special type of chameleon. The chameleon can turn itself into X different colors. When chameleons change their colors, they always follow a fixed order. For example, if X = 3, the order is: Interestingly, when two male chameleons meet each other, they will both turn into the next color. For example, if currently a chameleon is in color 1, and it meets another chameleon in color 3, it will turn into color 2 while the other chameleon will turn into color 1. When two female chameleons meet each other, they will both turn into the previous color. And when two chameleons with different gender meet each other, their colors remain unchanged. In this homework, you will design a serial of Java classes to simulate this color-changing process of chameleons. Specifically, you are required to implement the following classes:
1.Class Island. The class represents the island habitat of the chameleons. The following table tells you what variables and methods should Island have. You can add other variables and methods which you deem necessary.
Class: Island
Type
Name
Description
Variable
chameleons
An Arraylist that stores all chameleons on the island. Of course, each chameleon is an object of the class
Chameleon.
Method (All
methods are public)
Island
Constructor. Initialize the island, make the Arraylist
chameleons empty.
AddChameleon
Create a new chameleon object, and then add it to the
Arraylist chameleons. The color and gender of the chameleon are given as input parameters.
AddChameleonRandom
Create a new chameleon object with random color/gender, and then add it to the Arraylist
chameleons.
Meet
The method takes two integer i and j as parameter, and simulate the situation where the i-th chameleon meets the j-th chameleon. For example, if they are both male, change their color to the next color.
Use system.out.println() to output the result of their meeting. For example:
“Chameleon no.4 changed to color 3”
“Chameleon no.7 changed to color 1”
MeetRandom
Randomly select two chameleons, and simulate the
situation where they meet. Use system.out.println() to output the result of their meeting.
package HW6;
import java.util.*;
//this class represents the island where chameleons live a happy life.
public class Island {
// This arraylist holds all the chamelenons on the island
private ArrayList<Chameleon> chameleons;
// This construct creates an Island object and make the Arraylist chameleons empty.
// It is finished for you.
public Island(){
chameleons = new ArrayList<Chameleon>();
}
// Create a new chameleon object, and then add it to the Arraylist chameleons.
public Chameleon AddChameleon(int Color, int Gender, int Colors){
// Finish this method
return null;
}
// Create a new chameleon object with random color and gender, and then add it to the Arraylist chameleons.
// This method is finished for you.
// Note that it returns the newly created chameleon object, makes it easy to debug.
public Chameleon AddChameleonRandom(int Colors){
Chameleon c = new Chameleon(Colors);
chameleons.add(c);
return c;
}
// Simulate the event that the i-the and j-th chameleon meet with each other.
public void Meet(int i, int j){
// Finish this method
}
// Simulate the event that two randomely selected chameleons meet with each other.
// This method is partially finished to help you get start.
public void MeetRandom(){
Random rnd = new Random();
int i,j;
i = j = 0;
// randomly select two non-equal integers between 0 and chameleons.size().
while( i == j){
i = rnd.nextInt(chameleons.size());
j = rnd.nextInt(chameleons.size());
}
// Finish this method
}
}
Class: Island
Type
Name
Description
Variable
chameleons
An Arraylist that stores all chameleons on the island. Of course, each chameleon is an object of the class
Chameleon.
Method (All
methods are public)
Island
Constructor. Initialize the island, make the Arraylist
chameleons empty.
AddChameleon
Create a new chameleon object, and then add it to the
Arraylist chameleons. The color and gender of the chameleon are given as input parameters.
AddChameleonRandom
Create a new chameleon object with random color/gender, and then add it to the Arraylist
chameleons.
Explanation / Answer
package HW6;
import java.util.*;
public class Chameleon{
int Color, int Gender, int Colors;
Chameleon()
{
Color=1;
Gender=1; // 1 is male, 2 is female
Colors=1;
}
Chameleon(int Color, int Gender, int Colors)
{
//implement
}
Chameleon(int Colors)
{
//implement
}
//Create getters and setters for Chameleon class
}
public class Island {
private ArrayList<Chameleon> chameleons;
public Island(){
chameleons = new ArrayList<Chameleon>();
}
public Chameleon AddChameleon(int Color, int Gender, int Colors){
Chameleon ch=new Chameleon(Color,Gender,Colors)
chameleons.add(ch);
return ch;
}
public Chameleon AddChameleonRandom(int Colors){
Random r = new Random(); //For Gender
int Low = 1;
int High = 2;
int Result = r.nextInt(High-Low) + Low;
int Result2=r.nextInt(); //For color
Chameleon c = new Chameleon(Result2,Result,Colors);
chameleons.add(c);
return c;
}
// Simulate the event that the i-the and j-th chameleon meet with each other.
public void Meet(int i, int j){
if(chameleons[i].Gender==chameleons[j].Gender)
{
if(chameleons[i].Gender==1)
{
chameleons[i].Color=chameleons[i+1].Color; //Maintain the case when i+1 goes above the array size, change it to the zero in that case
chameleons[j].Color=chameleons[j+1].Color;
}
else if(chameleons[i].Gender==2)
{
chameleons[i].Color=chameleons[i-1].Color; //Maintain the case when i-1 goes zero, change it to the array length-1 in that case
chameleons[j].Color=chameleons[j-1].Color;
}
}
// else we do nothing, as color wont change in that case
}
public void MeetRandom(){
Random rnd = new Random();
int i,j;
i = j = 0;
// randomly select two non-equal integers between 0 and chameleons.size().
while( i == j){
i = rnd.nextInt(chameleons.size());
j = rnd.nextInt(chameleons.size());
Meet(i,j);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.