获取输入(0,0,0)而不是(50,40,30)[Java]

时间:2014-11-25 17:47:35

标签: java

我输错了。我将3个代码放在这里,它们非常基本,因为我刚开始学习。 (第三个代码是测试人员)

问题:在测试人员中我得到了那个输入(0,0,0),但我需要得到(50,40,30)

我认为主要问题在于:

    public void setColor (RGBColor color)
{
}

我试过玩它,而且我没有比以下更好的解决方案:

    _color = new RGBColor(color);
    _color.setRed(color.getRed());
    _color.setGreen(color.getGreen());
    _color.setBlue(color.getBlue());

RGBColor类。

  /**
 * This program is used to represent 3 Colors: Red, Green, Blue. (RGB)
 * These colors hold values between 0 and 255.
 * 
 * 
 * @Author doesn't matter ;)
 */
public class RGBColor
{
    /**
     * attributes: red, green and blue component of a color.
     */
    private int _red,_green,_blue;

    /**
     * final variables.
     */
    private final int MAX_VALUE = 255,MIN_VALUE = 0;
    private final double THIRTY_PERCENT = 0.3,FIFTY_NINE_PERCENT = 0.59,ELEVEN_PERCENT = 0.11;                   

    /**
     * Consctructor which gets 3 colors (RGB), we check here if their range is valid (0 - 255), if not we assign black to it.
     *
     *  @param red - The red color component value.
     *  @param green - The green color component value.
     *  @param blue - The blue color component value
     */
    public RGBColor(int red, int green, int blue)
    {
        if(isValid(red,green,blue))
        {
            _red   = red;
            _green = green;
            _blue  = blue;
        }
        else
            doBlack();
    }


/**
 * Construct a black RGBColor. i.e. red = green = blue = 0
 */
public RGBColor()
{
    doBlack();
}

/**
 * Construct a new RGBColor which is a copy of the given color. 
 * 
 * @param other - The RGBColor to copy.
 */
public RGBColor(RGBColor other)
{
    _red   = other._red;
    _green = other._green;
    _blue  = other._blue;
}

/**
 * Returns the Red component of RGBColor.
 * 
 * @return The red color component value of this RGBColor.
 */
public int getRed()
{
    return _red;
}

/**
 * Returns the Green component of RGBColor.
 * 
 * @return The green color component value of this RGBColor.
 */
public int getGreen()
{
    return _green;
}

/**
 * Returns the blue component of RGBColor.
 * 
 * @return The blue color component value of this RGBColor.
 */
public int getBlue()
{
    return _blue;
}

/**
 * Sets the red color component value of this RGBColor, only if the color range is valid (0-255).
 * 
 * @param num - The red color component value to set.
 */
public void setRed(int num)
{
    if(isValid(num))
        _red = num;
}

/**
 * Sets the green color component value of this RGBColor, only if the color range is valid (0-255).
 * 
 * @param num - The green color component value to set.
 */
public void setGreen(int num)
{
    if(isValid(num))
        _green = num;
}

/**
 * Sets the blue color component value of this RGBColor, only if the color range is valid (0-255).
 * 
 * @param num - The blue color component value to set.
 */
public void setBlue(int num)
{
    if(isValid(num))
        _blue = num;
}

/**
 * Compares the 3 RGB colors, returns true if all are equal.
 * 
 * @return true if the RGBColors are equal; false otherwise.
 */
public boolean equals(RGBColor other)
{
    return ((_red   == other._red)   && 
            (_green == other._green) && 
            (_blue  == other._blue));
}


/**
 * Changes this color to be a mix of this and other RGBColors, It simply takes this color and other RGBColor and makes and average of them.
 * For example (255,0,0) and (0,0,255) becomes: (127,0,127). Note that it returns integer numbers and not fraction numbers.
 * 
 * @param other is the other color.
 */
public void mix(RGBColor other)
{
    _red   = (_red + other._red) / 2;
    _green = (_green + other._green) / 2;
    _blue  = (_blue + other._blue) / 2;
}

/**
 * Returns the grayscale value of this RGBColor.
 * Grayscale is defined by taking Red multipled by 30% plus green multiplied by 59% plus blue multipled by 11%.
 * 
 * @return The grayscale value of this RGBColor, a double number.
 */
public double convertToGrayscale()
{
    return ((THIRTY_PERCENT * _red) + (FIFTY_NINE_PERCENT * _green) + (ELEVEN_PERCENT * _blue));
}


/**
 * Inverts the color of RGBColor, every spot is reduced relative to 255. For example: (10,20,30) becomes (245,235,225).
 */
public void invert()
{
    _red   = (MAX_VALUE - _red);
    _green = (MAX_VALUE - _green);
    _blue  = (MAX_VALUE - _blue);
}

/**
 * Here we check if the color number was entered correctly.
 * It has to be an integer (whole number) between 0-255.
 * 
 * @param nums - a component value, should be the number between 1-4
 * @param return - return true if the number is between 1-4, false otherwise.
 */
private boolean isValid(int nums)
{
    return ((nums >= MIN_VALUE) && (nums <= MAX_VALUE));
}

/**
 * Here we check if the color number was entered correctly.
 * It has to be an integer (whole number) between 0-255.
 * 
 * @param red - the red component
 * @param green - the green component
 * @param blue - the red component
 * @param return true if values are correct, false otherwise.
 */
private boolean isValid(int red, int green, int blue)
{
    return ((red <= MAX_VALUE && red >= MIN_VALUE && 
            green <= MAX_VALUE && green >= MIN_VALUE &&
            blue <= MAX_VALUE && blue >= MIN_VALUE));
}
/**
 * Returns RGB color string triplet with numbers between 0-255, i.e. (0,127,127)
 */
public String toString()
{
    return ("(" + _red + "," + _green + "," + _blue + ")");
}

/**
 * RGBColor will become the color Black. (0,0,0)
 */
private void doBlack()
{
    _red = _green = _blue = 0;
}

}

