1

Lets say I have a class of type Color. I want to create some child classes, Red, Green, and Blue. Then, in a completely separate class I have a List called Rainbow of type Color. I want to be able to place all types of colors (Red, Greed, Blue) into the list Rainbow that way later I can search the list Rainbow by a type of color to see if it contains one of those colors. Here is some pseudocode of what I'm wanting to accomplish:

class Color(){}
class Red extends Color(){}
class Blue extends Color(){}
class Green extends Color(){}
class JustSomeClass(){
    List<Color> Rainbow;
    Rainbow.add(new Red());
    Rainbow.add(new Blue());
    Rainbow.add(new Green());

    public Color getTypeOfColor(typeOfColor){
        for(Color c : Rainbow){
            if(c.getType().equals(typeOfColor)){
                return c;
            }
        }
    }
}

I'm not that familiar enough with Java to know a good way to go about accomplishing this. Can someone point me in the right direction?

5
  • Pertty much exactly the code you wrote will work. What part seems to concern you? Commented Aug 23, 2014 at 19:26
  • What does Color do? Could you maybe use an enum and a switch? Or, otherwise, a Visitor Pattern would be better. Commented Aug 23, 2014 at 19:26
  • 3
    Move Rainbow.add(new Red()); calls inside the constructor or method. Commented Aug 23, 2014 at 19:26
  • 1
    Without more details it is hard for us to tell you if this is a good approach. How are you going to use this? Why do you need to check the color? Can't you check the color using a hash instead? Commented Aug 23, 2014 at 19:31
  • Downvoter Let me know what is wrong with Map<String,Color>? Commented Aug 23, 2014 at 19:32

3 Answers 3

1

There's a few things you should consider. Collections (like List) use both the hashCode() method and the equals() method to do their work. You must override those methods for your class Color so that the Collection knows what to do.

After that, then you should think about what Color really does. Is each color unique? Can your Rainbow have more than one Color in it? If not (each color Red is the same as all colors) use a Set. If there's more than one flavor (more than one concrete type of red) then your list is probably better.

Here's the Set one. When run, it prints false, then true.

public class RainbowTest {
   public static void main(String[] args) {
      Set<Color> rainbow = new HashSet<>();
      rainbow.add(new Red(.1f));
      rainbow.add(new Blue(.1f));
      rainbow.add(new Green(.1f));
      System.out.println(rainbow.contains(new Blue(.5f)));
      System.out.println(rainbow.contains(new Red(.1f)));
   }
}

abstract class Color {
   private final float intensity;

   public Color(float intensity) {
      this.intensity = intensity;
   }

   public float getIntensity() {
      return intensity;
   }

   @Override
   public int hashCode() {
      int hash = 3;
      hash = 83 * hash + Float.floatToIntBits(this.intensity);
      return hash;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final Color other = (Color) obj;
      if( this.intensity != other.intensity )
         return false;
      return true;
   }

}

class Red extends Color {

   public Red( float intensity ) {
      super( intensity );
   }

}
class Green extends Color {

   public Green( float intensity ) {
      super( intensity );
   }

}
class Blue extends Color {

   public Blue( float intensity ) {
      super( intensity );
   }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain further what exactally you are doing with hashCode?
Actually, that's a large question, probably one you should post here as a question by itself. You can see a good example of how Java's hash code works at Wikipedia.. There's also numerous websites with additional information. In short, the answer is long and better suited to its own question.
0

A generic <colour> won't allow it's sub class Use it as <T extends color>

1 Comment

Sorry, yes a generic list can have members of its subclasses. See my answer for working code.
0

You could have a class RainBow which would extend a concrete implementation of List.
This is since a RainBow actually IS a list of colors.

public class RainBow extends ArrayList<Color> {

public void addColor(Color color)
{
    this.add(color);
}

public Color getTypeOfColor(String typeOfColor){
    for(Color c : this){
        if(c.getClass().getSimpleName().equals(typeOfColor)){
            return c;
        }
    }
    return null;
}
}

You can then use this class like so :

    Red red = new Red();
    Blue blue = new Blue();
    Rainbow rainbow = new Rainbow();
    rainbow.add(red);
    rainbow.add(blue);
    if (rainbow.getTypeOfColor("Yellow") == null) {
        //Color doesn't exist
    }
    if (rainbow.getTypeOfColor("Red") != null) {
        //Color exists
    }

Be aware this uses the class names for the comparisation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.