Constructors in Java:
We have seen earlier that objects are created using the new operator, for example:
Robot meetoo = new Robot();
This allocates memory space for a Robot object that is referenced by the variable meetoo. Given what we have done so far, this particular form of object creation would initialize the instance variables of the object with default values. The default value for variables of type int is zero, so this would create a robot initially at position (0, 0).
Java allows us to give an object a specific initial state when it is created. To do this, you must declare a constructor within the class.
For example, in our Robot class we would need to de?ne the constructor as follows:
public Robot ()
{
x=1;
y=1;
}
The effect of this is that whenever you create a Robot object, for example in:
Robot artoo;
artoo = new Robot();
the resulting robot will be positioned at (1, 1). The occurrence of the class name Robot following the new operator is actually a way of invoking the constructor. Like methods, constructors can have arguments - these allow initial values to be specified for the object when the constructor is invoked, rather than having fixed initial values coded into the body of the constructor.
For example, a Robot constructor that sets the initial x position and y position of a robot is shown below:
public Robot (int xPos, int yPos)
{
x = xPos;
y = yPos;
}
We could invoke the constructor as follows:
Robot deetoo;
deetoo = new Robot(6, 5);
To summarize, we set out the complete code for the Robot class; notice that two constructors are used:
// models an on-screen robot on a 2-D grid
public class Robot
{
// instance variables
private int x; // x-position(W-E) of robot
private int y; // y-position(N-S) of robot
// constructors
public Robot ()
{
x=1;
y=1;
}
public Robot (int xPos, int yPos)
{
x = xPos;
y = yPos;
} // methods
public void moveNorth ()
{
y=y+1;
}
public void moveSouth ()
{
y=y - 1;
}
public void moveEast ()
{
x=x+1;
}
public void moveWest ()
{
x=x - 1;
}
public void setX (int xPos)
{
x = xPos;
}
public void setY (int yPos)
{
y = yPos;
}
public int getX ()
{
return x;
}
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Constructors in Java homework and assignments? Live Constructors in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Constructors in Java homework help, java assignment help and Constructors in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours