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%

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:

  1. In the Rgb class, provide a method definition for the setRed method such that setRed can be called as part of a method-call chain.
  2. In the RgbDriver class, provide a single statement that chains calls to the setRed, setGreen, setBlue, and display For your method-call arguments, pass 100 to setRed, 60 to setGreen, and 100 to setBlue. With those argument values, your method-call-chaining statement should print this:

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

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

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

Related Questions