James decides to visit a small tropical island during his spring break. On the i
ID: 3680938 • 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 Chameleon. As the name implies this class represents the color changing chameleons. The following table tells you what variables and methods should Chameleon have. You can add other variables and methods which you deem necessary.
Class: Chameleon
Type
Name
Description
Variable
color
Current color of the chameleon. Can be represented as an integer. The actual color does not matter since we do not require you to draw the chameleon.
The variable must be private.
total_color
Total number of possible colors a chameleon can turn into.
gender
Can be an integer where 0 represents male and 1 represent female.
Method
(All methods are public)
Chameleon
Constructor.
You are required to provide two different constructors.
Constructor 1: Initialize the chameleon and set it to a given color/gender.
Constructor 2: Initialize the chameleon and set it to a random
color/gender.
Note that constructors all have the same method name, but take different input parameters.
NextColor
When the method is called, make the chameleon change to the
next color.
PrevColor
When the method is called, make the chameleon change to the
previous color.
GetColor
Return current color of the chameleon.
SetColor
Set the current color to a given color.
package HW6;
// this class represents the color changing chameleons.
public class Chameleon {
// Add necessary variables and methods according to the homework requirement.
// Basic skeleton is provided to you.
private int color; // current color, starts from 1. value <= 0 not allowed.
public int total_color; // total number of possible colors.
public int gender; // 0 = male, 1 = female. Other values not allowed.
// Feel free to add other variable you deem necessary
// This construct creates a Chameleon object with random color/gender
// Parameter: Colors = The total number of colors the chameleon can change into.
public Chameleon(int Colors){
// Finish this method
}
// This construct creates a Chameleon object with given color/gender
// Parameter: Color = Inital color of the chameleon
// Gender = Inital gender of the chameleon
// Colors = The total number of colors the chameleon can change into.
public Chameleon(int Color, int Gender, int Colors){
// Finish this method
}
// This method turn the chameleon into the next color
// It is already finished for you.
public void NextColor(){
color = (color+1)%total_color;
}
// This method turn the chameleon into the previous color
public void PreveColor(){
// Finish this method
}
// This method is already finished for you.
public int GetColor(){
return color;
}
public void SetColor(int Color){
// Finish this method
}
}
Class: Chameleon
Type
Name
Description
Variable
color
Current color of the chameleon. Can be represented as an integer. The actual color does not matter since we do not require you to draw the chameleon.
The variable must be private.
total_color
Total number of possible colors a chameleon can turn into.
gender
Can be an integer where 0 represents male and 1 represent female.
Method
(All methods are public)
Chameleon
Constructor.
You are required to provide two different constructors.
Constructor 1: Initialize the chameleon and set it to a given color/gender.
Constructor 2: Initialize the chameleon and set it to a random
color/gender.
Note that constructors all have the same method name, but take different input parameters.
NextColor
When the method is called, make the chameleon change to the
next color.
PrevColor
When the method is called, make the chameleon change to the
previous color.
GetColor
Return current color of the chameleon.
SetColor
Set the current color to a given color.
Explanation / Answer
Here is the code completed for you:
package HW6;
import java.util.*;
// this class represents the color changing chameleons.
public class Chameleon {
// Add necessary variables and methods according to the homework requirement.
// Basic skeleton is provided to you.
private int color; // current color, starts from 1. value <= 0 not allowed.
public int total_color; // total number of possible colors.
public int gender; // 0 = male, 1 = female. Other values not allowed.
// Feel free to add other variable you deem necessary
// This construct creates a Chameleon object with random color/gender
// Parameter: Colors = The total number of colors the chameleon can change into.
public Chameleon(int Colors){
total_color = Colors;
color = new Random().nextInt(Colors) + 1;
gender = new Random().nextInt(2);
// Finish this method
}
// This construct creates a Chameleon object with given color/gender
// Parameter: Color = Inital color of the chameleon
// Gender = Inital gender of the chameleon
// Colors = The total number of colors the chameleon can change into.
public Chameleon(int Color, int Gender, int Colors){
color = Color;
total_color = Colors;
gender = Gender;
// Finish this method
}
// This method turn the chameleon into the next color
// It is already finished for you.
public void NextColor(){
color = (color+1)%total_color;
}
// This method turn the chameleon into the previous color
public void PreveColor(){
color = color - 1;
if(color == 0)
color = total_color;
// Finish this method
}
// This method is already finished for you.
public int GetColor(){
return color;
}
public void SetColor(int Color){
color = Color;
// Finish this method
}
}
If you need any refinements, just get back to me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.