Showing posts with label constructor. Show all posts
Showing posts with label constructor. Show all posts

Saturday, May 25, 2013

Enums in java: Another Example

Here's another example of using enums. Note that the constructor of an enum cannot be public.

checkout the code here

/**
 * An enum for various payment modes.
 * The payment mode also specifies a surcharge to be charged for making payments.
 *
 * @author megha birmiwal
 */
public enum PaymentMode {
        CASH(0),
        CHECK(0.5),
        CREDIT_CARD(1.5),
        DEBIT_CARD(1.0),
        GIFT_CARD(0),
        PAYPAL(1.5),
        MONEY_ORDER(0.3);

        private final double surcharge;

        private PaymentMode(double surcharge) {
                this.surcharge = surcharge;
        }

        public double getSurcharge() {
                return surcharge;
        }
}

/**
 * Lists all payment modes and their surcharges.
 *
 * @author megha birmiwal
 */
public class PaymentModeLister {

        public static void main(String[] args) {
                // .values() returns an array of all possible values
                PaymentMode[] values = PaymentMode.values();
                for (PaymentMode pm : PaymentMode.values()) {
                        System.out.println(pm.name() + " / " + pm.getSurcharge());
                }

                // go from string to the corresponding enum
                PaymentMode pm = PaymentMode.valueOf("PAYPAL");
                System.out.println(pm == PaymentMode.PAYPAL);

                // throws exception because there is no enum value called XYZ
                PaymentMode mode = PaymentMode.valueOf("XYZ");
        }
}

Thursday, September 15, 2011

Overloaded Constructors

Here's how we can overload constructors - and use any one of them at create time.

browse

package org.megha.blog.example.part3;

/**
 * Represents a person, with a name and a title.
 */
public class Person {

	private String name;
	private String title;

	/** creates a {@link Person} with a name and a title */
	public Person(String name, String title) {
		this.name = name;
		this.title = title;
	}

	/** creates a {@link Person} with only a name (title is set to UNKNOWN) */
	public Person(String name) {
		this(name, "UNKNOWN"); // calls the previous constructor
                                       // with title as UNKNOWN
	}

	/** creates a {@link Person} without a name or a title */
	public Person() {
		this("NO_NAME"); 
                // note how the one-arg constructor is called, 
                // which calls the two arg constructor
	}

	public void print() {
		System.out.println("----------------------");
		System.out.println("Name = " + name);
		System.out.println("Title = " + title);
	}

	public static void main(String args[]) {
		// Person class has overloaded constructors,
                // and we can call any of these.
		Person person1 = new Person();
		Person person2 = new Person("Sam");
		Person person3 = new Person("Sam","Engineer");

		person1.print();
		person2.print();
		person3.print();
	}
}
Output of this program:
----------------------
Name = NO_NAME
Title = UNKNOWN
----------------------
Name = Sam
Title = UNKNOWN
----------------------
Name = Sam
Title = Engineer

Instantiation

This program shows how you create an object/instance of a class, and call methods in the created object.

browse

package org.megha.blog.example.part2;

/**
 * A Circle that can calculate it's area and perimeter.
 */
public class Circle {

	private double radius;

	/** 
	 * Constructor to create a circle of a given radius.
	 * Note: how you cannot create a Circle without specifying it's radius!
	 */
	public Circle(double radius) {
		this.radius = radius;
	}

	/** @return the radius of the circle */
	public double getRadius() {
		return radius;
	}

	/** @return the area of a circle (PI*r*r) */
	public double getArea() {
		double area = Math.PI*radius*radius;
		return area;
	}

	/** @return the perimeter of the circle (2*PI*r) */
	public double getPerimeter() {
		double perimeter = 2 * Math.PI * radius;
		return perimeter;
	}

	/** prints the radius, area and perimeter of this circle */
	public void print() {
		System.out.println("Circle, with radius  " + getRadius());
		System.out.println("Area = " + getArea());
		System.out.println("Perimeter = " + getPerimeter());
	}

	public static void main(String args[]) {
		// create a circle of radius 10
		Circle circle1 = new Circle(10);
		// print information about the circle we just created
		circle1.print();
	}
}