Question
(IN JAVA): Given this code (copy below): ------------------------------------------------------------------------------------------ public class Colour { static final Colour RED = new Colour(255, 0, 0); static final Colour BLUE
(IN JAVA):
Given this code (copy below):
------------------------------------------------------------------------------------------
public class Colour { static final Colour RED = new Colour(255, 0, 0); static final Colour BLUE = new Colour(0, 0, 255); private int red; private int green; private int blue; public static void main(String[] args) { /** * //run this to see if toString method is working. Output should be: #ff0000 * * Colour c = new Colour(255, 0, 0); * c.toString(); * System.out.println(c); */ /** * //run this to see if the add method is working. Output should be: Color(25,100,255) * * Colour c = new Colour(25,100,500); * Colour newColour = c.add(Colour.BLUE); * return newColour; */ } public Colour(int red, int green, int blue) { if (blue < 0) blue = 0; if (blue > 255) blue = 255; this.blue = blue; if (green < 0) green = 0; if (green > 255) green = 255; this.green = green; if (red < 0) red = 0; if (red > 255) red = 255; this.red = red; } }
------------------------------------------------------------------------------------------
Create a method called toString that takes no parameters. This method returns a string representation of the color in the form #rrggbb where rr, gg, and bb are the red, green, and blue component values expressed as hexadecimal digits. [hint: use Integer.toHexString()] to convert an integer value to a hexadecimal.
&
Create a method called add that takes a Color object as a parameter & returns a new Color value that is the sum of this color and the color specified as a parameter. The value of the red components of the two colors are added (The same for the green and blue). Any result greater than 255 is set to 255.
[Examples of what is being inputted & the expected output can be found in the main method]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started