Java Programming for Beginners by Mark Lassoff

Java Programming for Beginners by Mark Lassoff

Author:Mark Lassoff
Language: eng
Format: epub, pdf
Tags: COM051280 - COMPUTERS / Programming Languages / Java, COM051210 - COMPUTERS / Programming / Object Oriented, COM051230 - COMPUTERS / Software Development and Engineering / General
Publisher: Dominic Shakeshaft
Published: 2017-10-31T08:29:31+00:00


Using variables in the program

To see this, let's create a brand new method, and in its declaration, we'll have it the same as our existing magic method. However, instead of taking a single integer as input, we'll provide it as input in the integer array:

package advancedmethods;

public class AdvancedMethods {

public static void main(String[] args) {

int[] x = 5;

magic(x);

System.out.println("main: " + x);

}

public static void magic(int input)

{

input += 10;

}

public static void magic(int[] input)

{

input += 10;

}

}

Remember that our array will be named as a single variable, so all we need to do to let Java know that we'd like to pass an array to the function is inform it that the variable being given is an array of a certain type. You'll also notice that we now have two methods by the name of magic within our program. This is called method overloading, and it's perfectly legitimate to do this as long as Java has a way of telling the methods apart. In this case, Java can tell the methods apart because both the methods are going to be given different objects as input.

One of our magic methods will execute if the input given to the magic call is a single integer, and our new magic method will execute if the input given to the method is an array of integers. Now, let's write a quick for loop so that our new magic method will increment the value of every integer contained within our input array by 10:

public static void magic(int[] input) { for(int i = 0; i < input.length; i++) input[i] += 10; }

This is extremely similar to the magic method we wrote originally, except that instead of operating on a single integer, it's going to operate on any number of them. However, something that may appear weird is going to happen when we modify our main method to utilize this new implementation of the magic method. In order to make this happen, we need to make a few quick modifications to our program.

Let's change our variable x from an integer to an integer array so that our program will know how to utilize the newly written magic method, which runs when we give an integer array as input:

package advancedmethods; import java.util.*; public class AdvancedMethods { public static void main(String[] args) { int[] x = {5,4,3,2,1}; magic(x); System.out.println("main: " + Arrays.toString(x)); } public static void magic(int input) { input += 10; } public static void magic(int[] input) { for(int i = 0; i < input.length; i++) input[i] += 10; } }

We'll also need to modify our println statement to make use of Arrays.toString so that we display the value stored in the x array properly. We'll import java.util so that Java knows about the Arrays library:

import java.util.*; public class AdvancedMethods { public static void main(String[] args) { int[] x = {5,4,3,2,1}; magic(x); System.out.println("main: " + Arrays.toString(x)); }

Now it's time to ask ourselves another question: when we run the magic function on an integer array, will we see the same results we saw when



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.