A Color class has a constructor that accepts three integer parameters : red, gre
ID: 3545904 • Letter: A
Question
A Color class has a constructor that accepts three integer parameters : red, green and blue components in the range of 0 ... 255 with 0 representing no contribution of the component and 255 being the most intense value for that component .
The effect of full or semi-transparency can be achieved by adding another component to the color called an alpha channel. This value can also be defined to be in the range of 0...255 with 0 representing opaque or totally non-transparent, and 255 representing full transparency.
Define a class AlphaChannelColor to be a subclass of the Color class . AlphaChannelColor should have the following: - an integer instance variable alpha - a constructor that accepts four parameters : red, green, blue and alpha values in that order. The first three should be passed up to the Color class ' constructor , and the alpha value should be used to initialize the alpha instance variable . - a getter (accessor) method for the alpha instance variable
Explanation / Answer
Hi,
The following is an executable code with the specified classes.
class Color
{
int red, green, blue;
Color(int color1, int color2, int color3)
{
red = color1;
green = color2;
blue = color3;
}
public void displayColor()
{
if (red == 0)
System.out.println("Contibution of Red color is nill.");
else if (red == 255)
System.out.println("Contibution of Red color is complete.");
else if (red > 0 || red < 255)
System.out.println("Contibution of Red color is partial.");
if (green == 0)
System.out.println("Contibution of green color is nill.");
else if (green == 255)
System.out.println("Contibution of green color is complete.");
else if (green > 0 || green < 255)
System.out.println("Contibution of green color is partial.");
if (blue == 0)
System.out.println("Contibution of blue color is nill.");
else if (blue == 255)
System.out.println("Contibution of blue color is complete.");
else if (blue > 0 || blue < 255)
System.out.println("Contibution of blue color is partial.");
}
}
public class AlphaChannelColor extends Color
{
int alpha;
public int getAlpha()
{
return alpha;
}
AlphaChannelColor(int red, int green, int blue, int alphaValue)
{
super(red, green, blue);
this.alpha = alphaValue;
}
public void displayAlpha()
{
if (alpha == 0)
System.out.println("Alpha contribution resulted in" +
" opaque or non transparent.");
else if (alpha > 0 || alpha < 255)
System.out.println("Alpha contribution resulted in" +
" partial transparency.");
else if (alpha == 255)
System.out.println("Alpha contribution resulted in" +
" complete transparency.");
}
public static void main(String[] args)
{
AlphaChannelColor acc = new AlphaChannelColor(255, 150, 255, 100);
acc.displayColor();
acc.displayAlpha();
}
}
Hope this will help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.