Thinking in Java Fourth Edition Bruce Eckel Chapter 1 Exercise 11

Exercise 11: Turn the AllTheColorsOfTheRainbow example into a program that compiles and runs.

Solution:

public class Rainbow
{
    public static void main(String[] args)
    {
        AllTheColorsOfTheRainbow atc = new AllTheColorsOfTheRainbow();
        System.out.println("atc.anIntegerRepresentingColors = " + atc.anIntegerRepresentingColors);
        atc.changeColor(7);
        atc.changeTheHueOfTheColor(77);
        System.out.println("After color change, atc.anIntegerRepresentingColors = " + atc.anIntegerRepresentingColors);
        System.out.println("atc.hue = " + atc.hue);
    }
}

class AllTheColorsOfTheRainbow
{
    int anIntegerRepresentingColors = 0;
    int hue = 0;
    void changeTheHueOfTheColor(int newHue)
    {
        hue = newHue;
    }
    int changeColor(int newColor)
    {
        return anIntegerRepresentingColors = newColor;     
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *