Suppose there is a class in Java called RGB. This class is used to store the amo
ID: 3691893 • Letter: S
Question
Suppose there is a class in Java called RGB. This class is used to store the amount of red, blue, and green light used (these are int values) to paint a color on the screen using graphics. Determine whether each of the method signatures below should be a class method or an instance method, based on the signature. Do not try to get this by looking at the Color class in Java (The method signatures are changed).
a) void setRed(int red, RGB color)
The method changes the amount of red stored in the RGB object color to red.
b) RGB setGreen(int green)
The method creates a new RGB object that has the given amount of green and no red or blue components.
c) int[] toArray(RGB color)
The method converts the RBG object color to an array of three integers (red at index 0, green at index 1 and blue at index 2).
d) int[] toArray()
The method converts this RGB object to an array of three integers (red at index 0, green at index 1 and blue at index 2).
e) void setBlue(RGB color, int blue)
The method changes the amount of blue in the RGB object color.
f) int getBlue()
The method returns the amount of blue in this RGB object.
g) void lighter(RGB color)
The method makes this RGB object color lighter.
h) void decode (String color)
This method takes a String with the following format: “(127, 53, 27)” and sets the implicit object’s colors. The String above would change the RGB object to have red = 127, green = 53, and blue = 27
i) void darker()
The method makes this RGB object darker.
j) void setRGBFromHSV(int hue, int saturation, int value)
The method changes this RGB object to have red, blue, and green values converted from the hue, saturation, and value. Hue, saturation, and value is another way of describing a color.
Explanation / Answer
a) void setRed(int red, RGB color)
The method changes the amount of red stored in the RGB object color to red.
Class method.
As we already have the object of which we need to change the color red, we don't need an instance. Hence, this can be a class method.
b) RGB setGreen(int green)
The method creates a new RGB object that has the given amount of green and no red or blue components.
Class method.
Since we are creating a new RGB object, we don't need an instance on which this method needs to be called.
c) int[] toArray(RGB color)
The method converts the RBG object color to an array of three integers (red at index 0, green at index 1 and blue at index 2).
Class method.
The input itself is an object, so we don't need an instance of another object for calling this method.
d) int[] toArray()
The method converts this RGB object to an array of three integers (red at index 0, green at index 1 and blue at index 2).
Instance method
There is no object as input argument, so this method must operate on the instance using which this method is called.
e) void setBlue(RGB color, int blue)
The method changes the amount of blue in the RGB object color.
Class method
The input is an object, hence we don't need an instance on which this function can be called.
f) int getBlue()
The method returns the amount of blue in this RGB object.
Instance method
There is no object as input argument, so this method must operate on the instance using which this method is called.
g) void lighter(RGB color)
The method makes this RGB object color lighter.
Class method
The input is an object, hence we don't need an instance on which this function can be called.
h) void decode (String color)
This method takes a String with the following format: “(127, 53, 27)” and sets the implicit object’s colors. The String above would change the RGB object to have red = 127, green = 53, and blue = 27
Instance method
There is no object as input argument, so this method must operate on the instance using which this method is called.
i) void darker()
The method makes this RGB object darker.
Instance method
There is no object as input argument, so this method must operate on the instance using which this method is called
j) void setRGBFromHSV(int hue, int saturation, int value)
The method changes this RGB object to have red, blue, and green values converted from the hue, saturation, and value. Hue, saturation, and value is another way of describing a color.
Instance method.
There is no object as input argument, so this method must operate on the instance using which this method is called
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.