question archive In many languages (including Java), you can form a color by specifying certain amounts of red, green, and blue, with each color's amount ranging from 0% to 100%
Subject:Computer SciencePrice:5.87 Bought7
In many languages (including Java), you can form a color by specifying certain amounts of red, green, and blue, with each color's amount ranging from 0% to 100%. For example, to specify purple, you could use this code
rgb(100%, 60%, 100%)
The rgb stands for red, green, and blue. With 100% for red and blue, the resulting color is purple. In the following Rgb and RgbDriver class skeletons, replace the <insert...> lines with appropriate code such that the program operates properly. More specifically:
rgb(100%, 60%, 100%)
public class Rgb { private int red; private int green; private int blue; <insert setRed method definition here> public Rgb setGreen(int green) { this.green = green; return this; } // end setGreen public Rgb setBlue(int blue) { this.blue = blue; return this; } // end setBlue public void display() { System.out.printf("rgb(%d%%, %d%%, %d%%)n", this.red, this.green, this.blue); } // end display } // end Rgb class public class RgbDriver { public static void main(String[] args) { Rgb rgb = new Rgb(); <insert chained method calls here> } } // end RgbDriver class
Answer:
I write the method named setRed(int), that set the value of red, and return the object itself.
And in main method i write the method chaining, like this,
rgb.setRed(100).setGreen(60).setBlue(100).display();
This is work properly, you can see, i provide the source code and the output in explanation.
If you have any queries, comment it.
Step-by-step explanation
Source Code:
public class RgbDriver { public static void main(String[] args) { Rgb rgb = new Rgb(); rgb.setRed(100).setGreen(60).setBlue(100).display(); } } // end RgbDriver class public class Rgb { private int red; private int green; private int blue; public Rgb setRed(int red) { this.red = red; return this; } // end setRed public Rgb setGreen(int green) { this.green = green; return this; } // end setGreen public Rgb setBlue(int blue) { this.blue = blue; return this; } // end setBlue public void display() { System.out.printf("rgb(%d%%, %d%%, %d%%)\n", this.red, this.green, this.blue); } // end display } // end Rgb class
OUTPUT:
PFA