Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 1.12 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package learnquest;
  2.  
  3. public class Rectangle {
  4.         private final Size size;
  5.  
  6.         public Rectangle(Size size) {
  7.                 this.size = size;
  8.         }
  9.  
  10.         public Size getSize() {
  11.                 return size;
  12.         }
  13.  
  14.         public void getArea() {
  15.                 size.area();
  16.         }
  17.  
  18.         public static void main(String[] args) {
  19.                 Rectangle small = new Rectangle(Size.SMALL);
  20.                 Rectangle med = new Rectangle(Size.MEDIUM);
  21.                 Rectangle large = new Rectangle(Size.LARGE);
  22.                 small.getArea();
  23.                 med.getArea();
  24.                 large.getArea();
  25.         }
  26.         private static void foo(Size size){
  27.                 switch(size){
  28.                 case SMALL:
  29.                         ;
  30.                 case MEDIUM:
  31.                         ;
  32.                 case LARGE:
  33.                         ;
  34.                 }
  35.         }
  36. }
  37.  
  38. enum Size {
  39.         SMALL(5, 5) {
  40.                 public void area() {
  41.                         System.out.println("Area of SMALL");
  42.                 }
  43.         },
  44.         MEDIUM(7, 7) {
  45.                 @Override
  46.                 public void area() {
  47.                         System.out.println("Area of MEDIUM");
  48.  
  49.                 }
  50.         },
  51.         LARGE(10, 10) {
  52.                 @Override
  53.                 public void area() {
  54.                         System.out.println("Area of LARGE");
  55.  
  56.                 }
  57.         };
  58.         private final int height;
  59.         private final int width;
  60.  
  61.         private Size(int height, int width) {
  62.                 this.height = height;
  63.                 this.width = width;
  64.         }
  65.  
  66.         public int getHeight() {
  67.                 return height;
  68.         }
  69.  
  70.         public int getWidth() {
  71.                 return width;
  72.         }
  73.  
  74.         public abstract void area();
  75. }