Methods that return a value:
As well as methods that move the robot, we also have methods that indicate the current position of the robot. The two methods we need are called getX and getY. These methods each return a value, the current x or y position of the Robot object.The code for these two methods is shown below:
public int getX()
{
return x;
}
public int getY()
{
return y;
}
The method header includes the keyword int (rather than void as before) to indicate that the method will return an integer value. In the body of the method, the return keyword precedes the value to be returned. In this case, the methods simply return the value of the appropriate instance variable.
In previous discussions of methods that return a value, we have not explained what happens to the returned value after the method has done its work. By way of explanation, we now look at the method getX. We can invoke this method to find the current x position of the Robot object referenced by deetoo as follows:
deetoo.getX();
However, this statement is not very useful as we have not made use of the returned value in this case, it would be discarded. The simplest thing to do is to save the value by assigning it to an integer variable as follows:
int currentX;
currentX = deetoo.getX();
We can then use this saved value in any way appropriate to an integer value. For example, we could display it in a message:
System.out.println("Robot at x="+currentX);
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Methods that return a value homework and assignments? Live Methods that return a value experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Methods that return a value homework help, java assignment help and Methods that return a value 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