Ask Question

A Color class has three int color component instance variables: red, green, and blue. Write a toString method for this class. It should return a String consisting of the three color components (in the order red, green, blue) within parentheses, separated by commas, with a '#' prefix, e. g. # (125,30,210)

+3
Answers (1)
  1. 1 March, 21:19
    0
    public String toString () {

    return "# (" + red + "," + green + "," + blue + ") ";

    }

    Explanation:

    The code:

    public String toString () {

    return "# (" + red + "," + green + "," + blue + ") ";

    }

    Is a tostring java code, and it is so easy to write one, as compare to other programming languages

    Writing a tostring method returns a strinb representation of an object in Java. Normally, the toString method returns the name of the object's class plus its hash code.

    This code creates a new colour object; then the result of its toString method is printed to the console.

    Tostring method can be override. The default implementation of toString isn't very useful in most situations. So, one can just override it.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “A Color class has three int color component instance variables: red, green, and blue. Write a toString method for this class. It should ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers