User defined constructors Assignment Help

Assignment Help: >> Constructors in Java >> User defined constructors

User defined constructors:

Often when you are writing classes, you will provide a number of constructors, catering for a selection of initialization possibilities. Each constructor must have a unique set of arguments (if any). For example, the code for the class Coordinate
shown below contains three constructors.

public class Coordinate
{
private int xPos, yPos;

private static final int DEFAULT_X = 0;

private static final int DEFAULT_Y = 0;

// two-argument constructor
public Coordinate (int xVal, int yVal)
{
xPos = xVal;

yPos = yVal;

}

// one-argument constructor
public Coordinate (int xVal)
{
xPos = xVal;

yPos = DEFAULT_Y;

}

// zero-argument constructor
public Coordinate ()
{
xPos = DEFAULT_X;

yPos = DEFAULT_Y;

}

// methods of the class

}

It is good practice to set all instance variables to some value, however many arguments the constructor has. Above, we have used constants to de?ne default initial values for the instance variables of the class when the user has not provided values. Here are some more examples of classes and constructors. 

Consider a class that is used to store details about voters. The class defines an age instance variable. We will write a zero-argument constructor to create a voter with default age and a one-argument constructor to set the voter's age to an integer value.

public class Voter
{
private int age;

private final static int MINIMUM_AGE = 18;

// a one-argument constructor
public Voter (int personAge)
{
age = personAge;

}

// a zero-argument constructor
public Voter ()

{

age = MINIMUM_AGE;

}

// methods...

}

We used a final static constant here to create a default age. Any object created using the zero-argument constructor would initially have its age variable set to this value. The one-argument constructor allows the programmer to specify the initial value of the age variable. It may not be desirable to provide a zero-argument constructor as we did above. We may not want to allow a Voter to be constructed if the programmer cannot provide the voter's age. It may also be desirable to have the one- argument constructor perform a test to ensure that the age it receives as an argument is greater than or equal to the minimum age.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your User defined constructors homework and assignments? Live User defined constructors experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer User defined constructors homework help, java assignment help and User defined constructors projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd