Problem with creating a new object in Java and moving to setMethod

advertisements

I'm trying to create a new object using the parameters passed in, then use that new object to place in the setCarColor method listed below. But my setCarColor in my constructor is giving an error for my clv variable. It says "Can not find symbol". The clv is my variable from the CarColor class. I'm not sure if it is because the parameters being passed in(rdIn, grnIn, bluIn)are integers or what? Does anyone have any ideas please?

Best Regards,

public abstract class Vehicle
{
  private String shapeId;
  CarColor carColor;//CarColor data member from the ColorValue.java class

public Shape(String bodyid, int rd, int grn, int ble)
{
 CarColor clv = new CarColor(rdIn, grnIn, bluIn);
 setCarColor(clv(rd, grn, ble));// <---error here
}

private CarColor getCarColor()
{
    return carColor;
}

private void setCarColor(int redIn, int blueIn, int greenIn)
{
if (redIn == 0 || blueIn == 0 || greenIn == 0 )
{
    System.out.println("The value entered in is null.  Please try again ");
    System.exit(-1);
}

}


This line is nearly fine:

ColorValue clv = new ColorValue(rdIn, grnIn, bluIn);

... although it doesn't populate the colorValue field, which is what you might have been expecting, and you don't actually have rdIn, grnIn and bluIn variables. Did you mean rd, grn, ble? (It helps if you don't contract names like this, by the way.)

But this line is broken in two ways:

setColorValue(clv(rd, grn, ble));

Firstly, it's trying to call a method called clv. You don't have such a method. You've got a variable called clv, but you don't "call" a variable.

The second problem is that if you really meant this:

setColorValue(clv);

then you'd be using incorrect arguments - setColorValue doesn't have one parameter of type ColorValue, it has three parameters, all ints.

Unfortunately it's not clear what you're attempting to do, so it's hard to advise you. Perhaps you mean this:

public abstract class Geometry
{
    private String shapeId;
    private ColorValue colorValue;

    public Shape(String shapeId, int red, int green, int blue)
    {
        this.shapeId = shapeId;
        setColorValue(red, green, blue);
    }

    public ColorValue getColorValue()
    {
        return colorValue;
    }

    // Note the consistent order of the parameters - always red, green, blue.
    public void setColorValue(int red, int green, blue)
    {
        // Don't use System.exit() in the middle of a method! An exception
        // is the idiomatic way of reporting bad arguments.
        if (red == 0 || blue == 0 || green == 0)
        {
            throw new IllegalArgumentException("red green and blue must be non-zero");
        }
        colorValue = new ColorValue(red, green, blue);
    }
}