Question

In JAVA Thank You What is the exact output produced by running the method test? /**...

In JAVA Thank You

What is the exact output produced by running the method test?

/**
* TestSwap.java
* Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */

public class TestSwap
{
    public void swap (int x, int y)
    {

int temp;
temp = x;
x = y;
y = temp;
System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y);

}

    public void swap (int[] a, int i, int j)
    {

int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
System.out.println("Inside swap version 2:"); System.out.println("a[" + i + "] = " + a[i]); System.out.println("a[" + j + "] = "+ a[j]);

}

    public void printArray (int[] a)
    {

System.out.print("Array elements: ");

for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");

System.out.println(); }

    public static void test()
    {

TestSwap t = new TestSwap(); final int ARRAY_SIZE = 3;
int[] arr = new int[ARRAY_SIZE]; arr[0] = 3;

arr[1] = 8; arr[2] = 6;
int x = 1, y = 2;

t.printArray (arr);
t.swap (x, y);
System.out.println ("Inside test:"); System.out.println ("x = " + x); System.out.println ("y = " + y); t.printArray (arr);
t.swap (arr[x], arr[y]); t.printArray (arr);
t.swap (arr, x, y);
t.printArray (arr);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the Java program 4 methods are used as

  1. public void swap (int x, int y)   

swaps two integers

  1. public void swap (int[] a, int i, int j)

swaps two integers but choosen from array with index I and j

  1. public void printArray (int[] a)

This will print the elements of the array

  1. public static void test()

It is the final method where the other methods are called

In side test()

First the elements 3,8,6 are assigned to the array then the above three methods are called one after other.

So the out of the test method will be

OUTPUT

Array elements : 3 8 6

Inside swap version 1:

X = 2

Y = 1

Inside test:

X = 1

Y = 2

Array elements : 3 8 6

Inside swap version 1:

X = 6

Y = 8

Array elements : 3 8 6

Inside swap version 2:

a[1] = 6

a[2] = 8

Array elements : 3 6 8

Add a comment
Know the answer?
Add Answer to:
In JAVA Thank You What is the exact output produced by running the method test? /**...
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
  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational...

    please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational class ****JAVA**** Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The class definition should look like this: public class Rational extends Number implements Comparable<Rational> { // code goes here } In addition a new private method gcd should be defined to ensure that the number is always represented in the lowest terms. Here is the...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

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
ADVERTISEMENT