Question

The code snippet below is an example of pass by reference. We also provide a sample...

The code snippet below is an example of pass by reference. We also provide a sample method
setZeroAt, which sets 0 at a position i in the array, to illustrate pass by reference.
 
  public class Sample {
      public static void setZeroAt(int [] arr, int i){
        arr[i] = 0;
      }

      public static void main(String[] args) {
             Scanner input = new Scanner(System.in);
             int sample[] = {1, 2, 3};
             setZeroAt(sample, 1);
             //Now sample looks like {1, 0, 3}
      }
  }
 
Write a Java method that takes an array and two integer indices a and b. If a and b are
valid integer indices for the array, swap the values in that position.
 
Also ensure that the two indices are valid. If not, do not swap the values. Remember that
when you pass an array as a parameter, it is passed by reference.  Use the code snippet below.
 
For example, consider the array {0, 1, 2, 3}. For a = 0, b = 2, the swapped array must be
{2, 1, 0, 3}. You can swap with a = 2, b = 0 on the original array to get the same result.
 
import java.util.Scanner;

public class SwapArrayIndex {
     
      public static void print_array(int [] arr){
              for(int i = 0; i < arr.length; ++i)
                     System.out.print(arr[i]+" ");
              System.out.println();
      }
      public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              //Start of Sample Code
              int sample[] = {1, 2, 3};
              setZeroAt(sample, 1);
              //Now sample looks like {1, 0, 3}
              //End of Sample Code
              int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
              print_array(arr);
              int a = input.nextInt();
              int b = input.nextInt();
              swap(arr, a, b);
              print_array(arr);
      }
}


Part B: Random Shuffling
------------------------
Use swap method to perform random shuffling. Write a shuffle method that takes an array. It
generates two valid indices randomly, that can be swapped and calls the swap method to swap
the positions in the array. Look at question 1 for generating a valid index (but look out for
an obvious pitfall).
0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
The code snippet below is an example of pass by reference. We also provide a sample...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

  • 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...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • 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...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Searching an array: --Using the template provided, create a method called search that takes an int...

    Searching an array: --Using the template provided, create a method called search that takes an int and an array as parameters. The method search should traverse the array to look for the int. search should print whether it found the int or not, for example: found: 10 did not find: 11 examples (bold face is text entered by user) % java SearchArray 20 found: 20 % java SearchArray 13 did not find: 13 % java SearchArray 100 found: 100 %...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

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