Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

You Enumerate Me

Icon Leave Comment
One of the more interesting additions to java 1.5 was the stand along enum. Before then Javaphiles had to make a class with static values to "enumerate" objects:

public class Coin {

private static double QUARTER = 0.25;
private static double DIME = .10;
public double value;
public int radius;
public Color color;

public double getValue() 	{ return value; }
public int radius() 		{ return radius; }
public Color color()		{ return color; }

//so on and so forth
}



While this wasn't too much burden on us programmers, it was decided that we would be sent a gift. A beautiful baby boy, I mean enum. Here is the same concept, a coin enum, but in 1.5 language:

public enum Coin
{
	QUARTER (0.25, Color.LIGHT_GRAY, 50), //lookie constructors like classes!
	DIME (0.10, Color.LIGHT_GRAY, 40), //its too good to be true? 
	NICKEL(0.05, Color.GRAY, 45),
	PENNY (1, Color.YELLOW.darker(), 25);
	public double value;
	public int radius;
	public Color color;					//ARG pinch me! 
		
	//constructor, takes parameters and assigns them
	Coin(double value, Color color, int radius)
	{
		this.value = value;
		this.color = color;
		this.radius = radius;
	}
		
	public double getValue() 	{ return value; }
	public int radius() 		{ return radius; } //no way?!?!;-)
	public Color color()		{ return color; }
}



I was resistant to this new feature until I got to utilize it in a few school projects. Encapsulating all that data is simply excellent. Once it is set up, you don't have to worry about it or its members, just access it. Beautiful information hiding if I ever saw it. Example: calculateDeposit(Coin.QUARTER.getValue());

If you had any thoughts, criticism, or downright aversion to my last post, I'd like to hear about it! Perhaps you want me to write a whole comic book series on the memory leak? We'll see.

KYA out

0 Comments On This Entry

 

February 2020

S M T W T F S
      1
2345678
9101112131415
1617181920 21 22
23242526272829

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)