Credits

Saturday, November 27, 2010

More Java Constructors

Other illustrations of the use of constructors are the ones cited below. One program simply use constructors without the parameters while the other one makes use of parameters.

These programs however need another calling program to call these codes. Further, I used mutator and accessor methods to assign and read fields. Comparing two codes, the one that uses global fields were called anywhere in the program.

 
[+/-] show/hide


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Rose
*/
public class EventSiteConstructor {

private int siteNumber;
private double usageFee;
private String managerName;
EventSiteConstructor(){
siteNumber=5;
usageFee=1000.00;
managerName="Rosilie";
}

public int getSiteNumber()
{ return siteNumber;}
public void setSiteNumber(int n)
{ siteNumber=n;}

public void setUsageFee(double amt)
{ usageFee=amt;}

public double getUsageFee()
{ return usageFee;}
public String getManagerName()
{ return managerName;}

public void setManagerName(String name)
{ managerName=name;}

}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Rose
*/
public class EventSiteConstructorv2 {

private int siteNumber;
private double usageFee;
private String managerName;

EventSiteConstructorv2(int x, double y, String z){
siteNumber=x;
usageFee=y;
managerName=z;
}

public int getSiteNumber()
{ return siteNumber;}
public void setSiteNumber(int n)
{ siteNumber=n;}

public void setUsageFee(double amt)
{ usageFee=amt;}

public double getUsageFee()
{ return usageFee;}
public String getManagerName()
{ return managerName;}

public void setManagerName(String name)
{ managerName=name;}

}

AddThis