Need help implementing this Java class Randomly creates one of 6 dark rainbow co
ID: 3825782 • Letter: N
Question
Need help implementing this Java class
Randomly creates one of 6 dark rainbow colors: Blue, Green, Yellow, Orange, Red, Purple, Blue.
RainbowColorGenerator shall randomly return (with uniform probability) the following six
colors. Specifically, as a percentage of the possible amount of Red, Green or Blue, each Color
shall be constructed with the following:
Red: 25%(R) 0%(G) 0%(B)
Orange: 25%(R) 12.5% 0%(B)
Yellow: 25%(R) 25%(G) 0%(B)
Green: 0%(R) 25%(G) 0%(B)
Blue: 0%(R) 0%(G) 25%(B)
Purple: 25%(R) 0%(G) 25%(B)
It is up to you to find and use the appropriate constructor in Color to achieve this.
Constructor Summary
Method Summary
Makes a new color.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
RainbowColorGenerator
Method Detail
createColor
Description copied from interface: ColorGenerator
Makes a new color. For style, subclasses should create multiple colors.
Specified by:
createColor in interface ColorGenerator
Returns:
A Color
Constructors Constructor and Description RainbowColorGenerator()Explanation / Answer
import java.awt.Color;
/**
*
* @author Sam
*/
public class RainbowColorGenerator implements ColorGenerator {
/*Create float arrays representing Red, Green and Blue content of colors defined in specs*/
private final float red[] = {0.25f,0.25f,0.25f,0,0,0.25f};
private final float green[] = {0,0.125f,0.25f,0.25f,0,0};
private final float blue[] = {0,0,0,0,0.25f,0.25f};
@Override
public Color createColor() {
int colorSelect = (int)Math.round(Math.random()*5); //generate random index whose range is 0 to 5
Color color = new Color(red[colorSelect], green[colorSelect], blue[colorSelect]); //create color with given content
return color;
}
}
interface ColorGenerator {
Color createColor();
}
Here you go champ. I tried my best to keep the code as simple as possible. I have also commented parts of the code to make things easy. If incase you are still facing any trouble, please feel free to comment below. I shall be glad to help you :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.