LightBulb Class。

    /**
 * In this program we use _color to represent the color of the bulb
 * And we use _switchedOn to represent whether the bulb is turned on or off.
 * 
 * @author 
 * @date 20/11/2014.
 */
public class LightBulb
{
    /**
      instance private variables of the lightbulb's color and the switch state.
    */ 
    private static RGBColor _color;
    private boolean _switchedOn;
    private int ZERO = 0;
    private int MAX = 255;

    /**
     * Construct a new LightBulb with the given color component values. 
     * Check if atleast one of the color isn't in the given value range (0-255)
     * if not in the range, the default color is black (0,0,0).
     */
    public LightBulb(int red, int green, int blue)
    {
        if(isTrue(red,green,blue))
        _color = new RGBColor(red,green,blue);
        else
        _color = new RGBColor(ZERO,ZERO,ZERO);
        _switchedOn = false;
    }
    /**
     * Construct a new LightBulb which is a copy of the given bulb, and turn switch off.
     */
   public LightBulb(RGBColor color)
   {
       _color = new RGBColor(color);
        _switchedOn = false;
   }
    /**
     * Construct a new LightBulb which is a copy of the given LightBulb
     */
    public LightBulb (LightBulb other)
    {
     _color = new RGBColor(other._color);
     _switchedOn = other._switchedOn;
    }
    //Return the current color of the bulb.
    public RGBColor getColor()
    {
        return new RGBColor(_color);
    }
    //Sets the color of the bulb.
    public void setColor (RGBColor color)
    {
        _color = new RGBColor(color);
    }
    //Checks if the bulb is on, if it is - returns true. else, returns false.
    public boolean isSwitchedOn()
    {
        if(_switchedOn)
        return true;
        else
        return false;
    }

    //If the lightbulb is turned on, it turns it off. else,if it is off - it turns it on.
    public void switchLight()
    {
        if(_switchedOn)
        _switchedOn = false;
        else
        _switchedOn = true;
    }
//Returns a string which prints a triplet of the color in numbers of range (0-255), and the switch state of the bulb.
// e.g. (255,255,0) On , e.g. (255,127,0) Off
public String toString()
{
    _color = new RGBColor();
    return ("(" +_color.getRed()+ "," +_color.getGreen()+ "," + _color.getBlue() + ")" + " " + switchState());     
}
//If the switch is on - returns a string "On". else, if it is off returns a string "Off".
public String switchState()
{
    if(_switchedOn)
        return ("On");
    else
        return ("Off");
}
public boolean isTrue(int red, int green, int blue)
{
    if( red >= ZERO && red <= MAX &&
        green >= ZERO && green <= MAX &&
        blue >= ZERO && blue <= MAX )
        return true;
        else
        return false;
}

}

LightBulb测试员课程。

/**
 * Write a description of class Tester here.
 * 
 * @author  
 * @version (a version number or a date)
 */
public class LightBulbTester
{
    /**
     * 
     */
    public static void main(String[] args)
    {
        // Create two light bulb objects
        LightBulb l1 = new LightBulb(127,0,127);
        LightBulb l2 = new LightBulb(new RGBColor(127,0,127));
        LightBulb l3 = new LightBulb(l2);

        // Print (test the get method)
        System.out.println("Welcome to Light Bulb tester");
        System.out.println("1) color of light object is " + l1.getColor());

    // Test the set method
    l1.setColor(new RGBColor(50,40,30));
    System.out.println("2) The new color of light is :" + l1);

    // Test isSwitchedOn
    System.out.println("3) Light object is switched on? " + l1.isSwitchedOn());

    // Now switch on
    l1.switchLight();
    System.out.println("4) Light after switchLight():" + l1);
    System.out.println("Good luck!");
}
}

1 个答案:

答案 0 :(得分:3)

在此代码中 -

public String toString()
{
    _color = new RGBColor();
    return ("(" +_color.getRed()+ "," +_color.getGreen()+ "," + _color.getBlue() + ")" + " " + switchState());     
}

您始终输出 RGBColor对象的值,这将始终为您提供黑色。您可能不想在此处设置_color的值;只需删除该行(或用空检查替换它)。

另外,从static属性中删除_color限定符。你不想分享所有灯泡之间的价值!