Question

Show what is printed on the screen when this progr
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Practiceseven // Class declaration
{
   public static void main(String[] args) //main method declaration which means execution first starts here
{
int[] array = { 4,6,2,8,10 }; // array intialisation and declaration
printArray(array); // calling printArray method
int number=5; // declaring temp variable for swapping
swap(array,number); // calling swap method
printArray(array); //calling printArray method
}

static void printArray(int[] array) //method declaraton
{
System.out.print("the values in array are:");
for(int i=0;i<array.length;i++) //checking and sending array elements one by one for printing
{
System.out.print(array[i] +" "); //printing array.
}
System.out.println("]");
}
static void swap(int[] integers,int num) //swap method declaration
{
if(integers.length>0) //checking array length and comparing with 0
{
integers[0]=num; //replacing element in array(0) location to 'num' value
}
num=0;   
}
}

output:-

Successfully compiled /tmp/java_k7jfdh/Practiceseven.java <-- main method the values in array are:462810 the values in arrayThis program is for printing the values in the array and with the swap function.

==>Arrays are contigious memory locations which stores values in memory locations continiously one by one.Memory locations in array start from array(0) to increasing manner. Whenever we want to retrieve the values we can retrieve that by using the memory locations.

-==> Swapping is a function or method which is used to replace a number using temperory variable.

-->Here in this program temp variable is 'number',It is being swapped by the array elements this causes value in the will be replaced with this value.

In this program we are initialised and declared an array with some elements and also a swap method.

-> First we are printing the array as it is with the declared elements in the printArray method it first prints the message which is given and the it checks the length of the array and values and send those values for printing.

-->In the swap method it first checks the length of the array if the length is greater than 0 then this method swaps the the element in the 0th location with the 'num' value, other wise replace with '0'.

Add a comment
Know the answer?
Add Answer to:
Show what is printed on the screen when this program is executed. Show work for partial...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT