Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

James decides to visit a small tropical island during his spring break. On the i

ID: 3680566 • 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 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

import java.util.Scanner;

public class Chemeleons
{
private int colors;
private int gender;   
Chemeleons(int colors,int gender)
{
    this.colors=colors;
    this.gender=gender;
}
Chemeleons()
{
colors=1;
gender=0;
}

public static String nextColor(int c1,int c2,int g1,int g2)
{
int temp1 = 0,temp2=0;
    if(g1==0&&g2==0)
{
if(c1==1)
    {
    temp1=2;
    }
else if(c1==2)
    {
    temp1=3;
    }
else if(c1==3)
    {
    temp1=1;
    }
if(c2==1)
    {
    temp2=2;
    }
else if(c2==2)
    {
    temp2=3;
    }
else if(c2==3)
    {
    temp2=1;
    }
return Integer.toString(temp1)+" "+Integer.toString(temp2);
}
   
    else if(g1==1&&g2==1)
    {
    if(c1==1)
        {
        temp1=0;
        }
    else if(c1==2)
        {
        temp1=1;
        }
    else if(c1==3)
        {
        temp1=2;
        }
    if(c2==1)
        {
        temp2=0;
        }
    else if(c2==2)
        {
        temp2=1;
        }
    else if(c2==3)
        {
        temp2=2;
        }
    return Integer.toString(temp1)+" "+Integer.toString(temp2);
    }
    else if((g1==1&&g2==0)||(g1==0&&g2==1))
    {
        return Integer.toString(c1)+" "+Integer.toString(c2);
           
    }
    return null;
}
public int getColors() {
    return colors;
}
public void setColors(int colors) {
    this.colors = colors;
}


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int c1=0,c2=0,g1=0,g2 = 0;
System.out.println("Enter Color and Gender of the 2 chameleon sequencially... color1 colo2 gender1 gender2");
c1=sc.nextInt();
c2=sc.nextInt();
g1=sc.nextInt();
g1=sc.nextInt();
    System.out.println(nextColor(c1,c2,g1,g2));
    }

}