Sunday, August 16, 2009

Puzzle 48 - Fruity Tale

Language – Java | Type – Concept | Last date 23-Aug-2009 12:00 p.m. | IST Points 3

I have been keeping pretty busy these days with lots of parallel stuff going on. Unfortunately this means that I will have to reduce some of the stuff I do and twister is one of them. Of course I still going to keep posting puzzles but at a reduced frequency - of one puzzle a week at least for some time!

On the other hand, this should give me some time to get Quiz4J up in shape which would also explore features of Java in a fun filled manner - keep watching this space for more information on that.

Coming back to Today's question, it is not really a puzzle and depending on your comfort level with Java this might just be easy. It does bring out an interesting feature though so give it a shot ...

<modifier> Fruits{
apples(
"red"), oranges("orange"), grapes("green");
String color
= null;
Fruits(String color){
this.color = color;
}
}

What does this code do? Can you complete this code and explain what it does or give an example of how it can be used?

Got a solution? Do leave it here.

2 comments:

  1. "enum" is the "modifier", although modifier is a bit misleading, here, I woudl say.

    This code creates a type-safe enumeration with apples, oranges, and grapes as members and for each member a string (the color) is stored.

    void printColor(Fruits fruit){ System.out.println(fruit.color);}

    ReplyDelete

Solution for this question?