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

We consider a class Person describing a person by a sequence of 4 bits of 0s and

ID: 3625804 • Letter: W

Question

We consider a class Person describing a person by a sequence of 4 bits of 0s and 1s; the first 2 bits represents the eye color of a person and the 2 last bits represent the hair color of a person. Write a method that returns the eye color of a person.


public class Person {
private String geneString;
...
}

This question will only focus on the eye color of a person.
- If the first 2 bits of the sequence are 00, the person has black eyes.
- If the first 2 bits of the sequence are 01, the person has brown eyes.
- If the first 2 bits of the sequence are 10, the person has green eyes.
- If the first 2 bits of the sequence are 11, the person has blue eyes.

The method will be called eyeColor and must:
- Be an instance method;
- Be declared public;
- Take no parameters;
- Return the eye color of a person as a String (the color is written in lower case letters); and
- Throw an IllegalStateException if the person is not described by a sequence of 4 bits of 0s and 1s.

Note: If necessary, you can use the methods of java.util.regex.* to validate geneString.

Explanation / Answer

public class Person { private String geneString; Person(String gene) {geneString=gene;} public String eyeColor() { String eyeCol=""; int eye=Integer.parseInt(geneString.substring(0,2)); System.out.println(eye); eye=(eye%10)+(eye/10)*2; //convert to decimal System.out.println(eye); switch(eye) { case 0: eyeCol="black"; break; case 1: eyeCol="brown"; break; case 2: eyeCol="green"; break; case 3: eyeCol="blue"; } return eyeCol; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote